Home > OS >  struct allocation need help to understand what's wrong with my code -- allocating for double st
struct allocation need help to understand what's wrong with my code -- allocating for double st

Time:07-31

I am allocating memory for my struct test containers in function. But I am not sure whether the result is undefined behavior or is it some other mistake.

first I created a reference pointer

  struct test **x;

then I allocated 10 pointers and each pointer will suppose to point to single struct test at [0] index like this

 x=malloc(sizeof(struct test *) * 10);

so x[0] will be the only struct in container in *x and x[0] for next container (*x 1)

So now each container struct item I like to allocate in another function. So I assumed already that I should pass the address of x like get_s(&x,0x00200000);

so my function declaration need to be int get_s(struct test ***arr,int n) so is this correct understanding?

Then I address the real pointer x as in main like (*arr i) in function as i increments from 0 to 9 in loop but I need now to allocate struct test single item in each container so I need single loop and another starik sign to locate my struct in containers

so my function became something like this

int get_s(struct test ***arr,int n)
{
    for(int i=0;i<10;i  )
    {
        //each pointer will point to single struct test
        *(*arr i)=malloc(sizeof(struct test));
        (*(*arr i))->a=n;
    }
    return 0;

}

But I am not sure about this. and the first value in main is simply garbage value printing. So Whats wrong with following code?

When compiling it does not report any error

    #include <stdio.h>
    #include <stdint.h>
    #include <stdlib.h>

    struct test{
     uint32_t a;
    };  


    int get_s(struct test ***arr,int n)
    {
        for(int i=0;i<10;i  )
        {
            //each pointer will point to single struct test
            *(*arr i)=malloc(sizeof(struct test));
            (*(*arr i))->a=n;
        }
        return 0;

    }


    int main()
    {
        struct test **x;
        //allocated 10 pointers for arr[0] to arr[9] , is this correct? 
        x=malloc(sizeof(struct test) * 10);

        //alloc_populate(&x,10);
        get_s(&x,0x00200000);
        for(int i=0;i<10;i  )
        {       
            printf("%x\n", x[i]->a);
        }   
        return 0;
    }

CodePudding user response:

so my function declaration need to be int get_s(struct test ***arr,int n) so is this correct understanding?

There is no need to use 3 stars because you are simply modifying the contents of the array, not the value of the passed pointer, your function can look like:

int get_s(struct test **arr, int n)
{
    for(int i = 0; i < 10 ;i  )
    {
        // each pointer will point to single struct test
        arr[i] = malloc(sizeof(struct test));
        arr[i]->a = n;
    }
    return 0;

}

Then, from main, just pass the pointer itself and not a reference to the pointer:

get_s(x, 0x00200000);

Answering your comment in code:

struct test **x;
//allocated 10 pointers for arr[0] to arr[9] , is this correct? 
x=malloc(sizeof(struct test) * 10);

no, is not correct, you want:

x = malloc(sizeof(struct test *) * 10); // space for 10 pointers to struct test

or better yet:

x = malloc(sizeof(*x) * 10); // space for 10 pointers to struct test

Notice that your example, as is, doesn't need an array of pointers, why not an array of struct tests?

int get_s(struct test *arr, int n)
{
    for(int i = 0; i < 10; i  )
    {
        arr[i].a = n;
    }
    return 0;
}

int main(void)
{
    struct test *x;

    x = malloc(sizeof(*x) * 10);
    get_s(x, 0x00200000);
    for(int i = 0; i < 10; i  )
    {       
        printf("%x\n", x[i].a);
    }   
    return 0;
}

CodePudding user response:

It's easy to get lost with too much dereferencing...

The simplest way to learn may be to study a working version...

struct test {
    uint32_t a;
};

void get_s( struct test *arr[], int size, int initial ) {
    for( int i = 0; i < size; i   ) {
        arr[ i ] = (struct test*) malloc( sizeof( struct test ) ); // point to single
        arr[ i ]->a = initial;
    }
}

void main( void ) {
    const int nElem = 10;
    //allocated 10 pointers for arr[0] to arr[9] , is this correct?

    struct test **x = (struct test **)malloc( nElem * sizeof(struct test *) );

    get_s( x, nElem, 0x00200000 );

    for( int i = 0;i < nElem; i   )
        printf( "%d - %x\n", i, x[i]->a );
}

You should check that malloc() isn't failing and returning NULL...

  • Related