14

Does the following code free the memory that was allocated for x?

int main()
{
    char *x = (char*)calloc(100, sizeof(char));
    char *y = x;
    free(y);
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
cxphong
  • 847
  • 2
  • 9
  • 18

4 Answers4

19

Yes

When you do

char *y = x;

you make y point to the location where x points to. Since y points to a memory location returned by calloc,

free(y);

is perfectly valid. As @haccks commented, this would not work if you make y point to another memory location, provided that this memory location wasn't returned by malloc/calloc/realloc.


In C, you should not cast the result of malloc/calloc/realloc. Also, checking the return value of calloc to see if it was successful is good. calloc will return NULL on failure.

Community
  • 1
  • 1
Spikatrix
  • 20,225
  • 7
  • 37
  • 83
  • It would be nice if you add that this not work if `y` is reassigned to another memory location. – haccks May 01 '15 at 15:51
  • Technically, `free(NULL)` is legal and a no-op, so in this extremely simplistic example, OP's code is perfectly fine not checking the return value. – Kevin May 01 '15 at 19:11
  • @Kevin , I know. That's why I never said "you should". – Spikatrix May 02 '15 at 05:34
  • So if I do ```y++``` before ```free(y)```, the memory space which x pointed to will not be freed, right? – Alec.Zhou Mar 06 '23 at 12:23
  • 1
    @Alec.Zhou IIRC that will invoke undefined behavior. You should only `free` whatever was returned by `malloc`/`calloc`/`realloc` – Spikatrix Mar 06 '23 at 13:05
5

The original version of the question had

int *y = x;
free(y);

I.e. assigning the char pointer to an int pointer and then invoke free() on the int pointer. The signature of free() is void free(void *ptr); so irrespective of char * vs. int *, memory would be released.

The edited (and current) version of the question has

char *y = x;
free(y);

Here, both y and x points to the same memory. This is known as pointer aliasing. Upon the call to free(), that memory would certainly be released.

A problem, however, is that after free(y), the pointer x would be dangling, i.e. the memory it is pointing to is no longer valid.

Note, it is neither necessary nor recommended to cast the return value from calloc.

Arun
  • 19,750
  • 10
  • 51
  • 60
  • `char * x = ...; int * y = x;` provokes undefined behaviour, due to assigning to an incompatible pointer type. Afterwards anything can happen. So the correct answer would be "*May be.*". – alk May 01 '15 at 11:05
  • 2
    Assigning "to an incompatible pointer type" does not - in itself - cause undefined behaviour. Undefined behaviour results if that pointer of incompatible type is dereferenced. The code in the original version of the question did not do that, so did not exhibit undefined behaviour. – Peter May 01 '15 at 11:55
1

Yes, it does.

The method is void free(void *ptr), then when you make char* y = x, you have a pointer to the same space memory that y was pointing.

That let you free that space later with free (y).

However, you would put x = NULL because now x doesn't have a own space despite it have a value.

For further information about free read here.

Shondeslitch
  • 1,049
  • 1
  • 13
  • 26
0

The memory returned by your call of calloc() is released by the call of free().

In your code that affects both x and y.

Peter
  • 35,646
  • 4
  • 32
  • 74