Doubt on memmory deallocation

Discussion in 'C Programming' started by vaysagekv, Sep 9, 2012.

  1. vaysagekv

    vaysagekv Guest

    Hi,
    I am allocating some memory like below:
    char* result= malloc(len);

    then I assign another variable like below
    char* temp = result+2;

    If i deallocate temp
    free(temp);
    Does it deallocate whole space pointed by result.

    Thanks
    vaysage
     
    vaysagekv, Sep 9, 2012
    #1
    1. Advertising

  2. vaysagekv

    Ben Pfaff Guest

    vaysagekv <> writes:

    > I am allocating some memory like below:
    > char* result= malloc(len);
    >
    > then I assign another variable like below
    > char* temp = result+2;
    >
    > If i deallocate temp
    > free(temp);
    > Does it deallocate whole space pointed by result.


    No. It yields undefined behavior. Only addresses returned my
    malloc() (and a few other allocation functions) may be passed to
    free().
     
    Ben Pfaff, Sep 9, 2012
    #2
    1. Advertising

  3. vaysagekv

    vaysagekv Guest

    On 09/09/12 7:37 PM, Ben Pfaff wrote:
    > vaysagekv<> writes:
    >
    >> I am allocating some memory like below:
    >> char* result= malloc(len);
    >>
    >> then I assign another variable like below
    >> char* temp = result+2;
    >>
    >> If i deallocate temp
    >> free(temp);
    >> Does it deallocate whole space pointed by result.

    >
    > No. It yields undefined behavior. Only addresses returned my
    > malloc() (and a few other allocation functions) may be passed to
    > free().

    Thank a lot.
     
    vaysagekv, Sep 9, 2012
    #3
  4. vaysagekv

    vaysagekv Guest

    On 09/09/12 7:36 PM, pete wrote:
    > vaysagekv wrote:
    >>
    >> Hi,
    >> I am allocating some memory like below:
    >> char* result= malloc(len);
    >>
    >> then I assign another variable like below
    >> char* temp = result+2;
    >>
    >> If i deallocate temp
    >> free(temp);
    >> Does it deallocate whole space pointed by result.

    >
    > No.
    >
    > It will probably crash.
    >
    > (result+2) is not a value which was returned by malloc.
    >
    >

    Thank you
     
    vaysagekv, Sep 9, 2012
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. qazmlp
    Replies:
    1
    Views:
    773
    John Harrison
    Mar 7, 2004
  2. Replies:
    2
    Views:
    758
    Paavo Helde
    Dec 18, 2004
  3. AnToine van Maarle

    Windows memmory "read" error

    AnToine van Maarle, May 6, 2004, in forum: Python
    Replies:
    0
    Views:
    343
    AnToine van Maarle
    May 6, 2004
  4. mohi

    memmory corruption

    mohi, Jul 25, 2008, in forum: C++
    Replies:
    1
    Views:
    323
    James Kanze
    Jul 25, 2008
  5. Sachin Midha
    Replies:
    7
    Views:
    403
    Default User
    Sep 13, 2010
Loading...

Share This Page