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
50
votes
9 answers

If free() knows the length of my array, why can't I ask for it in my own code?

I know that it's a common convention to pass the length of dynamically allocated arrays to functions that manipulate them: void initializeAndFree(int* anArray, size_t length); int main(){ size_t arrayLength = 0; scanf("%d", &arrayLength); …
Chris Cooper
  • 17,276
  • 9
  • 52
  • 70
46
votes
9 answers

How to check if a pointer is freed already in C?

I would like to check if a pointer is freed already or not. How do I do this using gnu compiler set?
Kitcha
  • 1,641
  • 6
  • 19
  • 20
44
votes
2 answers

If `malloc(0)` returns a non-null pointer, can I pass that to `free`?

I've been reading over discussions of how malloc behaves when you request a zero sized block. I understand that the behavior of malloc(0) is implementation-defined, and it is supposed to either return a null pointer, or a non-null pointer that I am…
pnkfelix
  • 3,770
  • 29
  • 45
43
votes
5 answers

C: Correctly freeing memory of a multi-dimensional array

Say you have the following ANSI C code that initializes a multi-dimensional array : int main() { int i, m = 5, n = 20; int **a = malloc(m * sizeof(int *)); //Initialize the arrays for (i = 0; i < m; i++) { …
Andreas Grech
  • 105,982
  • 98
  • 297
  • 360
42
votes
5 answers

Should I free memory before exit?

Should I free all my mallocated memory when I am exiting program in the due of error? something = (char**) malloc (x * sizeof(char*)); for (i = 0; i < x; i++) something[i] = (char*) malloc (y + 1); ... if (anything == NULL) { printf("Your…
Lucfia
  • 621
  • 1
  • 7
  • 14
39
votes
5 answers

Can I assume that calling realloc with a smaller size will free the remainder?

Let’s consider this very short snippet of code: #include int main() { char* a = malloc(20000); char* b = realloc(a, 5); free(b); return 0; } After reading the man page for realloc, I was not entirely sure that the…
qdii
  • 12,505
  • 10
  • 59
  • 116
39
votes
7 answers

Is free() zeroing out memory?

Until today I lived in belief that calling free() on memory space releases it for further allocation without any other modifications. Especially, considering this SO question that clearly states that free() DOESN'T zero out memory. Yet, let's…
browning0
  • 901
  • 2
  • 11
  • 21
35
votes
3 answers

Proper Way to Free Memory of a Returned Variable

I created a function designed to get user input. It requires that memory be allocated to the variable holding the user input; however, that variable is returned at the end of the function. What is the proper method to free the allocated…
w m
  • 493
  • 1
  • 5
  • 7
35
votes
3 answers

C free(): invalid pointer

I am teaching myself C. My goal is to make a C function that just walks a query string and splits on the ampersand and the equals sign. I am getting stuck on this error from Valgrind. ==5411== Invalid free() / delete / delete[] / realloc() ==5411== …
Dan
  • 2,209
  • 3
  • 23
  • 44
34
votes
3 answers

malloc implementation?

I'm trying to implement malloc and free for C, and I am not sure how to reuse memory. I currently have a struct that looks like this: typedef struct _mem_dictionary { void *addr; size_t size; int freed; } mem_dictionary; My malloc looks…
user675257
  • 341
  • 1
  • 3
  • 3
34
votes
4 answers

double free or corruption (!prev) error in c program

I get the following error when running a c program: *** glibc detected *** ./a.out: double free or corruption (!prev): 0x080b8008 *** I believe this is due to free() being called at the end of the program, but I can't figure out where the malloc'd…
user1640921
  • 343
  • 1
  • 3
  • 4
33
votes
5 answers

LinkedList - How to free the memory allocated using malloc

I have a very simple C code for constructing a Singly Linked list as below, in which I allocate memory for each node dynamically using malloc. At the end of code, I want to free the memory for each node allocated, was wondering how to go about it -…
goldenmean
  • 18,376
  • 54
  • 154
  • 211
32
votes
3 answers

Does malloc reserve more space while allocating memory?

I am observing the following behavior in my test program: I am doing malloc() for 1 MB and then free() it after sleep(10). I am doing this five times. I am observing memory consumption in top while the program is running. Once free()-d, I am…
user1228352
  • 569
  • 6
  • 21
30
votes
6 answers

Allocate memory and save string in c

I was wondering why the following code isnt't working int main(int argc, char **argv) { char *test = (char*) malloc(12*sizeof(char)); test = "testingonly"; free(test); } After thinking about it my assumption was that first i allocate…
pluckyDuck
  • 1,099
  • 3
  • 13
  • 14
28
votes
6 answers

C++ freeing static variables

I would like my class to have a static pointer to a dynamically allocated region of memory. I understand how to initialize it - in my case I will initialize it when the first object needs it. However, I don't know when/where in the code to free…
jbu
  • 15,831
  • 29
  • 82
  • 105