I'm having an issue where a call I'm making to malloc()
causes my program to crash. Here's the code:
void update_item(char *input, item_t *new_node){
int i, count, shelf, weight, price, quantity;
char *name;
char *specifier;
char aisle[1];
count = 0;
/*Find name of the new item and assign to the name field of new_node...*/
for (i = 0; input[i] != ','; i++){
count++;
}
name = (char*)malloc((count+1)*sizeof(char));
if (name == NULL){
printf("Out of memory. Shutting down.\n");
exit(EXIT_FAILURE);
}
for (i = 0; input[i] != ','; i++){
name[i] = input[i];
}
name[count+1] = '\0';
new_node->name = name;
printf("%s\n", new_node->name);
/*Find aisle specifier and assign it to aisle field of new_node...*/
i++;
aisle[0] = input[i];
aisle[1] = '\0';
new_node->aisle = aisle;
printf("%s\n", new_node->aisle);
for(i = i+2, count = 0; input[i] != ','; i++){
count++;
}
specifier = (char*)malloc(count*sizeof(char)); /*PROGRAM CRASHES HERE*/
if (specifier == NULL){
printf("Out of memory. Shutting down.\n");
exit(EXIT_FAILURE);
}
printf("boom\n");
I'm utterly stumped. There's two identical calls to malloc()
but for some reason the second fails every single time while the first one always is a success.