10

I am new in C programming language so can you tell me if this is correct way to do.

for example: program points on buffer and i use that pointer as parameter in free() function. So, what problems can this function cause ?

Sagar Naliyapara
  • 3,971
  • 5
  • 41
  • 61
David Demetradze
  • 1,361
  • 2
  • 12
  • 18
  • 4
    If you give a pointer to `free` that was not a pointer returned by `malloc`/`calloc`/`realloc`, Then the code exhibits Undefined Behavior – Spikatrix Apr 04 '15 at 10:03

4 Answers4

24

You should call free only on pointers which have been assigned memory returned by malloc, calloc, or realloc.

char* ptr = malloc(10); 

// use memory pointed by ptr 
// e.g., strcpy(ptr,"hello");

free(ptr); // free memory pointed by ptr when you don't need it anymore

Things to keep in mind:

  • Never free memory twice. This can happen for example if you call free on ptr twice and value of ptr wasn't changed since first call to free. Or you have two (or more) different pointers pointing to same memory: if you call free on one, you are not allowed to call free on other pointers now too.

  • When you free a pointer you are not even allowed to read its value; e.g., if (ptr) not allowed after freeing unless you initialize ptr to a new value

  • You should not dereference freed pointer

  • Passing null pointer to free is fine, no operation is performed.

Giorgi Moniava
  • 27,046
  • 9
  • 53
  • 90
2

Think that the computer has a whole bunch of memory not (yet) used by your program. Now you need some more memory and you ask your computer to give you some more (for example, a large buffer). Once you are done with it, you want to return it to the computer.

This memory is called the heap. You ask for memory by calling malloc() and you return it by calling free();

char *buffer;
buffer = malloc(512);           // ask for 512 bytes of memory
if (buffer==NULL) return -1;   // if no more memory available
...
free(buffer);                 // return the memory again
user5305519
  • 3,008
  • 4
  • 26
  • 44
Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
2

free() function is used to deallocate memory used by one program and move it back to available memory area so that other operating system processes can use that memory location. Also free function takes any type of pointer that points to that memory location. For example:

int a = 10;  // suppose 2 byte is allocated ie location 1000 and 1001

Now this 2 byte of memory belongs to specific problem; hence OS will not give this memory location to another process (memory is now allocated memory not available memory)

 int *ptr =&a;
 /*ptr is pointer variable pointing to 1000  
 as it is int pointer therefore ptr++ will move pointer to 1002*/

Now if we do free(ptr), it will check the pointer type and depending on type free function deallocate memory in this case 2 bytes starting from 1000.

Now interesting point is your data will be there until OS allocates this memory to some other process and that process overwrites it.

Also ptr is pointing to 1000 even after free() function but that memory location does not belong to our program hence ptr pointer has given new name DANGLING POINTER.

*ptr may or may not give the same value therefore it is better to make ptr =null.

Chris Tang
  • 567
  • 7
  • 18
-1

From the man page of free() function:

The free() function frees the memory space pointed to by a pointer ptr which must have been returned by a pre‐ vious 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.

You have to use the free() function when you are allocating the memory dynamically.

If you are using that as a static variable then it may lead to unintended behavior.


 char *c=malloc(100);//allocating the 100 bytes of memory for a pointer variable c.

Here after usage of that varaible you can free that allocated memory,

 free(c);

If you are declared a variable like this,

char c= malloc(100);// It is illegeal. And c will have a memory in stack.

If you free this variable,

free(c);//it will lead to system crash. Because your freeing the memory which is in  stack memory.
Gerhard
  • 6,850
  • 8
  • 51
  • 81
Karthikeyan.R.S
  • 3,991
  • 1
  • 19
  • 31
  • can it lead to spatial memory safety or temporal memory safety errors ? – David Demetradze Apr 04 '15 at 09:56
  • ... What? `char c = malloc(100)` allocate 100 bytes on the **heap**, convert the pointer to a `char`, and then store it to `c`. Call to `free(c)` will convert `c` (a char) to a pointer, and then try to free it, which will lead to system crash because (most of the time) converting a pointer to a `char` and back will change its value and make it an invalid pointer. – user202729 Jan 17 '18 at 04:12