Thread: questions about realloc()

  1. #1
    Run! Forrest Gump! TalosChen's Avatar
    Join Date
    May 2006
    Location
    Shanghai
    Posts
    27

    questions about realloc()

    Code:
    char *ptr1, *ptr2;
    ptr1 = (char *)malloc(m);
    ptr2 = ptr1;
    ptr2 = (char *)realloc(ptr1, n); // n > m
    ptr2 could be different from ptr1 after realloc(), in that case, should I free(ptr1)?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yes. No. Read the FAQ on why you shouldn't typecast functions that return void pointers.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Run! Forrest Gump! TalosChen's Avatar
    Join Date
    May 2006
    Location
    Shanghai
    Posts
    27
    -> Yes. No.

    Is Yes said that ptr2 != ptr1 maybe true, and No said that I need not free(ptr1) if it's no longer used?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    > ptr2 could be different from ptr1 after realloc(), in that case, should I free(ptr1)?
    Only if ptr2 is NULL.

    If ptr2 isn't NULL, ptr1 has already been freed and the memory copied to where ptr2 now points.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Run! Forrest Gump! TalosChen's Avatar
    Join Date
    May 2006
    Location
    Shanghai
    Posts
    27
    thanks.

    BTW, is there any function can figure out that if a pointer is allocated any memory?

  6. #6
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    Do you mean to check if allocation was successfull? the malloc
    set of functions return NULL if they fail, if its not equal to NULL,
    you can take it that allocation was successfull and the pointer
    points to some form of memory.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  7. #7
    Run! Forrest Gump! TalosChen's Avatar
    Join Date
    May 2006
    Location
    Shanghai
    Posts
    27
    No, what I mean is, giving you a pointer ptr, then can you know that if the memory pointed by ptr was once allocated by using malloc(), realloc(), etc. ? For example,
    Code:
    char *ptr1 = &'a';
    char *ptr2 = malloc(20);
    My question is, is there any function can tell me that ptr1 just points to a single char 'a', while ptr2 points to a block of memory?

  8. #8
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    Well i asked google and came up with this. Naturally i can't stand
    over this because it's not my code/idea but it might help. Maybe
    one of the more experienced members here can shed more light

    [edit]
    The MSDN link at the bottom of that page recommends a function
    in malloc.h - this is outdated by about 20 years so there's
    probably a more modern way.
    [/edit]
    Last edited by Richie T; 05-10-2006 at 07:11 AM.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    > then can you know that if the memory pointed by ptr was once allocated by using malloc(), realloc(), etc. ?
    No, there is no portable way to know this.
    Nor is there any portable way to know how much was allocated simply by looking at the pointer.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Run! Forrest Gump! TalosChen's Avatar
    Join Date
    May 2006
    Location
    Shanghai
    Posts
    27
    Thanks any way.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by TalosChen
    No, what I mean is, giving you a pointer ptr, then can you know that if the memory pointed by ptr was once allocated by using malloc(), realloc(), etc. ? For example,
    Code:
    char *ptr1 = &'a';
    char *ptr2 = malloc(20);
    My question is, is there any function can tell me that ptr1 just points to a single char 'a', while ptr2 points to a block of memory?
    Your code won't compile. You cannot get the address of a character constant. It would be like getting the address of a number. You just can't do it.


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Run! Forrest Gump! TalosChen's Avatar
    Join Date
    May 2006
    Location
    Shanghai
    Posts
    27
    :-) I know that. It's just an example to explain my question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. did i understood right this explantion of realloc..
    By transgalactic2 in forum C Programming
    Replies: 3
    Last Post: 10-24-2008, 07:26 AM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  3. using realloc
    By bobthebullet990 in forum C Programming
    Replies: 14
    Last Post: 12-06-2005, 05:00 PM
  4. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 09:44 AM
  5. realloc realloc realloc
    By Linette in forum C++ Programming
    Replies: 4
    Last Post: 01-19-2002, 09:18 PM