Thread: 2-Dimensional character strings and realloc()

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    21

    2-Dimensional character strings and realloc()

    Hello, the following code fails/crashes in the second loop where it prints the content and frees the memory. But the issue could be with the first loop. Any help is appreciated in advance.

    Code:
    int main(int argc, char *argv[])
    {
        char **a, buf[80];
        int x;
    
        a = (char **)realloc(NULL, sizeof(char *));
        for(x = 0; x < 5; x++) 
        {
            gets(buf);
            a[x] = (char *)realloc(a, strlen(buf) * sizeof(char) + 1);
        }
        printf("--------------\n");
        for(x = 0; x < 5; x ++) 
        {
            printf("%s\n", a[x]);
            free(a[x]);
        }
        free(a);
        return 0;
    }

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You are only allocating space for a single char* in you first realloc.

    You are reallocating into your "a" variable in the second realloc, which you clearly do not want to do.

    (You should be using malloc since you are not really reallocating anything.)

    You are not copying your buf into the newly allocated space.

    (You should be using fgets instead of gets since fgets is safer.)

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    5
    Assuming you want "a" to be an array of pointers,
    which contains pointers to strings read by gets()...

    In the first loop, there are two problems...
    1) you are using "realloc", and not "malloc" or "calloc"
    for the "strings" read by gets()... (this assumption is based
    upon the fact that you are performing a "free()" in the 2nd loop.
    2) you are never "copying" from "buf" to the newly allocated
    space...

    In other words, off the top of my head, this is what I think you
    are trying to do:

    Code:
        
        for(x = 0; x < 5; x++) 
        {
            gets(buf);
            /*
             * acquire space for string just read
             */
            a[x] = (char *)malloc(strlen(buf) * sizeof(char) + 1);
            strcpy(a[x], buf); 
            /*
             * expand the pointer list
             */
            a     = realloc(a, (x + 2) * (sizeof(char*)));
        }
    HTH,
    -tony

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    21
    Thanks Tony, your suggestion worked.

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,728
    Just for reference :

    Why gets() is bad
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Realloc Strings Array
    By lautarox in forum C Programming
    Replies: 41
    Last Post: 12-12-2008, 12:09 PM
  2. How to put a string in a 2 dimensional character array?
    By atif7865 in forum C Programming
    Replies: 2
    Last Post: 12-05-2008, 10:26 PM
  3. using character arrays and realloc
    By MK27 in forum C Programming
    Replies: 9
    Last Post: 09-05-2008, 08:21 AM
  4. gibrish when copying strings character by character
    By captain-cat in forum C Programming
    Replies: 2
    Last Post: 07-23-2004, 07:48 AM
  5. two dimensional character array
    By feuerraeder in forum C Programming
    Replies: 4
    Last Post: 11-22-2002, 08:59 AM