C programming doubt ?

Hey guys,
If I execute the following code, what will happen ?
int *ptr;
ptr = malloc(100);
ptr++;
free(ptr);


Now ptr points to 2nd integer block.
Now if i use free ptr (as shown in code) what will it do ?
Will it free the whole 100 byte memory or something else ?


Please reply....๐Ÿ˜”

Replies

  • gaurav.bhorkar
    gaurav.bhorkar
    I think it will crash the program because you have created an invalid reference to the block of 100 bytes of allocated memory.

    I'm not sure though. Did the program run?
  • csiscool
    csiscool
    Nice Question..

    this code results in memory leak.
  • Sachin Jain
    Sachin Jain
    @ csiscool
    Can you please explain more ?
  • nansijs
    nansijs
    I think the program cause memory leak. That is the allocated memory cannot be used by another application.
  • pradeep_agrawal
    pradeep_agrawal
    Based on ANSI C specification, if the argument passed to free does not matches with value returned by malloc/calloc/realloc, or if the space has already been freed by calling free or realloc then the behavior is undefined.

    With this i feel the behavior can be different based on the compiler being used. If the code is compiled using gcc then the program will crash at free itself.

    -Pradeep
  • vik001ind
    vik001ind
    int *ptr; // this is a pointer of int type
    ptr = malloc(100);
    /* ERROR - malloc returns pointer of void type (void *), it is incompatible typecasting.
    Correct statement - ptr = (int *)malloc(100); Here we are dynamically allocating an array of 50 integers (considering 16 bit compiler). */
    ptr++; // PTR IS NOW POINTING TO NEXT INT
    free(ptr); // ERROR - deallocating the pointer which contains the wrong address. Allocation of memory is done to different address.

    Program may result in segmentation fault. Memory leak is a different concept altogether.
  • PraveenKumar Purushothaman
    PraveenKumar Purushothaman
    I got System Error and as said here, it caused a memory issue... Since I worked on a virtual machine, it crashed the system too. I used 64 MB of RAM and when I restarted it, it was fine. ๐Ÿ˜ It causes a Segmentation Fault! ๐Ÿ˜€

You are reading an archived discussion.

Related Posts

hey, sorry to ask, but wat is the difference between electrical and electronics?? a basic question though, but most of the people have their different views......... so guys.........
Q 1. A man driving a car then see his spedometer the number is palindrome . the number is 13931 km. after driving for 2hr then he see the another...
hello friends, I have some problem in controlling sound in my music player i used floatcontrol in order to control the volume check this. FloatControl volume = (FloatControl) c.getControl(FloatControl.Type.MASTER_GAIN); volume.setValue(-20.63f);...
CEans ... Dr. Ashok Agarwal graduated from BITS Pilani and later went to do Ph.D from the University of Minnesota. His experience includes working with Bell Telephone Labs and then...
Discuss the official announcement about our exclusive small talk with Mr. Ashok Agrwal, the chairman of engineers without borders: https://www.crazyengineers.com/forum...chairman-engineers-without-borders-india.html in this thread ๐Ÿ˜€