Thread: realloc fails

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    30

    realloc fails

    My first attempt at realloc isn't yielding the results I'd hoped for. Can anybody point me in the right direction? (linux gcc 4.1.2)

    Code:
    int main(int argc, char *argv[])
    {
      char *smallArray[300];
      char *tmpArray;
      
    
      if ((tmpArray = realloc (smallArray, sizeof (smallArray) + 10 )) == NULL)
      {
                         printf ("realloc failed\n");
      }
      system("PAUSE");	
      return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You don't have a pointer, you have an array. "smallArray" is an array of pointers, so you can't reallocate it. Just like you can't reallocate any other array. You can use realloc on one of the pointers in the array, but not on the whole array.


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

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    30
    Well that sucks. So how do I get a dynamic array that I can resize as needed?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Search the forums for something like "multidimensional arrays" or "dynamic arrays". Basically you need a pointer to a pointer. You first allocate the number of actual pointers you want. Then you allocate for each one. I just did a post a few days ago on it. Much less colorful, sad to say.


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

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by beginner.c View Post
    Well that sucks. So how do I get a dynamic array that I can resize as needed?
    First malloc() a basic amount for a pointer. Now treat that pointer as if it is your array. There you go. You can realloc() it.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You don't need to call malloc first, you can do everything with realloc.

    char *array = NULL;

    char *temp = realloc( array, 10 * sizeof *array ); // this is identical to malloc
    if ( temp != NULL ) array = temp;

    Then later on, expand it with
    temp = realloc( array, 20 * sizeof *array );
    if ( temp != NULL ) array = temp;


    That means you can just have a loop which periodically calls realloc inside the loop, starting with a NULL pointer, rather than having to prepend an initial malloc just to get things started.
    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.

  7. #7
    Registered User
    Join Date
    Apr 2007
    Posts
    30
    Thanks, but is that increasing the number of elements of the array? That's what I need to do.

  8. #8
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    but is that increasing the number of elements of the array? That's what I need to do.
    Yes it does

    Code:
    char *temp = realloc( array, 10 * sizeof *array );
    This one allocated 10 bytes and makes it as string. which is something similar to array[10]

    this one expands that a bit more to

    Code:
    temp = realloc( array, 20 * sizeof *array );
    And for the same array to allocated 20 more bytes to make array[30]. Thats is how it works. relloc stores the original pointer in a locatoin that was 10bytes and then allocated 20bytes and returns a nw pointer to the new memory location. This is how it works

    ssharish2005

  9. #9
    Registered User
    Join Date
    Apr 2007
    Posts
    30
    OK I must be missing something. Are you saying that the resulting array will be the equivalent of
    Code:
    char *smallArray[30];
    As I need each element to be able to store a string rather than a single char.

  10. #10
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    A while ago I posted a pretty large example on how to read a file into an array of strings. The problem is the same and you can modify the code to your needs:

    http://cboard.cprogramming.com/showp...53&postcount=6

  11. #11
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quote Originally Posted by beginner.c View Post
    OK I must be missing something. Are you saying that the resulting array will be the equivalent of
    Code:
    char *smallArray[30];
    As I need each element to be able to store a string rather than a single char.
    It would be just smallArray[30] rather than pointer to a char array

    ssharish2005

  12. #12
    Registered User
    Join Date
    Apr 2007
    Posts
    30
    Thanks a lot, I'll have a look through it.

  13. #13
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by ssharish2005 View Post
    Yes it does

    Code:
    char *temp = realloc( array, 10 * sizeof *array );
    This one allocated 10 bytes and makes it as string. which is something similar to array[10]

    this one expands that a bit more to

    Code:
    temp = realloc( array, 20 * sizeof *array );
    And for the same array to allocated 20 more bytes to make array[30]. Thats is how it works. relloc stores the original pointer in a locatoin that was 10bytes and then allocated 20bytes and returns a nw pointer to the new memory location. This is how it works

    ssharish2005
    Quote Originally Posted by beginner.c View Post
    OK I must be missing something. Are you saying that the resulting array will be the equivalent of
    Code:
    char *smallArray[30];
    As I need each element to be able to store a string rather than a single char.
    No, no, no!
    Realoc does not add 20 bytes, It changes the allocated size to the new size
    In this case it changes (if successful) the 10-bytes regeon to the 20 bytes regeon and copies the 10 bytes to the new location if needed.

    When the new size is less than the original size - the regeon is shrinked to the new size...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

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. writing a pack-style function, any advices?
    By isaac_s in forum C Programming
    Replies: 10
    Last Post: 07-08-2006, 08:09 PM
  3. using realloc
    By bobthebullet990 in forum C Programming
    Replies: 14
    Last Post: 12-06-2005, 05:00 PM
  4. Realloc inappropriate for aligned blocks - Alternatives?
    By zeckensack in forum C Programming
    Replies: 2
    Last Post: 03-20-2002, 02:10 PM
  5. realloc realloc realloc
    By Linette in forum C++ Programming
    Replies: 4
    Last Post: 01-19-2002, 09:18 PM