1

I am new at programming and i just don't get this. I am supposed to make a function which takes an 1d Array as argument, and frees this Array. I've got this:

void destroy(double A[])
{
    free(A);
}

and my main:

void main()
{ 
   swrmeg = (double *)malloc ((10)*sizeof(double));
   swrmeg[0] = 3,2;
   destroy(swrmeg);
   printf("%lf\n",swrmeg[0]);
}

This is supposed to give a segmentation fault, but it does not, it prints the first double of the array. This means the array has not been freed.. Any ideas why does this happen? Any proper ways to do the freeing in a function?

Natan Streppel
  • 5,759
  • 6
  • 35
  • 43
fets
  • 21
  • 1
  • 4

3 Answers3

2

Segfaults cannot be guaranteed when doing undefined operations, they just sometimes occur when doing undefined operations.

What is actually occurring in your case is that the memory has been assigned to your program in the malloc and then your program has decided it doesn't need it in the free statement; however, the operating system has decided not to move it's memory fences in such a manner to cause a segfault.

Why it doesn't do so includes a lot of reasons:

  1. It could be far more expensive to move the fence rather than just to let your program get away with having a few extra bytes for a little while.
  2. It could be that you'll ask for some memory in a few minutes, and if you do (and it's small enough) then the same memory will be returned, without the need to move memory fences.
  3. It could be that until you hit some hardware dependent limit (like a full page of memory) the OS can't reset the memory fence.
  4. It could be ...

That's the reason why it is undefined, because it is basically dependent on so many things that all the implementations do not need to align. It is the defined part that needs to align.

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
2

You're freeing it correctly.

Doing something wrong, like accessing a piece of memory after it's been freed, doesn't necessarily mean you'll get a segmentation fault, any more than driving on the wrong side of the road means you'll necessarily have an accident.

j_random_hacker
  • 50,331
  • 10
  • 105
  • 169
  • You are right..But how is it possible to print the number AFTER i freed the array? I mean what's the point of freeing if the numbers stored in the Array are still there? – fets May 12 '14 at 18:43
  • 1
    @fets by freeing, you are telling the OS that *it can* (not *must*) put other things in that memory location from now on. If it didn't have anything to put there at the time, it will leave what was in it untouched. Have a read on the related link that I pasted on your question's comment section to see what happens under other circumstances. – Natan Streppel May 12 '14 at 18:46
  • thank you guys, you made it clear to me.. :) – fets May 12 '14 at 18:56
0

It appears you are being asked to investigate undefined behavior (UB). ( This is supposed to give a segmentation fault ) What you are doing is not guaranteed to get a seg fault, but you can increase your chances by writing to places you do not own:

void main()
{ 
   swrmeg = (double *)malloc ((10)*sizeof(double));
   swrmeg[0] = 3,2;
   destroy(swrmeg);
   printf("%lf\n",swrmeg[0]);
}

void destroy(double *A)
{
    int i;
    for(i=0;i<3000;i++)//choose a looping index that will have higher likelyhood of encroaching on illegal memory
    {
        A[i] = i*2.0;  //make assignments to places you have not allocated memory for
    }
     free(A);
}  

Regarding using free'd memory: This post is an excellent description of why free'd memory will sometimes work. (albeit, dealing directly with stack as opposed to heap memory, concepts discussed still illustrative on using free'd memory in general)

Community
  • 1
  • 1
ryyker
  • 22,849
  • 3
  • 43
  • 87