Questions tagged [free]

free is a function to deallocate memory obtained from malloc and other functions in C. Do not use this tag to refer to free software. Asking for software recommendation is off-topic on Stack Overflow. If you have a question about free software, you can ask here: https://softwarerecs.stackexchange.com/

free() is the Standard C (ISO 9899:1989) function declared in <stdlib.h> to deallocate memory allocated by a previous call to malloc, calloc, or realloc. On POSIX (IEEE Std 1003.1) systems, it is also used to free memory obtained from calls to posix_memalign and strdup.


NAME

free - free allocated memory

SYNOPSIS

#include <stdlib.h>

void free(void *ptr);

DESCRIPTION

The free() function frees the memory space pointed to by ptr, which must have been returned by a previous call to malloc(), calloc() or realloc(). Otherwise, or if free(ptr) has already been called before, undefined behavior occurs. If ptr is NULL, no operation is performed.

RETURN VALUE

The free() function returns no value.


Wikipedia

References

Related Tags

2564 questions
14
votes
3 answers

How does free() affect the memory address on the heap?

This assignment asks us to allocate two int-type variables using malloc() (named var1 and var2), print the addresses of each variable (the address of the pointer on the stack and the address on the heap), then use free() to deallocate var1, print…
Dustin R.
  • 143
  • 1
  • 6
14
votes
5 answers

How to use strtok in C properly so there is no memory leak?

I am somewhat confused by what happens when you call strtok on a char pointer in C. I know that it modifies the contents of the string, so if I call strtok on a variable named 'line', its content will change. Assume I follow the bellow…
user246392
  • 2,661
  • 11
  • 54
  • 96
14
votes
4 answers

Free an assigned pointer

Does the following code free the memory that was allocated for x? int main() { char *x = (char*)calloc(100, sizeof(char)); char *y = x; free(y); }
cxphong
  • 847
  • 2
  • 9
  • 18
14
votes
2 answers

Do I need to free local variables?

I have the following struct: typedef struct cell Cell; struct cell { int value; int *nextcell; }; And I have the following function to free a linked list: void freelist(Cell *beginning) { Cell *thisCell = beginning; Cell *NextCell =…
Thi G.
  • 1,578
  • 5
  • 16
  • 27
14
votes
6 answers

When do I need dynamic memory?

Possible Duplicate: Malloc or normal array definition? We learn that there is dynamic memory in C and dynamic variables: #include int a = 17; int main(void) { int b = 18; //automatic stack memory int * c; c = malloc( sizeof( int )…
Niklas Rosencrantz
  • 25,640
  • 75
  • 229
  • 424
13
votes
2 answers

Can I force a numpy ndarray to take ownership of its memory?

I have a C function that mallocs() and populates a 2D array of floats. It "returns" that address and the size of the array. The signature is int get_array_c(float** addr, int* nrows, int* ncols); I want to call it from Python, so I use…
Tom Future
  • 1,900
  • 2
  • 15
  • 15
13
votes
4 answers

C free and struct

My question is about C free() function for deallocating memory blocks previously allocated with malloc(). If i have a struct data type compose of several pointers, each of them pointing to different memory locations, what would happen to those…
Leandro Galluppi
  • 915
  • 1
  • 7
  • 19
13
votes
8 answers

Why do I have to use free on a pointer but not a normal declaration?

Why do I have to use free() when I declare a pointer such as: int *temp = (int*)malloc(sizeof(int)) *temp = 3; but not when I do: int temp = 3;
samoz
  • 56,849
  • 55
  • 141
  • 195
13
votes
13 answers

Determining Whether Pointer is Valid

It has been my observation that if free( ptr ) is called where ptr is not a valid pointer to system-allocated memory, an access violation occurs. Let's say that I call free like this: LPVOID ptr = (LPVOID)0x12345678; free( ptr ); This will most…
Jim Fell
  • 13,750
  • 36
  • 127
  • 202
13
votes
1 answer

How to free and garbage collect a WebGL context?

I'm developing a WebGL application for web and mobile. I often use hard-refreshs to test the results of my WebGL implementation. After a view tries, I get the error: Error: WebGL: Exceeded 16 live WebGL contexts for this principal, losing the least…
q9f
  • 11,293
  • 8
  • 57
  • 96
13
votes
7 answers

Should I free allocated memory on abnormal termination?

My program (a text-mode web browser) is dynamically allocating memory. I do free unneeded blocks during runtime, of course. And I do free everything before normal termination - so that memory leak checkers won't give me false positives (and to be…
RWS
  • 625
  • 3
  • 12
13
votes
2 answers

new, delete ,malloc & free

This question was asked to me in an interview: In C++, what if we allocate memory using malloc and use delete to free that allocated memory? what if we allocate the memory using new and free it using free? What are the problems that we would…
Vijay
  • 65,327
  • 90
  • 227
  • 319
13
votes
7 answers

Why exactly should I not call free() on variables not allocated by malloc()?

I read somewhere that it is disastrous to use free to get rid of an object not created by calling malloc, is this true? why?
ultrajohn
  • 2,527
  • 4
  • 31
  • 56
13
votes
8 answers

Freeing in an atexit()

Is there any point to freeing memory in an atexit() function? I have a global variable that gets malloc'ed after startup. I could write an atexit() function to free it, but isn't the system going to reclaim all that memory when the program exits…
Matt
  • 84,419
  • 25
  • 57
  • 67
13
votes
6 answers

Freeing strings in C

If I would write: char *a=malloc(sizeof(char)*4); a="abc"; char *b="abc"; do I need to free this memory, or is it done by my system?
Andna
  • 6,539
  • 13
  • 71
  • 120