top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

C: What will happen if free() use an invalid pointer to release the memory ?

+2 votes
520 views
C: What will happen if free() use an invalid pointer to release the memory ?
posted Jan 18, 2017 by Neelam

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

0 votes

I wrote the code and tested, it is crashing.
Code snapshot:

#include<stdio.h>
#include<stdlib.h>

int main()
{
   int *ptr = malloc(sizeof(int)*5);
   free(ptr+1);
   return 0;
}
answer Jan 19, 2017 by Harshita
–1 vote

int main()
{
int *ptr;
ptr = (int*)malloc(sizeof(int)*5); /* malloc returns void pointer you need to typecast */
free(ptr);
return 0;
}

answer Apr 19, 2017 by Leon Martinović
...