I have a string array like this:
char **strArray;
this array is full and i can print its contents by this method:
while(*strArray){
printf("%s\n",*(strArray++));
}
This works normally. But when i use this:
while(*strArray){
process(*(strArray++));
}
I take out of memory error on second string in array. It runs for first string, but fails when goes for second.
Thanks for help!
Process method is like below:
process(char *line)
{
char *server_id,*delimiter,*outputMessage,*capacity_str;
int capacity;
delimiter = " ";
strtok(line,delimiter);
server_id = (char *)strtok(NULL,delimiter);
capacity_str = (char *)strtok(NULL,delimiter);
capacity = atoi(capacity_str);
curr_server = (server *)malloc(sizeof(server));
curr_server->server_id = server_id;
curr_server->capacity = capacity;
curr_server->full_capacity = 0;
curr_server->next = head_server;
head_server = curr_server;
strcpy(outputMessage , "server added ");
strcat(outputMessage,server_id);
strcat(outputMessage,"~");
strcat(outputMessage,capacity_str);
strcat(outputMessage,"\n");
writeOutput(outputMessage);
}
process? where does it fail using step by step debugging?processand fail when you useprocess. Hmm... that's a tricky one... it would seem to me that common sense would suggest that we need to see the code forprocess. Alas, common sense doesn't seem to be all that common.process? If so, make sure you deallocate it properly.strcpy(outputMessage , "server added ");You're not allocating memory foroutputMessage. That means thestrcpyand subsequentstrcats are undefined behaviour.