This question has already been answered:
Hello, help please deal with memory. When the realloc function is called, the parse_command_input function crashes: "pointer being realloc'd was not allocated."
What could be the problem?
typedef char* string; int execute(person* person_array) { string* parsed_command; if(!(parsed_command = malloc(sizeof(string)))){ error_notification(12); return 2; } parsed_command[0] = malloc(SIZE_ARG*sizeof(char)); char command[MAX_BUFFER_SIZE]; string quit = "quit\n"; do{ printf("esp> "); if(fgets(command, MAX_BUFFER_SIZE, stdin)==NULL){ // save input in "command" return 2; } parse_command_input(command, person_array, &parsed_command); }while(strcmp(command,quit)); printf("Bye.\n"); free(&parsed_command[0]); free(parsed_command); return 0; } void parse_command_input(const string command, person* person_array, char*** parsed_command){ string delim = strtok(command, " "); int counter = 0; while (delim != NULL){ if(counter > 0) { char **tmp = realloc(*parsed_command, (counter+1)*sizeof(char*)); if(tmp!=NULL) *parsed_command = tmp; } parsed_command[counter] = &delim; counter++; delim = strtok (NULL, " \n"); } which_command(parsed_command, counter, person_array); }
string? - VladD