0

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);
}
6
  • 2
    what is your process? where does it fail using step by step debugging? Commented Apr 5, 2013 at 21:02
  • So, things work when you don't use process and fail when you use process. Hmm... that's a tricky one... it would seem to me that common sense would suggest that we need to see the code for process. Alas, common sense doesn't seem to be all that common. Commented Apr 5, 2013 at 21:04
  • Are you allocating any memory inside process? If so, make sure you deallocate it properly. Commented Apr 5, 2013 at 21:05
  • I just make some linked list processes inside process Commented Apr 5, 2013 at 21:13
  • 1
    strcpy(outputMessage , "server added "); You're not allocating memory for outputMessage. That means the strcpy and subsequent strcats are undefined behaviour. Commented Apr 5, 2013 at 21:27

1 Answer 1

1

outputMessage needs to be allocated before you strcpy/strcat to it. I'm surprised you get an out of memory error and not just a segmentation fault.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.