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; } } }
person_array, which you pass to the function, as well as the declaration of this structure. But I will tell you right away - usingmalloc/reallocand others like them with C ++ classes (as well as using a pointer to astring) is a mockery of common sense. There is avectorfor a dynamic array. - Harry