Search code examples
carraysstructrealloc

How to create dynamic array of structs using realloc in function


I'm trying to create dynamic array of structs (pics in this case) using realloc. I want to do it in function, but there's a problem: variables in main are not changed by this function. There must be problem with pointers but I cannot find it. PS: Function UploadFile works properly. There's a function:

int AddPicture(struct Picture ***tab, int *PicCounter)
{
    struct Picture *temp;
    struct Picture pic;
    (*PicCounter)++;
    temp = realloc(*tab, (*PicCounter) *sizeof(*temp));
    if (temp != NULL)
    {
        UploadFile(&pic);
        **tab = temp;
        (*tab)[*PicCounter-1]->name = pic.name;
        (*tab)[*PicCounter-1]->type[0] = pic.type[0];
        (*tab)[*PicCounter-1]->type[1] = pic.type[1];
        (*tab)[*PicCounter-1]->width = pic.width;
        (*tab)[*PicCounter-1]->height = pic.height;
        (*tab)[*PicCounter-1]->scale = pic.scale;
        (*tab)[*PicCounter-1]->table = pic.table;
    }
    else {
        printf("Memory reallocation error");
        free(temp);
        return 1;
    }
return 0;
}

There's how I call it in main:

struct Picture *tabofpics;
int piccounter = 0;
tabofpics = malloc(1 * sizeof(*tabofpics));
AddPicture(&tabofpics,&piccounter);

Thanks for help.

EDIT: I've tried **tab instead of ***tab and values in main are correct, but it acts like memory doesn't reallocate properly, even if realloc doesn't return NULL.

  int AddPicture(struct Picture **tab, int *PicCounter)
{
    struct Picture *temp;
    struct Picture pic;
    (*PicCounter)++;
    temp = realloc(*tab, (*PicCounter) * sizeof(*temp));
    if (temp != NULL)
    {
    UploadFile(&pic);
    *tab = temp;    
    tab[*PicCounter - 1]->name = pic.name;
    tab[*PicCounter - 1]->type[0] = pic.type[0];
    tab[*PicCounter - 1]->type[1] = pic.type[1];
    tab[*PicCounter - 1]->width = pic.width;
    tab[*PicCounter - 1]->height = pic.height;
    tab[*PicCounter - 1]->scale = pic.scale;
    tab[*PicCounter - 1]->table = pic.table;
}
else {
    printf("Memory reallocation error");
    free(*tab);
    return 1;
}
return 0;
}                                                                                                                             

The idea is to put as many pics in the program as one want to, do some operations on its and leave. There must be function to add and delete pics whenever user want to, however when I call function with **tab in argument second time I get Access violation location, so as I mentioned earlier, realloc must not work properly.


Solution

  • According to answers of @Iharob and @John I was able to write a working function:

     int AddPicture(struct Picture **tab, int *PicCounter)
    {
    struct Picture *temp;
    (*PicCounter)++;
    temp = realloc(*tab, (*PicCounter) * sizeof(*temp));
    if (temp != NULL)
    {
        struct Picture *pic;
        pic = malloc(sizeof(*pic));
        UploadFile(pic);
        *tab = temp;
        (*tab)[(*PicCounter)-1] = *pic;
        free(pic);
    }
    else {
        printf("Memory reallocation error");
        free(*tab);
        (*PicCounter)--;
        return 1;
    }
    return 0;
    }
    

    It's not perfect, but it works properly. Thanks for help!