0

In the following code, I ask the user to give me some strings. I make a 2-D pointer char array, so that I read the input with pointer string which points to the start of a string of length 50. My problem is that I keep crashing after the input of the first string.. and I assume that my problem has to do with the realloc. I am not used to it.. can you please help to figure out what is happening?? I tried to debug with netbeans, but didn't manage to see anything interesting, since it doesn't give feedback for the new addresses made from realloc!!

Here is the code:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main()
{
    char *str,**string,buffer[50],temp[2];
    int i,j,q,size,counter;
    size=10;
    string=(char**) calloc(size,sizeof(char*));
    for (i=0; i<size; i++) string[i]=(char*) malloc(50*sizeof(char));
    printf("\nGimme strings, terminate input with x");
    i=0;
    gets(string[i]);
    temp[0]=120;//x
    temp[1]='\0';
    size=0;
    while(strcmp(string[i],temp)!=0)
    {
        string=realloc(string,size*sizeof(char**));
        i++;
        gets(string[i]);
        size++;
        counter++;
    }
return 0;
}

I want to make the table of pointers bigger with this realloc.

alex777
  • 167
  • 5
  • 14
  • `strings[I] = realloc( strings[I], size * sizeof(char) );` – sean Jan 09 '13 at 13:47
  • but I don't want to make the string bigger.. I want to make the table of the strings bigger! – alex777 Jan 09 '13 at 13:48
  • I stopped trying to figure out the code after `sun()` / `str` / `buffer` / `j` / `q` (unused) and the one-line `for` loop. There's only so far I will go to help someone, and example code of such low quality is a waste of time. – DevSolar Jan 09 '13 at 13:51
  • 1
    Oh, okay can you edit your question to make that clear. – sean Jan 09 '13 at 13:51
  • 1) Yeah I know about gets.. haven't tried fgets yet. I will soon. 2) Yeah sorry for the unsused variables.. this is not all of the code!!!!!!! 3) What is the problem with one line for-loop ? – alex777 Jan 09 '13 at 13:52
  • @alex777: Check out sscce.org. Reducing your example to a *minimal* example shows courtesy to the people you are asking for help, trains your debugging / programming skills, *and* goes a long way towards finding your errors yourself. – DevSolar Jan 09 '13 at 13:55

3 Answers3

4
    string=realloc(string,size*sizeof(char**));
    i++;
    gets(string[i]);
    size++;

After you call realloc to enlarge string, the new portion contains no valid pointers. So when you call gets, you're passing it a pointer you failed to initialize.

Also, that size=0; is totally broken.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
2

realloc does not initialize the allocated memory with zeros, in addition you forgot to initialize the newly allocated string pointers.

Consider to move up i++ and size++ within the while loop.

CubeSchrauber
  • 1,199
  • 8
  • 9
0

Code Review

initialize all your variables

  char *str = NULL,**string = NULL,buffer[50] = {0},temp[2] = {0};
  int i = 0,j = 0,q = 0,size = 10,counter = 0;

do not cast what is returned from malloc/calloc and use {} when possible for clarity

string=calloc(size,sizeof(char*));
for (i=0; i<size; i++) 
{ 
  string[i]=malloc(50*sizeof(char));
}

When reading from the keyboard do not use gets, use fgets() since you can specify the max size to read.

printf("\nGimme strings, terminate input with x");
char input[256];
fgets(input,sizeof(input),stdin); // another varname, will explain below

With newer compilers you can declare variables where you need them instead of decl at top of function.

char temp={'x','\0'}; // 120;//x

setting size=0 here seems a bit strange

size=0;

it is better to keep what the user inputs in a separate buffer (input) then if it is not "x" copy it into your string array so instead of

while(strcmp(string[i],temp)!=0)
{
    string=realloc(string,size*sizeof(char**));
    i++;
    gets(string[i]);
    size++;
    counter++;
}

e.g.

while (fgets(input,sizeof(input),stdin) != NULL && input[0] != 'x')
{
   string[i] = calloc(1,strlen(input)+1); // add a byte for \0
   strncpy(string[i],input,strlen(input)-1); // not copying ending \n
   if ( ++i == size ) // a new chunk needed
   {
     char *newstring = realloc((size + 10)*sizeof(char*), string );
     if ( newstring != NULL )
     {
       string = newstring;
       size += 10;
     }
   }
}
AndersK
  • 35,813
  • 6
  • 60
  • 86