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
27
votes
6 answers

Can a call to free() in C ever fail?

Can a call to free() fail in any way? For example: free(NULL);
Taichman
  • 1,108
  • 2
  • 10
  • 21
25
votes
8 answers

Is freeing allocated memory needed when exiting a program in C

If I allocated memory in my C program using malloc and now I want to exit, do I have to free the allocated memory, or can I assume that since my entire program terminates, it will be freed by the OS? I run in Linux environment.
SIMEL
  • 8,745
  • 28
  • 84
  • 130
25
votes
8 answers

What is the difference between freeing the pointer and assigning it to NULL?

Could somebody tell me the difference between: int *p; p=(int*)malloc(10*sizeof(int)); free(p); or int *p; p=(int*)malloc(10*sizeof(int)); p=NULL;
Sanjeev Kumar Dangi
  • 6,925
  • 7
  • 28
  • 30
24
votes
9 answers

Freeing memory twice

In C and C++, Freeing a NULL pointer will result in nothing done. Still, I see people saying that memory corruption can occur if you "free memory twice". Is this true? What is going on under the hood when you free memory twice?
Vijay
  • 65,327
  • 90
  • 227
  • 319
24
votes
7 answers

Trying to use free() to understand how it works

To understand the usage of free in the C programming language I tried running this code on Ubuntu, but on running the EXE file I am receiving a SIGABRT error. Why is the program not exiting normally? #include #include int…
user3314983
  • 405
  • 3
  • 11
24
votes
4 answers

Do I need to free the strtok resulting string?

Or rather, how does strtok produce the string to which it's return value points? Does it allocate memory dynamically? I am asking because I am not sure if I need to free the token in the following code: The STANDARD_INPUT variables is for exit…
Sunspawn
  • 817
  • 1
  • 12
  • 27
22
votes
6 answers

Howto check if a char* points to a string literal in C

I have a struct struct request { int code; char *message; }; that I'd like to free properly. I have the following function to do that: void free_request(struct request *req) { if (req->message != NULL) { free(req->message); } …
Niklas
  • 223
  • 2
  • 5
21
votes
7 answers

How bad it is to keep calling malloc() and free()?

I'm sending a text file - client-server breakup the text into packets each of 512 bytes but some packets contain text less than max size so on the servers side when receiving each packet I'm calling malloc() to build a string again , is this a bad…
cap10ibrahim
  • 587
  • 1
  • 6
  • 16
21
votes
4 answers

Calling free on a pointer twice

I have been taught in lectures, that calling free() on a pointer twice is really, really bad. I know that it is good practice, to set a pointer to NULL, right after having freed it. However, I still have never heard any explanation as to why that…
Joe
  • 213
  • 1
  • 2
  • 7
21
votes
2 answers

What can cause "corrupted double-linked list" error?

I am having problems with a fairly complex code. I wasn't able to produce a short snippet that reproduces the error, so I'll try to explain the problem in words. The code crashes randomly with the error *** glibc detected *** gravtree: corrupted…
hanno
  • 6,401
  • 8
  • 48
  • 80
19
votes
3 answers

Code for malloc and free

Where can I find the code for malloc my gcc compiler is using at the moment? I actually want to write my own malloc function which will be a little different from the original one. I know I can use hooks et all, but I want to see the real code.
MetallicPriest
  • 29,191
  • 52
  • 200
  • 356
19
votes
3 answers

C - Does freeing an array of pointers also free what they're pointing to?

Say I have an array of pointers to structs that contain a string each and so for something like this: printf("%s\n", array[0]); The output is: Hello. If I perform a free(array) will this free what array[0] is pointing to? ("Hello."). I've spent…
Laefica
  • 489
  • 1
  • 5
  • 13
19
votes
4 answers

fclose() then free()?

Assume I have the following program: #include int main () { FILE * pFile; pFile = fopen ("myfile.txt","r"); fclose (pFile); //This never happens: free(pFile) return 0; } I have never seen a program which does…
Oliver Spryn
  • 16,871
  • 33
  • 101
  • 195
18
votes
2 answers

Where do malloc() and free() store allocated sizes and addresses?

Where do malloc() and free() store the allocated addresses and their sizes (Linux GCC)? I've read that some implementations store them somewhere before the actual allocated memory, but I could not confirm that in my tests. The background, maybe…
Elmar Weber
  • 2,683
  • 28
  • 28
18
votes
5 answers

What is the correct way to dynamically create/release runtime forms?

I always try to create my Applications with memory usage in mind, if you dont need it then don't create it is the way I look at it. Anyway, take the following as an example: Form2:= TForm2.Create(nil); try Form2.ShowModal; finally …
user741875