1

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]));
5
  • *matcod=malloc(256*sizeof((*matcod)[0]));-->matcod=malloc(256*sizeof(codification *)); Commented May 6, 2020 at 20:31
  • 4
    dereferencing uninitialized pointer matcod causes the error Commented May 6, 2020 at 20:33
  • @chqrlie the problem is the part before = Commented May 6, 2020 at 20:50
  • @user12986714: yes indeed. sorry I misinterpreted your comment, but your first comment does not point an error, merely a style issue. Commented May 6, 2020 at 20:57
  • @sartrap1: you can accept one of the answers by clicking on the grey checkmark below its score Commented May 15, 2020 at 9:47

3 Answers 3

2

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 );
Sign up to request clarification or add additional context in comments.

3 Comments

@sartrap1 I have updated my answer. Do you mean to allocate and array of 256 elements?
sadly for this assigment we are given the type of data that goes into the functions so i cant change ¨codificacion **matcod;¨ but i do know it points to an array of structures
@sartrap1 I do not understand what function you are saying. See my updated post.
1

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");
    }

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.