Thread: Dynamically Allocating a 2D Array.

  1. #16
    Registered User
    Join Date
    Sep 2005
    Posts
    41
    Quote Originally Posted by Salem
    > if i did a realloc would it still trash all my previous strings?
    Your realloc allocates one more char * pointer, not a string.
    All your existing strings (the pointers AND the memory they point to) are preserved. You don't need to do any more work with the strings you already have.

    so is
    Code:
     	another quick question
    
    Code:
    
    for(i = 0; i < num_of_inputs; i++)
             {
                Inputs[i] = malloc(cols * sizeof(char));
             }
    not needed?

  2. #17
    Registered User
    Join Date
    Sep 2005
    Posts
    41
    Code:
    #define _GNU_SOURCE 1
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <getopt.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    
    typedef enum
    {
       FALSE = 0,
       TRUE = 1
    } Bool;
    
    int main()
    {
       ssize_t readReturn = 0;
       size_t bufLen = 0;
       char **Inputs = NULL;
       void *temp;
       char *input = NULL;
       int x = 0;
       Bool stillGoing = TRUE;
       int num_of_inputs = 0;
       int cols = 0;
       int old_cols = 0;
       int i = 0;
    
       while(stillGoing)
       {
         printf("enter text: ");
       
         readReturn = getline(&input, &bufLen, stdin);
       
         for(x = 0; x < strlen(input) + 1; ++x)
         {
            if(input[x] == '\n')
            {
               input[x] = '\0';
            }
         }
         
         if(strncmp(input, "exit", 4) == 0)
         {
             stillGoing = FALSE;
             break;
         }
            
         num_of_inputs++;
         cols = strlen(input) +1;
         
         printf("maybe here (inputs realloc)\n");
         temp = realloc(Inputs, num_of_inputs * sizeof(char *));
         
         if(temp)
         { 
            Inputs = temp;
            Inputs[num_of_inputs - 1] = input;
         }
         else
         {
            /*handle error*/
            printf("error\n");
            exit(EXIT_FAILURE);
         }
         
         
         
         
         /*if(cols > old_cols)
          {
             for(i = 0; i < num_of_inputs; i++)
             {
                Inputs[i] = malloc(cols * sizeof(char));
             }
             old_cols = cols;
          }*/     
    
          printf("maybe this is where it's seg faulted (strcpy)\n");
          /* strcpy(Inputs[num_of_inputs - 1], input);*/
    
           
          free(input);
          input = NULL;
          free(temp);
       }
       
       
       int z = 0;
       for(z=0; i < num_of_inputs-1; z++)
       {
          printf("%s\n", Inputs[z]);
       }
       
       free(Inputs);
       return 0;
    }
    new code but it still prints out junk then seg faults

  3. #18
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I'm just wondering if you actually try anything anyone else posts, or just find new and interesting ways to screw up your own code.

    Here, try this
    Code:
    #define _GNU_SOURCE 1
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <getopt.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    
    typedef enum
    {
      FALSE = 0,
      TRUE = 1
    } Bool;
    
    int main()
    {
      ssize_t readReturn = 0;
      size_t bufLen = 0;
      char **Inputs = NULL;
      void *temp = NULL;
      char *input = NULL;
      Bool stillGoing = TRUE;
      int num_of_inputs = 0;
      int i = 0;
    
      while(stillGoing)
      {
        printf("enter text: ");
        fflush(stdout);
    
        /* read a line, and blow away the newline */
        readReturn = getline(&input, &bufLen, stdin);
        if ( input[ strlen(input) - 1 ] == '\n' ) {
          input[ strlen(input) - 1 ] = '\0';
        }
    
        /* time to quit yet? */
        if(strncmp(input, "exit", 4) == 0)
        {
          stillGoing = FALSE;
          break;
        }
    
        /* save the pointer getline() allocated for us */
        num_of_inputs++;
        temp = realloc(Inputs, num_of_inputs * sizeof(char *));
        if(temp)
        {
          Inputs = temp;
          Inputs[num_of_inputs - 1] = input;
        }
        else
        {
          /*handle error*/
          printf("error\n");
          exit(EXIT_FAILURE);
        }
    
        /* make getline call malloc again for the next line */
        input = NULL;
      }
    
    
      /* print and free each line */
      for(i=0; i < num_of_inputs-1; i++)
      {
        printf("%s\n", Inputs[i]);
        free(Inputs[i]);
      }
    
      /* free all the pointers */
      free(Inputs);
      return 0;
    }
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  2. help allocating 2d array w/ new
    By Mr_Jack in forum C++ Programming
    Replies: 2
    Last Post: 04-12-2004, 08:10 AM
  3. Replies: 6
    Last Post: 10-21-2003, 09:57 PM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. Replies: 4
    Last Post: 09-12-2001, 02:05 PM