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); } 
  • Can you guarantee that in parse_command_input , the value pointed to by parsed_command does not change? But in general, you somehow everything is done so strangely that ... Why, for example, are these games indirectly char** , why the allocation is dynamic - is SIZE_ARG really that big? Why not read immediately through fgets ? In short, there are more questions than answers ... - Harry
  • Yes, still - you can potentially jump out of the buffer when reading MAX_BUFFER_SIZE characters and certainly read beyond these limits when i==0 in a loop ... - Harry
  • The fgets () function is forbidden to use ... And dynamic selection, on the contrary, is mandatory for execution - Anton Barinov
  • Prohibited by whom? If with a teacher, you use it anyway ... And the dynamic selection is fine, but why double? - Harry
  • I am sorry, confused, getline () is forbidden, fgets () is allowed. And at the expense of the pornography that was written there, it was made to process EOF and terminate the program, when I tried to enter EOF without getchar () and checking for EOF, an infinite loop was obtained, it does not save getline () - EOF in short, and then it is clear whether the empty string was entered, the EOF roofing felts, and I, at EOF, need to exit the cycle. I didn’t come up with a more elegant solution. But how can I do it differently ? I’ll have to save each word of the entered command separately, but obviously it will not work through a one-dimensional array - Anton Barinov

1 answer 1

This error may be due to the fact that somewhere in your program there is a recording outside the allocated memory, as a result of which the value of the parsed_command pointer parsed_command been overwritten. That is, your program has an undefined behavior.

Check all functions and loops where you use the command and parsed_command .

The easiest way is to insert the output to the console of the variable parsed_command everywhere after it has been used.

For example,

 printf( "%p\n", ( void * )parsed_command[0] ); 

In passing, I note that this cycle is not already correct

  for(int i = MAX_BUFFER_SIZE-1; i>=0; i--) { command[i]=command[i-1]; ^^^^ } 

since when variable i is 0 , memory is accessed outside the array.

EDIT Judging by the comment to my answer, it is possible that the cause of the program’s indefinite behavior could be the redistribution of memory addressed by the parsed_command pointer when the pointer was passed to the function by value. For example,

 void f( char **p ); { p = realloc( p, 2 * sizeof( char * ) ); //... } //... f( parsed_command ); 

In this case, the original parsed_command pointer parsed_command not been changed. The function dealt with a copy of the original pointer. As a result, you may get an error when releasing the parsed_command pointer, indicating that the memory has already been freed. If this is the case, then the pointer must be passed to the function by reference. For example,

 void f( char ***p ); ^^^ { char **tmp = realloc( *p, 2 * sizeof( char * ) ); if ( tmp != NULL ) *p = tmp; //... } //... f( &parsed_command ); ^^^^^^^^^^^^^^^ 
  • Thanks, corrected the array, now the error crashes on the second free () and the first works - Anton Barinov
  • @AntonBarinov Print printf ("% p \ n", (void *) parsed_command); after allocating the memory and before deleting and compare the values ​​obtained. If they are different. then look for the place in the program where they change. - Vlad from Moscow
  • I looked in the debugger, and at the time of initialization and at the time of calling the free () function, the address is the same. I went through the program step by step in the case of one argument entered and several (then realloc from another function will work, I added it to the listing) and the address does not change from the very beginning to the end, all the same error pointer freed freed was not allocated for free (parsed_command); - Anton Barinov
  • @AntonBarinov I can only guess, but I suspect that you passed this pointer by value (that is, you passed a copy of the pointer value to the function) to the function where you called realloc for that copy. As a result, your original pointer has not changed. Only the value of the function parameter, which is a local variable of the function, has changed. After exiting the function, this parameter has ceased to exist. You need to pass the original pointer by reference. - Vlad from Moscow
  • Well, you can check it out by printing out the contents of the variable before freeing the memory, if it is empty, it means I worked with a copy, and if it contains what the program should do, then it was transmitted via a link? - Anton Barinov