segmentation fault - Out of Memory in String Arrays with C -
i have string array this:
char **strarray;
this array full , can print contents method:
while(*strarray){ printf("%s\n",*(strarray++)); }
this works normally. when use this:
while(*strarray){ process(*(strarray++)); }
i take out of memory error on second string in array. runs first string, fails when goes second.
thanks help!
process method 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); }
outputmessage needs allocated before strcpy/strcat it. i'm surprised out of memory error , not segmentation fault.
Comments
Post a Comment