Thread: Quick Realloc question

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    11

    Quick Realloc question

    Code:
    strucType makeBigger(structType  x, char ch)
    { /* Struct x was previous malloc to 8 bits*/ 
       x = realloc(x, 100); 
       printf("%d", sizeof(x));
    }
    So this should print out 100, but it keeps printing out 8.. I know its something to do with pointers but I have nooo idea what. Have added *'s and &'s infront of the x's and nothing is working. Any help would be really appreciated :-)

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    It prints 8 because you are asking for the size of the pointer (x).

    You can't actually print out the size of a block of allocated memory. You have to track that for yourself.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    11
    Oh!!! Ok, that makes sense... Thanks so much!

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Sonny Trujillo View Post
    Code:
    strucType makeBigger(structType  x, char ch)
    { /* Struct x was previous malloc to 8 bits*/ 
       x = realloc(x, 100); 
       printf("%d", sizeof(x));
    }
    What happens to x when realloc fails?


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

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Which is precisely why I recommend that people do not use realloc.
    Virtually all of those who start threads on here and have code that includes realloc are using it wrong.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about realloc
    By donthate in forum C Programming
    Replies: 2
    Last Post: 10-14-2010, 07:20 PM
  2. Realloc Quick Question.
    By killpoppop in forum C Programming
    Replies: 36
    Last Post: 01-13-2009, 01:04 PM
  3. Realloc Question
    By cstudent in forum C Programming
    Replies: 3
    Last Post: 05-17-2008, 01:32 AM
  4. question about realloc
    By thesand in forum C Programming
    Replies: 5
    Last Post: 10-29-2006, 01:45 AM
  5. Quick realloc question
    By Matt13 in forum C Programming
    Replies: 7
    Last Post: 04-02-2004, 08:40 AM