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
17
votes
8 answers

C - Design your own free( ) function

Today, I appeared for an interview and the interviewer asked me this, Tell me the steps how will you design your own free( ) function for deallocate the allocated memory. How can it be more efficient than C's default free() function ?…
Saurabh Gokhale
  • 53,625
  • 36
  • 139
  • 164
17
votes
6 answers

How to avoid long chain of free's (or deletes) after every error check in C?

Suppose I write my code very defensively and always check the return types from all the functions that I call. So I go like: char* function() { char* mem = get_memory(100); // first allocation if (!mem) return NULL; struct binder* b =…
bodacydo
  • 75,521
  • 93
  • 229
  • 319
17
votes
11 answers

Why is free() not allowed in garbage-collected languages?

I was reading the C# entry on Wikipedia, and came across: Managed memory cannot be explicitly freed; instead, it is automatically garbage collected. Why is it that in languages with automatic memory management, manual management isn't even…
Sundar R
  • 13,776
  • 6
  • 49
  • 76
17
votes
6 answers

How do I free a pointer returned from a function?

#include #include #include char* f(void) { char *x; x = malloc(sizeof(char) * 4); strcpy(x, "abc"); return(x); } int main(void) { char *a; a = f(); printf("%s", a); free(a); return(0); } Does the…
user1588871
  • 185
  • 1
  • 1
  • 4
16
votes
9 answers

Why doesn't free(p) set p to NULL?

Any reasons why this can not be standard behavior of free()? multiple pointers pointing to the same object: #include #include void safefree(void*& p) { free(p); p = NULL; } int main() { int *p = (int…
Oleg Razgulyaev
  • 5,757
  • 4
  • 28
  • 28
16
votes
7 answers

Freeing allocated memory: realloc() vs. free()

so I have a piece of memory allocated with malloc() and changed later with realloc(). At some point in my code I want to empty it, by this I mean essentially give it memory of 0. Something which would intuitively be done with realloc(pointer,0). I…
user3021085
  • 407
  • 2
  • 5
  • 16
16
votes
4 answers

Reusing freed pointers in C

There are many questions on this website regarding freeing pointers after use and, further, setting them to NULL. Arguments are fierce and the topic is seemingly divided equally. For example: This question. I am confused about freeing pointers in…
sherrellbc
  • 4,650
  • 9
  • 48
  • 77
16
votes
5 answers

When a program terminates what happens to the memory allocated using malloc that is not free'ed?

Say I have the following program #include #include int main(void) { int * i; if ((i = malloc(sizeof(int) * 100)) == NULL) { printf("EROOR: unable to allocate memory \n"); return -1; } /*…
Sangeeth Saravanaraj
  • 16,027
  • 21
  • 69
  • 98
15
votes
6 answers

Why is my pointer not null after free?

void getFree(void *ptr) { if(ptr != NULL) { free(ptr); ptr = NULL; } return; } int main() { char *a; a=malloc(10); getFree(a); if(a==NULL) printf("it is null"); else printf("not null"); } Why is the output of…
Jeegar Patel
  • 26,264
  • 51
  • 149
  • 222
15
votes
8 answers

Reassigning to a pointer variable after freeing it

Is this legal to do? Can you assign ptr to something after it has been freed? int * ptr = (int*) malloc(sizeof(int)); free(ptr); ptr = (int *) malloc(sizeof(int));
skmiley1
  • 249
  • 2
  • 11
15
votes
3 answers

free() syntax with arguments in C

I am working on converting many C programs from Unix to Linux and this free() syntax caught my attention: free((char *) area ); What's the difference between this and free( area ); ?
user3772839
  • 265
  • 3
  • 11
15
votes
1 answer

Is the compiler allowed to recycle freed pointer variables?

It has been claimed that a compiler is free to reuse the pointer variable for some other purpose after the realloc being freed, so you have no guarantee that it has the same value as it did before ie void *p = malloc(42); uintptr_t address =…
Christoph
  • 164,997
  • 36
  • 182
  • 240
15
votes
4 answers

Free allocated memory before return a function

I am trying to return an array using malloc in a function: char* queueBulkDequeue(queueADT queue, unsigned int size) { unsigned int i; char* pElements=(char*)malloc(size * sizeof(char)); for (i=0; i
KiL
  • 1,500
  • 1
  • 14
  • 16
14
votes
1 answer

Free dependency graph of visual studio C# project

I'm looking to map the C# classes of my Visual Studio 2019 project on a dependency graph. I've seen some tools available but they require you to pay for a full version (ReSharper, NDepend). I'm looking for something free/cheap. If anyone has ideas,…
Anto098
  • 141
  • 1
  • 3
14
votes
9 answers

What is the best way to free memory after returning from an error?

Suppose I have a function that allocates memory for the caller: int func(void **mem1, void **mem2) { *mem1 = malloc(SIZE); if (!*mem1) return 1; *mem2 = malloc(SIZE); if (!*mem2) { /* ... */ return 1; } …
Andrew Keeton
  • 22,195
  • 6
  • 45
  • 72