Like Tree3Likes
  • 1 Post By Salem
  • 1 Post By whiteflags
  • 1 Post By anduril462

Flexible Array Size / Realloc

This is a discussion on Flexible Array Size / Realloc within the C Programming forums, part of the General Programming Boards category; Hi all-- I'm writing a program that initializes an integer double pointer to NULL, and realloc's it's size each time ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    22

    Flexible Array Size / Realloc

    Hi all--

    I'm writing a program that initializes an integer double pointer to NULL, and realloc's it's size each time a particular method is called. Something like:

    Code:
    int** list = NULL;
    
    void someMethod() {
    
    int* listElement = malloc(sizeof(int)*3);
    list = realloc(list, sizeof(list)+sizeof(listElement));
    }
    I'm not sure if this is the correct way of going about this, but you get the idea of what I'm trying to do.

    Unfortunately, to test this-- I try printing the sizeof(list) before and after I realloc, and to my dismay-- the size is always 8.

    1) Why is sizeof(list) always 8, even if I malloc it instead of initializing it to NULL?
    2) How do I go about my intended program implementation?

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Sizeof( list ) is always 8 because you're asking it for the size of your pointer.

    C does not let you discover the size of a memory patch after the fact. The only confirmation you get is to check the returned pointer is not null... if not, then you assume it worked for the size you specified.

    For keeping track of the size... use a variable...

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,447
    sizeof() on a pointer tells you the size of the pointer, NOT how much memory it points to.

    It's up to you to keep track of the size.

    Typically, something like
    Code:
    void *temp = realloc( pointer, (currentSize + extraNeeded)*sizeof(*pointer) );
    if ( temp != NULL ) {
      pointer = temp;
      currentSize += extraNeeded;
    } else {
      // possibly an error, but you STILL have pointer as a valid allocation.
    }
    rags_to_riches likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,627
    1) Why is sizeof(list) always 8, even if I malloc it instead of initializing it to NULL?
    sizeof is an operator that tells you the size of a type, and the type of list is int**, therefore, the result is the size of a pointer, int**. Likewise, sizeof(listElement) would result in the size of an int*.

    2) How do I go about my intended program implementation?
    I'm not sure how else to put it, but you need to make a proper calculation to get what you ask for.

    Since you're implementing a 2-d array, I recommend reading this. Additionally, please store realloc's returned pointer in a temporary variable. That way if realloc fails, you can free the original memory instead of leaking it.
    rags_to_riches likes this.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,444
    1) Question 7.28
    2) You need to decide if you really want to work with a double pointer or just a single pointer. I'm not totally clear what you are trying to do with your code there. Do you want a 2-d array? Are you just trying to grow it by 3 elements? You will have to keep track of how many elements list is pointing to in some other variable. Also, you need to be careful with realloc. You always want to use a temp variable to hold the value of realloc and check it for NULL before assigning it to list. If realloc fails and returns NULL, you set list to NULL, lose the original value of list and cant free it or access the data in there anymore.
    rags_to_riches likes this.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    22
    Thanks everyone :]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. get size allocated memory for ARRAY structs
    By grytskiv in forum C Programming
    Replies: 1
    Last Post: 03-13-2011, 03:55 AM
  2. Replies: 16
    Last Post: 03-02-2011, 06:35 PM
  3. How do you use variable to initialize array size?
    By yougene in forum C Programming
    Replies: 11
    Last Post: 09-04-2007, 02:50 PM
  4. Array size
    By tigrfire in forum C Programming
    Replies: 5
    Last Post: 11-14-2005, 07:45 PM
  5. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 12:01 AM

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