Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to free size_t from struct

I am trying to free the elements of a struct, which has size_t variables and char. How do free the size_t ones, because I keep getting warnings like

[Warning] passing arg 1 of `free' makes pointer from integer without a cast

Now I understand that i need to make a cast, but I don't know how! Here's the code:

typedef struct collection
{
    char **c;
    size_t nc, na, ne;
} TCS, * ACS;

void Destroy(ACS *x)
{
    int i;
    free((*x)->na);
    for(i=0;i<(*x)->nc;i++)
        free((*x)->c[i]);
    free((*x)->c);
    free((*x)->nc);
    free((*x)->ne);

}

/* allocating */
ACS AlocCS(size_t d, size_t e)
{
    ACS *af=(ACS*)malloc(d);
    if(af==NULL)
        return 0;
    (*af)->na=e;
    (*af)->nc=d;
    (*af)->c=(char**)calloc(e*d,sizeof(char));
    if((*af)->c==NULL){
        free(af);
        return 0;}            

    return *af;
}

I am getting 3 warnings, all related to na,ne,nc. What am I skipping? Thanks!

LE: thanks everyone, my project works now ! Happy holidays!

like image 742
SpaceNecron Avatar asked Dec 11 '22 19:12

SpaceNecron


1 Answers

I am getting 3 warnings, all related to na,ne,nc. What am I skipping?

malloc returns a pointer to a dynamic allocated memory area. size_t variables can't hold such addresses.

You just need to free what you alloced, ie (*af)->c and af.

like image 72
md5 Avatar answered Dec 23 '22 00:12

md5