简体   繁体   中英

allocating memory to a pointer of an array of structures

I am trying to allocate memory to a pointer of an array of structures but when I compile the terminal sends me segmentation fault , this is my code:

typedef struct codif {
    unsigned char simbolo;
    int nbits;
    unsigned int codigo;
} codificacion;

codificacion **matcod;
*matcod = malloc(256 * sizeof((*matcod)[0]));

This pointer

codificacion **matcod;

is uninitialized and has an indeterminate value. So dereferencing it like *matcod results in undefined behavior.

Maybe you mean the following

codificacion *matcod;
matcod = malloc( 256 * sizeof( *matcod ) );

Or something like the following

codificacion *p;
codificacion **matcod = &p;
*matcod=malloc(256*sizeof((*matcod)[0]));

For example if you have a function defined like

void f( codificacion **matcod )
{
    *matcod=malloc(256*sizeof((*matcod)[0]));
    //...
}

then it can be called like

codificacion *matcod;
f( &matcod );

You are probably expected to write a function that takes a pointer to a pointer to this array of structures as an argument. So codificacion **matcod is the argument of the function, not a local undefined variable which causes undefined behavior when you dereference it to store the address of the allocated array.

Here is a modified version:

typedef struct codif {
    unsigned char simbolo;
    int nbits;
    unsigned int codigo;
} codificacion;

int allocate_array(codificacion **matcod) {
    *matcod = malloc(256 * sizeof((*matcod)[0]));
    return *matcod != NULL;
}

And call this function passing the address of an actual pointer:

    codificacion *mat;
    if (!allocate_array(&mat)) {
        printf("allocation error\n");
    }

You should always declare your variables with something. Compilers will not always zero-out uninitialized variables. I'm also a big fan of using calloc when declaring arrays -- it's a stylistic choice, but especially with arrays of pointers, ensures everything is zeroed out. Uninitialized data can be hell to debug.

codificacion **matcod = NULL;
matcod = calloc(256, sizeof(codificacion*));

Note that we have created a 256 element array of pointers, not whole structs, and because ** is an array of pointers to structs and not an array of structs you then need to allocate each struct:

for(int index=0; index<256; index++)
  matcod[index] = malloc(sizeof(codificacion));

You would then reference your elements with matcod[index]->nbits .

Now, what you should do is just implement a flat array of structs and then pass the pointer to that around. Using static allocation, you even get to avoid the calloc call.

codificacion matcod_array[256] = { 0 };
codificacion *matcod = (codificacion *)&matcod_array;

Because you're only passing a pointer to an array of structs as opposed to a pointer to an array of pointers to single structs, you would then reference elements in the array using matcod[index].nbits .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM