Hello, help please deal with memory. When calling this function, the error takes off: "17_final (10369,0x100085000) malloc: * error for object 0x7fff5fbff550: allocated memory to it through malloc, and the error indicates that the memory is not initialized. I will be glad to any advice
UPDATE: Now with realloc, this kind of error crashes: pointer being realloc'd was not allocated.
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) { //*(parsed_command) = (string*)realloc(parsed_command, counter+2); 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); }
parse_command_input, the value pointed to byparsed_commanddoes not change? But in general, you somehow everything is done so strangely that ... Why, for example, are these games indirectlychar**, why the allocation is dynamic - isSIZE_ARGreally that big? Why not read immediately throughfgets? In short, there are more questions than answers ... - HarryMAX_BUFFER_SIZEcharacters and certainly read beyond these limits wheni==0in a loop ... - Harry