Hello, I need to create an array of structures with a family tree, and one of the functions adds a new person to this tree, provided that such a person does not exist in it yet. And everything seems to be good, initially in the array of structures 1 element, when the function is first called (always called with two persons), it writes the first person into the first cell, expands the memory, and writes the second person into the second cell. The next time the function is called and the person is added, the error "malloc: *** error for object 0x100200390: pointer being crashed" crashes when trying to add memory using realloc. I have a static variable size, which stores the size of the array.

What could be the error and how to fix it, thanks in advance

At the input is a command like add name surname [m / f] relation name surname [m / f] example: add ivan ivanov [m] father petr petrov [m] In case such oyudey does not exist in the database, then you need them add there.

typedef char* string; int parse_com_line_arg (int const argc, char** argv){ if(argc < 2){ if(argc == 1){ person *person_array; person_array = (person*)malloc(sizeof(person)); set_struct_to_null(&person_array[0]); } else{ in_out_file(); } } else{ error_notification(1); return 1; } return 0; } void add(person* person_array, const string* parsed_command, const int length) { static int size = 0; string name_p; int rel_position = 0; person buffer_person; int counter = 0; for (int i = 0; i < length; i++) // Π² массивС parsed_command хранятся //всС слова ΠΊΠΎΠΌΠ°Π½Π΄Ρ‹ ΠΏΠΎ ΠΎΠ΄Π½ΠΎΠΌΡƒ Π² ячСйкС // Π½Π°Ρ…ΠΎΠΆΡƒ ΠΏΠΎΠ·ΠΈΡ†ΠΈΡŽ "родства" ΠΌΠ΅ΠΆΠ΄Ρƒ двумя людьми { if (is_relation(parsed_command[i])) { rel_position = i; } } for (int i = 1; i < rel_position + 2; i = i + rel_position + 1) { // Π½Π°Ρ…ΠΎΠΆΡƒ ΠΈΠΌΠ΅Π½Π° людСй ΠΈ Ρ€ΠΎΠ΄, добавляю ΠΈΡ… Π² Π±ΡƒΡ„Π΅Ρ€Π½ΡƒΡŽ структуру для дальнСйшСго сравнСния с ΠΈΠΌΠ΅Π½Π°ΠΌΠΈ ΠΈ Ρ€ΠΎΠ΄ΠΎΠΌ Π² массивС // сначала имя ΠΏΠ΅Ρ€Π²ΠΎΠ³ΠΎ Ρ‡Π΅Π»ΠΎΠ²Π΅ΠΊΠ°, ΠΏΠΎΡ‚ΠΎΠΌ ΡΡ€Π°Π²Π½ΠΈΠ²Π°ΡŽ Π΅Π³ΠΎ с ΠΈΠΌΠ΅Π½Π°ΠΌΠΈ Π² массивС, Ссли ΡƒΠΆΠ΅ сущСствуСт, ΠΈΠ΄Ρƒ ΠΌΠΈΠΌΠΎ, Ссли Π½Π΅Ρ‚ добавляю // ΠΏΠΎΡ‚ΠΎΠΌ имя ΠΈ Ρ€ΠΎΠ΄ Π²Ρ‚ΠΎΡ€ΠΎΠ³ΠΎ Ρ‡Π΅Π»ΠΎΠ²Π΅ΠΊΠ°, Ρ‚ΠΎΡ‚ ΠΆΠ΅ Π°Π»Π³ΠΎΡ€ΠΈΡ‚ΠΌ, Π½Π° этом Ρ†ΠΈΠΊΠ» заканчиваСтся ΠΈ Ρ€Π°Π±ΠΎΡ‚Π° Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ Ρ‚ΠΎΠΆΠ΅ заканчиваСтся name_p = return_name(parsed_command, i, length); i--; strcpy(buffer_person.name, name_p); free(name_p); strcpy(buffer_person.sex, parsed_command[rel_position - 1]); if (size == 0) { // Ссли массив пустой, Ρ‚ΠΎ Π±Π΅Π· ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΎΠΊ добавляю ΠΏΠ΅Ρ€Π²ΠΎΠ³ΠΎ Ρ‡Π΅Π»ΠΎΠ²Π΅ΠΊΠ° strcpy(person_array[size].name , buffer_person.name); strcpy(person_array[size].sex , buffer_person.sex); size++; } // Ссли массив Π½Π΅ пустой, Ρ‚ΠΎ ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΊΠ° Π½Π° совпадСниС else { for (int j = 0; j < size; j++) { if ((strcmp(buffer_person.name, person_array[j].name)) || (strcmp(buffer_person.sex, person_array[j].sex))) counter++; } if (counter == size) { //Ссли счётчик Ρ€Π°Π²Π΅Π½ Ρ€Π°Π·ΠΌΠ΅Ρ€Ρƒ массива, Ρ‚ΠΎ добавляю Π½ΠΎΠ²ΠΎΠ³ΠΎ Ρ‡Π΅Π»ΠΎΠ²Π΅ΠΊΠ° //добавляю ΠΎΠ΄Π½Ρƒ ячСйку памяти ΠΊ ΠΈΠΌΠ΅ΡŽΡ‰Π΅ΠΌΡƒΡΡ массиву person* temp = (person*)realloc(person_array, ((size + 1) * sizeof(person))); if (!temp) { error_notification(5); exit(0); } person_array = temp; strcpy(person_array[size].name , buffer_person.name); strcpy(person_array[size].sex , buffer_person.sex); size++; } counter = 0; } } } 
  • Show how you get person_array , which you pass to the function, as well as the declaration of this structure. But I will tell you right away - using malloc/realloc and others like them with C ++ classes (as well as using a pointer to a string ) is a mockery of common sense. There is a vector for a dynamic array. - Harry
  • I don’t have any pointers to string, this is a simple typedef for ease of reading) is now being added to the listing - Anton Barinov
  • Do not do such typedef'y, which are confusing ... - Harry
  • Well, the program is on a classic c, here the string from c ++ can not be, I just had to add it to the listing immediately, I did not think - Anton Barinov
  • In general, the message indicates that an invalid pointer is being passed. This may also be due to the fact that somewhere the memory deteriorates - writing abroad, double release - in a word, a violation of the service memory structures. Getting through when there is not a single comment requires excessive effort. For a start, I would make sure in the debugger or debug output that in realloc that pointer is passed that is received from malloc. - Harry

0