0

I have been scratching my head regarding the memory 'behavior' with arrays decayed as pointers.

I have a function where an array of structures is created in a function (without explicit memory allocation) and then passed to another function as a pointer. Said function store the pointer in a static variable.

Reading this kind of code, I would say the pointer should be invalidated at the end of the first function (since no malloc was done) yet it is not. However, calling a free() on this pointer throws a glibc error : invalid pointer. Makes sense, since no malloc was called.

  • Is there some implicit memory allocation being performed because of the decaying array ?
  • Is there a way to properly free the memory after this ? I know the trivial answer would be to allocate the memory myself, I am mainly asking out of curiosity.

Edit : some dummy code as requested :

static structure* s_array = NULL;

void foo()
{
    structure array[5];
    bar(array); // array decaying as a pointer
}

void bar(structure* ptr)
{
    s_array = array; // pointer stored in the static, not invalidated at the end of foo()
}

void freeBar()
{
    free(s_array); // invalid pointer
}
  • 1
    Please post relevant code. "Without explicit memory allocation" is not a useful way to describe a C implementation. – Kerrek SB Aug 04 '14 at 18:57
  • 4
    Sounds like the array was automatic storage duration? If so, yes, the pointer becomes invalid. Accessing it after the array's lifetime ends is undefined behavior. – T.C. Aug 04 '14 at 18:59
  • Check this. http://stackoverflow.com/questions/408670/stack-static-and-heap-in-c. The array has to be somwhere... – Jakub Aug 04 '14 at 19:01
  • http://stackoverflow.com/questions/6441218/can-a-local-variables-memory-be-accessed-outside-its-scope/6445794#6445794 has a good explanation of what is happening when you try to access a local variable outside its scope. – T.C. Aug 04 '14 at 19:01
  • Would love to get the reason of the downvote. – Monsieur Grumme Aug 04 '14 at 19:03
  • 1
    Most likely the downvote was because you hadn't posted any code. – Jim Balter Aug 04 '14 at 19:06
  • I didn't feel like this question would require code snippet to be understood. But thanks for the suggestion ! – Monsieur Grumme Aug 04 '14 at 19:08
  • Me neither ... since I answered it before you posted the code. Have you read my answer? – Jim Balter Aug 04 '14 at 19:10
  • @Igor it's not static ... neither in the description nor in the code subsequently posted that matches the description. It's local, and freeing that is just as "serious". – Jim Balter Aug 04 '14 at 19:12
  • @Jim I did, thanks. Had to wait 5 minutes to accept it.. :) – Monsieur Grumme Aug 04 '14 at 19:13

1 Answers1

1

I would say the pointer should be invalidated at the end of the first function (since no malloc was done) yet it is not.

Yes, it is, but "invalidation" in C just means that subsequent behavior is undefined ... anything can happen, including your program appearing to work perfectly.

However, calling a free() on this pointer throws a glibc error : invalid pointer. Makes sense, since no malloc was called.

Indeed it makes sense since malloc wasn't called. You can only free memory that was malloced; freeing memory that isn't is undefined behavior, and in this case the library was kind enough to produce a diagnostic.

Is there some implicit memory allocation being performed because of the decaying array ?

There's no such thing as a "decaying array". Arrays aren't first class objects in C so they cannot be passed to functions ... only their addresses can. The "decay" simply consists of the name of the array being converted to a pointer to the first element of the array when used in expression context ... it's a compile-time thing.

Is there a way to properly free the memory after this ?

The memory was "freed" at the end of the first function; nothing else need be done ... other than not storing its address.

Jim Balter
  • 16,163
  • 3
  • 43
  • 66
  • Thank you. T.C. link above is also a good way to explain what is going on : http://stackoverflow.com/questions/6441218/can-a-local-variables-memory-be-accessed-outside-its-scope/6445794#6445794 – Monsieur Grumme Aug 04 '14 at 19:12