How do I check memory size after malloc and realloc?

This is a discussion on How do I check memory size after malloc and realloc? within the C Programming forums, part of the General Programming Boards category; I wonder if there is a way to check how big the memory area is after malloc and realloc? This ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    63

    Talking How do I check memory size after malloc and realloc?

    I wonder if there is a way to check how big the memory area is after malloc and realloc?

    This is just to see if realloc really works as expected.
    You cantīt teach an old dog new tricks.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    In short no. If realloc() fails it will return NULL, else it returns a pointer to the memory, size of whatever you asked for.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    63
    Originally posted by Hammer
    In short no. If realloc() fails it will return NULL, else it returns a pointer to the memory, size of whatever you asked for.
    Ok, thanks.
    You cantīt teach an old dog new tricks.

  4. #4
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>I wonder if there is a way to check how big the memory area is after malloc and realloc?
    Save the size you pass to malloc() or realloc() in a variable so you can use it later :-)

    >>This is just to see if realloc really works as expected.
    They return a null pointer if they fail
    Code:
    if ((p = malloc(mem_size * sizeof(*p))) == 0)
    {
      /* Error */
    }
    *Cela*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Malloc for extending the size....!
    By patil.vishwa in forum C Programming
    Replies: 5
    Last Post: 09-11-2008, 03:34 AM
  2. malloc and realloc
    By jayfriend in forum C Programming
    Replies: 4
    Last Post: 01-05-2007, 01:25 PM
  3. malloc, realloc
    By figo2476 in forum C Programming
    Replies: 3
    Last Post: 04-28-2006, 10:11 PM
  4. malloc and realloc
    By odysseus.lost in forum C Programming
    Replies: 3
    Last Post: 05-27-2005, 08:44 AM
  5. confused about arrays
    By sal817 in forum C Programming
    Replies: 17
    Last Post: 09-20-2004, 03:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21