top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration

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

+2 votes
63 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 Google+ Share Button LinkedIn Share Button Multiple Social 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ć
Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...