-2

Will free() work correctly if I change pointer the following way?

ar_byte_100=calloc(100,1);
ar_byte_100=&((unsigned long int*)ar_byte_100)[50];
free(ar_byte_100);
ZygD
  • 22,092
  • 39
  • 79
  • 102

2 Answers2

2

No.

You must pass the exact pointer returned by malloc() or calloc() to free().

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
2

According to the standard free(ptr)

ptr should be a pointer returned by malloc() calloc() and realloc() anything other than that is a undefined behavior.

Gopi
  • 19,784
  • 4
  • 24
  • 36
  • And can I `realloc(ar_byte_100, 0); ar_byte_100=&((unsigned long int*)ar_byte_100)[-50];realloc(ar_byte_100, 0);`? – user4753163 Apr 08 '15 at 11:59