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

freeing memory of a binary tree C

I would like to free memory from my allocated binary tree what traversal is the best for doing so? typedef struct Node{ struct Node * right; struct Node * left; void * data; }Node; typedef int (*cmp) (void*,void *); Node* init(void * element){ …
mary
  • 869
  • 5
  • 13
  • 26
12
votes
7 answers

How does C free() work?

Possible Duplicate: How malloc() and free() work #include #include int * alloc() { int *p = (int *)calloc(5,4); printf("%d\n",p); return p; } int main() { int *p = alloc(); free(p); printf("%d\n",p); p[0] =…
slee
  • 493
  • 1
  • 5
  • 17
12
votes
5 answers

free() on stack memory

I'm supporting some c code on Solaris, and I've seen something weird at least I think it is: char new_login[64]; ... strcpy(new_login, (char *)login); ... free(new_login); My understanding is that since the variable is a local array the memory…
vidicon
  • 123
  • 1
  • 1
  • 4
12
votes
3 answers

Why FreeAndNil implementation doing Nil before Free?

If you will look at the code of FreeAndNil procedure you will see: procedure FreeAndNil(var Obj); var Temp: TObject; begin Temp := TObject(Obj); Pointer(Obj) := nil; Temp.Free; end; What is the reason they assigning Nil to an object…
Andrew
  • 3,696
  • 3
  • 40
  • 71
12
votes
4 answers

does exit() free allocated memory on both _SUCCESS and _FAILURE

This a short snippet of code, with two calls to exit(3) in case of failure. Do these calls deallocate memory allocated by malloc? Google search once says it does, and even more times, it doesn't... Should I add free()? Also: which is better if…
Peter Cerba
  • 806
  • 4
  • 14
  • 26
11
votes
2 answers

which free tools can I use to generate the program dependence graph for c codes

I want to generate a Program Dependence Graph (PDG) from C source code. I found papers that explain how do it, but all used the commercial CodeSurfer tool. Are there any free tools or open source projects that can do this job?
user1283336
  • 297
  • 3
  • 8
11
votes
4 answers

How to free a struct that contains only pointers

I have a struct which you see below: typedef struct _List { Person *person; // pointer for people list DoList *do; // Kinda timer, for checking list in some intervals } List; Are there any need to free this struct? If so, how can i free it?
wonnie
  • 459
  • 3
  • 6
  • 19
11
votes
1 answer

Freeing memory wrapped with NewDirectByteBuffer

I have a region of memory wrapped with JNI NewDirectByteBuffer. I would like to run free/release code in the cleaner of the ByteBuffer. Is there a way to do this or do I have to offer a custom free method that the user will have to call with the…
Philippe Marschall
  • 4,452
  • 1
  • 34
  • 52
11
votes
2 answers

Is it vali to call free with a pointer to the first member?

Is it okay to call free on a pointer which is pointing at the first member of a struct (and the struct is the one involved with malloc)? I know in principle the pointer is pointing at the right thing anyway... struct s {int x;}; //in main struct s*…
user3614107
11
votes
1 answer

Cython: Memory view of freed memory

In Cython code, I can allocate some memory and wrap it in a memory view, e.g. like this: cdef double* ptr cdef double[::1] view ptr = PyMem_Malloc(N*sizeof('double')) view = ptr If I now free the memory using PyMem_Free(ptr),…
jmd_dk
  • 12,125
  • 9
  • 63
  • 94
11
votes
5 answers

How to "free" variable after end of the function?

what is the right way to free an allocated memory after executing function in C (via malloc)? I need to alloc memory, use it somehow and return it back, than I have to free it. char* someFunction(char* a, char* b) { char* result =…
Jax-p
  • 7,225
  • 4
  • 28
  • 58
11
votes
5 answers

Taming the malloc/free beast -- tips & tricks

I've been using C on some projects for a master's degree but have never built production software with it. (.NET & Javascript are my bread and butter.) Obviously, the need to free() memory that you malloc() is critical in C. This is fine, well and…
roufamatic
  • 18,187
  • 7
  • 57
  • 86
11
votes
7 answers

Segmentation fault after free(), what are the common causes for this?

I get a segmentation fault after freeing a certain pointer: free(studentDB->name); I can get its value without any errors or warnings: printf("[DBG] studentDB->name: %s\n", studentDB->name); However, as I said, the program crashes when I try to…
Pieter
  • 31,619
  • 76
  • 167
  • 242
10
votes
5 answers

Printing pointer addresses in C [two questions]

I know that my questions are very simple but googleing them didn't get me any useful results... They'r probably too simple!! No. 1 char* createStr(){ char* str1 = malloc(10 * sizeof(char)); printf("str1 address in memory : %p\n", &str1); …
Stamoulohta
  • 649
  • 1
  • 7
  • 21
10
votes
7 answers

When should I use free() in C?

The code works as it is supposed to, though it never frees the memory allocated by malloc(). I have tried to free memory in any place that I can, but no matter where I do it, it breaks the program. Specifically, I get a "double free or corruption…
Koffeeaddict4eva
  • 152
  • 1
  • 2
  • 10