Arthur VP
Arthur VP

Reputation: 17

Releasing pointer memory aswell as pointer itself

A "Deeltal" keeps track of how many dividers an integer has (count) and keeps them in an array (dividers).

Examples:

value = 8 -> count = 3 and dividers = {1,2,4}

value = 10, count = 3, dividers = {1,2,5}

Hope everything is clear, take a look at the following code:

typedef struct{
    int value;
    int count;
    int* dividers;
} Deeltal;


void free_dividers(Deeltal *g){  /*Deletes the int* dividers of a given Deeltal*/
    free (g - > dividers);
}

/* the following two functions have the same purpose: deleting the content of a 
given amount of "Deeltal" AND deleting the pointer to it aswell*/

void free_amountOfdeeltal(Deeltal *d, int amount){
    int i;
    for (i = 0; i < amount; i++){
        free_dividers(&d[i]);
    }
    free(d);
}

void free_amountOfdeeltalVersion2(Deeltal **g, int amount){
    int i;
    for(i = 0; i < amount; i++){
        free_dividers(&(*g)[i]);
    }
    free(*g);
}

If my main looked something like this

int main(void){
    /*EDIT 3/11/2017: forgot to allocate memory for *d and initializing g.
    Thanks for pointing this out*/

    Deeltal g = 0;
    g.value = 6; g.count = 3; g.dividers = {1,2,3};

    Deeltal *d = malloc(sizeof(Deeltal));
    d->value = 6; d->count = 3; d->dividers = {1,2,3};

    free_amountOfdeeltal(&g);
    free_amountOfdeeltalVersion2(&d);
}

What is the difference between free_amountOfdeeltal and free_amountOfdeeltalVersion2?

Both should do the same thing: releasing the memory of a Deeltal and also deleting the pointer pointing to that memory.

On a sidenote: How do you delete the memory as well as the pointer?

Upvotes: 0

Views: 87

Answers (2)

MFisherKDX
MFisherKDX

Reputation: 2866

Not withstanding calling this function with invalid data as pointed out by others .. I'll attempt to answer the question I think you are asking.

On a sidenote: How do you delete the memory as well as the pointer?

You can't really "delete the pointer" in this context as a pointer is simply a variable that is assigned an address. You delete memory that was allocated to you by passing free a pointer to the memory. Note that free does not modify the value of the pointer at all. (It can't because the pointer is passed by value.) After the call to free the pointer still points to the same memory address.

If what you mean is "how can I assign a meaningful value to the pointer to identify that its memory has already been deleted," then you can use the second form of your function:

void free_amountOfdeeltalVersion2(Deeltal **g, int amount);

and set *g to NULL before returning. You can then use this information than the pointer is NULL to identify the memory has already been deleted.

Upvotes: 1

dieortin
dieortin

Reputation: 522

You didn't allocate any memory for d so your pointer doesn't point to any structure. Therefor, you can't access its properties or free its memory because you didn't reserve it in the first place. There's no way this code could come remotely close to compiling.

First of all you should be allocating memory for a "Deeltal" structure like this:

Deeltal *d = malloc(sizeof(Deeltal));

I recommend you go back and relearn how pointers work, as you're doing some really weird stuff there.

Upvotes: 0

Related Questions