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
651
votes
20 answers

What REALLY happens when you don't free after malloc before program termination?

We are all taught that you MUST free every pointer that is allocated. I'm a bit curious, though, about the real cost of not freeing memory. In some obvious cases, like when malloc() is called inside a loop or part of a thread execution, it's very…
Scott
  • 10,407
  • 13
  • 37
  • 35
507
votes
11 answers

How does free know how much to free?

In C programming, you can pass any kind of pointer you like as an argument to free, how does it know the size of the allocated memory to free? Whenever I pass a pointer to some function, I have to also pass the size (ie an array of 10 elements needs…
Joshua Cheek
  • 30,436
  • 16
  • 74
  • 83
315
votes
13 answers

How do malloc() and free() work?

I want to know how malloc and free work. int main() { unsigned char *p = (unsigned char*)malloc(4*sizeof(unsigned char)); memset(p,0,4); strcpy((char*)p,"abcdabcd"); // **deliberately storing 8bytes** cout << p; free(p); //…
Priyanka Mishra
  • 10,400
  • 18
  • 62
  • 75
192
votes
23 answers

Setting variable to NULL after free

In my company there is a coding rule that says, after freeing any memory, reset the variable to NULL. For example ... void some_func () { int *nPtr; nPtr = malloc (100); free (nPtr); nPtr = NULL; return; } I feel that, in…
Alphaneo
  • 12,079
  • 22
  • 71
  • 89
147
votes
11 answers

Does free(ptr) where ptr is NULL corrupt memory?

Theoretically I can say that free(ptr); free(ptr); is a memory corruption since we are freeing the memory which has already been freed. But what if free(ptr); ptr=NULL; free(ptr); As the OS will behave in an undefined manner I cannot get an…
Vijay
  • 65,327
  • 90
  • 227
  • 319
120
votes
9 answers

How to track down a "double free or corruption" error

When I run my (C++) program it crashes with this error. * glibc detected * ./load: double free or corruption (!prev): 0x0000000000c6ed50 *** How can I track down the error? I tried using print (std::cout) statements, without success. Could gdb…
neuromancer
  • 53,769
  • 78
  • 166
  • 223
93
votes
12 answers

Why does `free` in C not take the number of bytes to be freed?

Just to be clear: I do know that malloc and free are implemented in the C library, which usually allocates chunks of memory from the OS and does its own management to parcel out smaller lots of memory to the application and keeps track of the number…
90
votes
12 answers

Unable to free const pointers in C

How can I free a const char*? I allocated new memory using malloc, and when I'm trying to free it I always receive the error "incompatible pointer type" The code that causes this is something like: char* name="Arnold"; const char* str=(const…
lego69
  • 1,403
  • 3
  • 12
  • 15
83
votes
3 answers

Why cast free's return value to void?

I am reading a book (Programming with POSIX Threads by Butenhof, 1997) that uses C, and I came across the following line: (void)free(data); Here, data is just a pointer to an allocated struct, data = malloc(sizeof(my_struct_t)); Why is the result…
Adam Johnston
  • 1,399
  • 2
  • 12
  • 23
72
votes
8 answers

How do free and malloc work in C?

I'm trying to figure out what would happened if I try to free a pointer "from the middle" for example, look at the following code: char *ptr = (char*)malloc(10*sizeof(char)); for (char i=0 ; i<10 ; ++i) { ptr[i] =…
user238082
  • 723
  • 1
  • 6
  • 7
70
votes
8 answers

Will malloc implementations return free-ed memory back to the system?

I have a long-living application with frequent memory allocation-deallocation. Will any malloc implementation return freed memory back to the system? What is, in this respect, the behavior of: ptmalloc 1, 2 (glibc default) or 3 dlmalloc tcmalloc…
osgx
  • 90,338
  • 53
  • 357
  • 513
68
votes
3 answers

Free memory allocated in a different function?

I'm trying to learn C and I'm currently trying to write a basic stack data structure, but I can't seem to get basic malloc/free right. Here's the code I've been using (I'm just posting a small part here to illustrate a specific problem, not the…
Jeff Tratner
  • 16,270
  • 4
  • 47
  • 67
56
votes
5 answers

Is this a good way to free memory?

The function for freeing an instance of struct Foo is given below: void DestroyFoo(Foo* foo) { if (foo) free(foo); } A colleague of mine suggested the following instead: void DestroyFoo(Foo** foo) { if (!(*foo)) return; Foo *tmpFoo…
Ferit Buyukkececi
  • 2,665
  • 2
  • 20
  • 23
52
votes
10 answers

Should one really set pointers to `NULL` after freeing them?

There seem to be two arguments why one should set a pointer to NULL after freeing them. Avoid crashing when double-freeing pointers. Short: Calling free() a second time, by accident, doesn't crash when it's set to NULL. Almost always this masks a…
Georg Schölly
  • 124,188
  • 49
  • 220
  • 267
51
votes
1 answer

Understanding "corrupted size vs. prev_size" glibc error

I have implemented a JNA bridge to FDK-AAC. Source code can be found in here When bench-marking my code, I can get hundreds of successful runs on the same input, and then occasionally a C-level crash that'll kill the entire process, causing a…
Sheinbergon
  • 2,875
  • 1
  • 15
  • 26
1
2 3
99 100