-1

From cplusplus.com:

A block of memory previously allocated by a call to malloc, calloc or realloc is deallocated, making it available again for further allocations.

If ptr does not point to a block of memory allocated with the above functions, it causes undefined behavior.

If ptr is a null pointer, the function does nothing.

Notice that this function does not change the value of ptr itself, hence it still points to the same (now invalid) location.

But what happen if my pointer doesn't point to the start of the block.

For example:

int *ptr = malloc(sizeof(int)*10);

ptr++;

free(ptr);

Does free only free the last 9 positions? This would be horrible in this case, then:

int *ptr = malloc(sizeof(int)*10);
int i;

for(i=0; i<10; i++, ptr++);

free(ptr);

Here ptr points outside of the block of memory reserved by malloc.

And what would be the behaviour of this?

int *ptr1 = malloc(sizeof(int)*10);
int *ptr2 = ptr1 + 2;

free(ptr2);

Thanks.

Community
  • 1
  • 1
Jumer
  • 51
  • 1
  • 7
  • 2
    free has to match malloc exactlly, or UB – user3528438 Feb 17 '16 at 16:20
  • 1
    You can only pass the exact pointer returned by `malloc` or `realloc` or `calloc` to `free`. You can not pass a modified pointer to `free` without encountering undefined behavior. – Tom Karzes Feb 17 '16 at 16:21
  • 1
    The answer is directly in what you pasted: _If ptr does not point to a block of memory allocated with the above functions, it causes undefined behavior._ Pointing somewhere other than the start is the same as not pointing at the block. – mah Feb 17 '16 at 16:22
  • When you say that I have to pass free the exact pointer returned by malloc family you mean the variable itself or the memory direction? Would this be legal? int *ptr1 = malloc(sizeof(int)*10); int *ptr2 = ptr1; free(ptr2); – Jumer Feb 17 '16 at 16:26
  • 1
    @jumer the _value_ that `malloc()` returned is what must be provided to `free()`. How you manage storage of that value is based on your needs but in your simple example, the unmodified variable is sufficient. – mah Feb 17 '16 at 16:27

1 Answers1

1

You will have memory leak (failed free) or broken malloc chain if try to free modified (increased) pointer.

i486
  • 6,491
  • 4
  • 24
  • 41
  • 3
    Or... perhaps your program will crash. You do not know with certainty what will happen, you only know that the behavior is _undefined_. – mah Feb 17 '16 at 16:26
  • Never heard of _failed `free()`_, can you quote something? – Sourav Ghosh Feb 17 '16 at 16:42
  • Moreover, IMHO, _"You will have memory leak"_ is wrong, as that is far from being guaranteed. – Sourav Ghosh Feb 17 '16 at 16:43
  • @mah Program crash will be side effect from the messed memory. – i486 Feb 17 '16 at 21:16
  • @SouravGhosh Memory leak will occur when `free` fails. If the same pointer is assigned (malloc-ed) multiple times without freeing, there is memory leak. – i486 Feb 17 '16 at 21:18
  • 1
    @i486 you don't seem to get it... you are predicting what will happen based on anecdotal evidence. The problem is your prediction is not based on a defined expectation; the only valid expectation is "undefined behavior" which might as well be saying "we don't really know what will happen but it's probably not desirable". This makes your answer a bit less than useful. – mah Feb 17 '16 at 21:24