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); } 

Marked as a duplicate by members of Harry , user194374, Community Spirit Dec 19 '16 at 7:54 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • And what is your string ? - VladD
  • there typedef char * string - Anton Barinov
  • @Anton Barinov: Start by stopping typing typedef names that hide pointers. It is completely incomprehensible what level of code in your code is obtained. - AnT
  • And what is the fundamental difference between this issue from sta.kv.flowflow.com/questions/604984/… ? - Harry

1 answer 1

The code does not make sense. For example, why is memory allocated here?

 string* parsed_command; if(!(parsed_command = malloc(sizeof(string)))){ error_notification(12); return 2; } parsed_command[0] = malloc(SIZE_ARG*sizeof(char)); 

Where is this array used for SIZE_ARG long?

Why in this sentence is taken the address of the pointer?

 free(&parsed_command[0]); ^^^ 

The parsed_command pointer is not used at all by the execute function. Therefore, it is not at all clear why he is declared in it.

What is the point of this sentence?

 parsed_command[counter] = &delim; 

This is where non-allocated memory is accessed.

  • The bottom line is that I need a dynamic array in which the input command will be saved as each individual word in its cell. Later it is processed and various commands are called (there are 6 of them). Therefore, I allocate memory dynamically, and with each new input word I need to expand it. I divide the commands I entered into words by spaces, save these words separately in delim, expand the memory and save delim in parsed_command. That is the meaning. At the expense of free (), there is an error, corrected. - Anton Barinov
  • What do you think, maybe it would be better to initialize and release this array right after each call right into parsed_input_command (), because as I understand it, I expand memory every time, with each call, but there’s no point in that, one harm, better to release it after the call? - Anton Barinov
  • And I apologize for the offtopic, but you obviously have a lot of programming experience, could you tell me which book (in English / German), or something like that, which describes in detail how to work with threads / multi-threading on C, how many I searched, everywhere either in fragments or only a part of the information, the university also only provides links to the site with a brief description. Thanks in advance - Anton Barinov
  • @AntonBarinov Alas, I don’t know a single book about multithreading in C. - Vlad from Moscow
  • and how then to learn / learn it? where to get the information? only forums and articles? - Anton Barinov