Home > Enterprise >  problem with free in langage C which doesn't work
problem with free in langage C which doesn't work

Time:01-22

I wrote this program and I want to free a structure myStruct but it doesn't compile it says:

free():double free detected in tcache2

Can you help me please ?

#include <stdio.h>
#include <stdlib.h>

typedef struct myStruct {
    char* chaine;
    struct myStruct* next;
} mystruct;

void supprimer(myStruct* D) {
    free(D->chaine);
    free(D);
}

int main()
{
    myStruct* D = malloc(sizeof(myStruct));
    D->next = NULL;
    char* chaine = malloc(sizeof(char)*10);
    chaine = "ouii";
    D->chaine = chaine;
    supprimer(D);
    printf("Hello World");

    return 0;
}

I tried to run the main with creating a new structure and deleting it but it doesn't work

CodePudding user response:

chaine = "ouii"; is wrong. That does not copy “ouii” into the memory pointed to by chaine, it sets the pointer chaine to point to the array created for the string literal "ouii". That address should not be passed to free.

Change chaine = "ouii"; to strcpy(chaine, "ouii");.

CodePudding user response:

char* chaine = malloc(sizeof(char)*10);
    chaine = "ouii";

The second statement changes what chaine was originally pointing to (i.e. a pointer to a region of heap memory returned by malloc), such that it now points to the string literal ouii. Now you've lost access to the original memory returned by malloc (have leaked memory), and are trying to free memory not returned by one of the memory allocation functions. Hence the warning.

Possible fix:

  1. Use the standard strcpy to copy the strings.

See also: How do I modify a char** in another function

CodePudding user response:

This code snippet

char* chaine = malloc(sizeof(char)*10);
chaine = "ouii";

results in a memory leak.

At first a memory was allocated dynamically and its address was assigned to the pointer chaine

char* chaine = malloc(sizeof(char)*10);

and then the pointer was reassigned with the address of the first character of the string literal "ouii"

chaine = "ouii";

So the address of the dynamically allocated memory was lost.

Instead of this assignment

chaine = "ouii";

you need to copy characters of the string literal into the dynamically allocated memory using standard string function strcpy declared in the header <string.h>. For example

#include <string.h>

//...

char* chaine = malloc(sizeof(char)*10);
strcpy( chaine, "ouii" );

Pay attention to that this function

void supprimer(myStruct* D) {
    free(D->chaine);
    free(D);
}

makes the pointer D declared in main

myStruct* D = malloc(sizeof(myStruct));

invalid.

It is better to pass the pointer to the function by reference. In C passing by reference means passing an object (including pointers) indirectly through a pointer to it. Thus dereferencing the pointer you will have a direct access to the object pointed to by the pointer.

Also as it seems you want to define a singly linked list then the function can look the following way

void supprimer( myStruct **D ) 
{
    while ( *D != NULL )
    {
        myStruct *current = *D;
        *D = ( *D )->next;
        free( current->chaine );
        free( current );
    }
}

And the function is called like

supprimer( &D );

In this case after calling the function the pointer F defined in main will be equal to NULL.

  • Related