Home > database >  Memory of dynamically allocated array gets overwritten after funktion returns
Memory of dynamically allocated array gets overwritten after funktion returns

Time:05-31

I know there are a lot of posts about this topic already, but I still don't find my error. The Problem is, that after the first time I call add_record() and return to main function, the Data still gets overwritten by printf, even though I use malloc and realloc (So the Data should be there after the function terminates). Where is my error?

Thanks in advance

This is my Code:

void add_record(Person ***pointer, int *lenght){
    *lenght = 1;
    //allocate one new Person struct pointer or create the first one
    if(*pointer == NULL)
        *pointer = malloc(1 * sizeof(Person*));
    else
        *pointer = realloc(*pointer, (*lenght) * sizeof(Person*));

    //return if realloc failed
    if(*pointer == NULL){
        printf("Fehler kein Speicher mehr vorhanden!!\n");
        return;
    }

    //creating new Struct (Person)
    Person temp;
    
    //Funktion call to fill the new Struct
    input_data(&temp);

    //giving the adress of new Person to pointer list
    (*pointer)[*lenght-1] = &temp;

    pointer = &(*pointer);
    
    //Output of Data (The Data should be correct here)
    printf("gehalt: %d\n", ((*pointer)[*lenght-1])->gehalt);
    printf("personalnummer: %d\n", ((*pointer)[*lenght-1])->personalnummer);
    printf("nachname: %s\n", ((*pointer)[*lenght-1])->nachname);

}

The input data function:

void input_data(Person *temp){

    printf("\nBitte Geben Sie den Nachnamen der Person ein: ");
    char temp_nachname[20];
    scanf(" s", &temp_nachname);
    strncpy((*temp).nachname,temp_nachname,20);
    printf("\nBitte Geben Sie das Gehalt der Person ein: ");
    int temp2;
    scanf("%d", &temp2);
    (*temp).gehalt = temp2;
    while(getchar() != '\n');
    printf("\nBitte Geben Sie die Personalnummer der Person ein: ");
    scanf("%d", &temp2);
    (*temp).personalnummer = temp2;
    while(getchar() != '\n');
    printf("\n");
}

The main function:

int main(){
    Person *(*array) = NULL;
    
    Person ***pointer = &array;
    int lenght = 0;
    
    add_record(pointer, &lenght);
    printf("lenght in main: %d", lenght);
    add_record(pointer, &lenght);
    
    printf("%d\n%s\n%d\n",(*pointer)[0]->gehalt,(*pointer)[0]->nachname,(*pointer)[0]->personalnummer);
    printf("\n\n%d\n%s\n%d\n",(*pointer)[1]->gehalt,(*pointer)[1]->nachname,(*pointer)[1]->personalnummer);
    printf("Programm beendet\n");
    return 0;
}

CodePudding user response:

Your actual bug is here

Person temp; <<<<=====

//Funktion call to fill the new Struct
input_data(&temp);

//giving the adress of new Person to pointer list
(*pointer)[*lenght-1] = &temp; <<<<<====

you are storing the address of a stack structure. This is not a valid operation and results in Undefeined Behavior. Use a std::vector instead

CodePudding user response:

Looking at your add_record() function, it looks like you're trying to dynamically allocate / initialize your Person Struct. It may be better if your would create your Person{} struct first, followed by your function's preprocessors, your main() function, and then your function definitions last. This format is standard and also makes your code easier to read, search for errors, and debug.

// You could use a typedef here ...
typedef struct Person {
     ...
     ...
     ...

} psn;

// Preprocessor
psn *create_person(psn *p);

// Preprocessor
void add_record(arg1, arg2);

// Preprocessor
void input_data(arg1);

int main(void) {
   ...
   ...
   ...

   return 0;
}

// Function Definition ...
psn *create_person(psn *p) {
    ...
    ...
    ...

}

// Function Definition ...
void add_record(arg1, arg2) {
    ...
    ...
    ...

}

// Function Definition ...
void input_data(arg1) {
    ...
    ...
    ...

}

Also, could you please give some more details about what your program is trying to accomplish?

As for your add_record(Person ***pointer, *length) function, I don't see where you have actually declared and initialized your Person struct or your length Pointer. I do not think anything is being overwritten because it has never been initialized.

Looking at your add_record() function call in the main(),it is not clear where these Pointers are being initialized.

I am assuming you want to initialize them (allocate memory to them) in your input_data() function, but I do not see where that actually happens.

If you could also rename your variables and arguments to something related to their purpose, it would help a lot when looking for the problem.

If you would please make some of these changes, it would help myself and others help you find a solution to your problem.

  • Related