-1

I have allocated memory to some data type and assigned some value. Now using free is the data in the memory deleted or not? What is use of using free if the data assigned is not deleted? Can anyone help me out? Ex:

int *arr;
arr=(int*)malloc(sizeof(int)*1000);
assert(arr!=NULL);
/*Some operation*/

arr[123]=354;
//some operations

printf("%d",*(arr+123));
//calling some funcs

free(arr);

printf("\n%d",*(arr+123));
Shahbaz
  • 46,337
  • 19
  • 116
  • 182
  • 3
    What is the connection between `arr` and `arr2`? This is very confused. – Kerrek SB Sep 17 '12 at 14:19
  • 1
    What you are doing is undefined behavior. If it is printing a reasonable value, then you should consider that a coincidence. – Vaughn Cato Sep 17 '12 at 14:19
  • See this answer for more information: http://stackoverflow.com/questions/1119134/how-do-malloc-and-free-work?lq=1 – Vaughn Cato Sep 17 '12 at 14:22
  • Side note: With `arr=malloc(1000*sizeof(*arr))` you don't have to repeat the type the pointer points to. This will be much more maintainable in the future. – Shahbaz Sep 17 '12 at 14:39
  • possible duplicate of [How to know if C function free is working?](http://stackoverflow.com/questions/5334132/how-to-know-if-c-function-free-is-working) – Bo Persson Sep 17 '12 at 15:02

3 Answers3

5

The point of free is to make the memory you allocated available for following calls to malloc. It does not guarantee that the buffer passed to it is wiped in any way.

In fact, what you're doing is provoking undefined behavior; accessing a buffer that has been free'd might give the value that was previously stored in it, or any other value, or it might crash your program, or do anything else.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
2

You cannot "delete" things from memory.

What free() does is it reclaims the memory, so that it can be recycled by a future call to malloc().

You cannot legally dereference a pointer that you got from malloc() (or any other dynamic allocation call) after calling free() on it. Doing so invokes undefined behavior.

unwind
  • 391,730
  • 64
  • 469
  • 606
0

By using malloc you set aside a specified number of bytes to be used for what ever you want. Returned is an address to the area. The memory area is also flagged in a special table as occupied meaning it can't be reserved by another process or another malloc.

When you free the allocated memory the area is flagged as free, but the data is not erased. If you want to erase the data you'll have to do a memset or manually loop and set data to 0 or some other value before you free it.

As mentioned by others the operation on free memory is undefined.

A short introduction to how memory can be arranged. Look i.e. at heap: http://www.geeksforgeeks.org/archives/14268 or Fig 1. at bottom of this comment: Strange global variable behaviour, once variable name is changed issue disappears

I have written some more about the malloc process itself here: Can some explain the performance behavior of the following memory allocating C program? where also virtual vs. physical memory, pages etc are some of the points.

Community
  • 1
  • 1
Morpfh
  • 4,033
  • 18
  • 26