0

What I'm basically doing is

  • passing a pointer to struct to a function and
  • the called function then allocates memory to it by malloc.

Now when I access this memory in Caller function there are no errors.

However if I try to access the same in the called function after allocation.

  • there are no errors in accessing first index of array
  • But as I try to access the next index of that array it gives segmentation fault

I'm trying to understand what is happening here or am I missing something.

Try it Online result

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

typedef struct _Data
{
    int  a;
    int  b;
    int  c;
} Data_t;

void update(Data_t** ppData)
{
    int i;

    *ppData = malloc(3 * sizeof(Data_t));
    if(*ppData == 0)
    {
        puts("Error malloc\n");
    }
    printf("2:Addr = %u %u\n", &ppData, ppData);

    #if 1
    for(i=0; i < 3; i++)
    {
        ppData[i]->a = (i+1)*10;
        ppData[i]->b = (i+1)*20;
        ppData[i]->c = (i+1)*30;

        printf("3:[%d] %d %d %d\n", i, ppData[i]->a, ppData[i]->b, ppData[i]->c);
    }
    #endif
}

int main()
{
    Data_t* data;
    int i = 0;

    printf("1:Addr = %u %u\n", &data, data);

    update(&data);

    printf("2:Addr = %u %u\n", &data, data);
    #if 0
    for(i=0; i < 3; i++)
    {
        data[i].a = (i+1)*10;
        data[i].b = (i+1)*20;
        data[i].c = (i+1)*30;

        printf("4:[%d] %d %d %d\n", i, data[i].a, data[i].b, data[i].c);
    }
    #endif
    for(i=0; i < 3; i++)
    {
        printf("5:[%d] %d %d %d\n", i, data[i].a, data[i].b, data[i].c);
    }
}
7
  • 2
    You are not referencing your pointer to pointer properly. Try using it like this. (*ppData)[i].a = (i+1)*10; Commented Nov 25, 2017 at 14:25
  • when outputting an error message, it should be output to stderr, not stdout When the error condition is from a system function, should also output the text that indicates why the system thinks the error occurred. Suggest using: perror( "malloc failed" ); which will handly both requirements. Commented Nov 26, 2017 at 3:30
  • when an error occurs, that cannot be recovered from, like the call to malloc() failing, then after displaying the error info via perror(), should 'cleanup' if necessary, the exit the program. The posted code keeps right on executing the program, as if the call to malloc() were successful. Commented Nov 26, 2017 at 3:32
  • in general, names that begin with a '_' followed by a capital letter and/or names ending with '_t' are 'reserved' for the system, so 'should' not be generated by user programs. Commented Nov 26, 2017 at 3:35
  • the posted code contains several 'magic' numbers. 'magic' numbers are numbers with no basis. I.E. 3, 10, 20, 30. 'magic' numbers make the code much more difficult to understand, debug, etc. Suggest using a enum statement or #define statements to give those 'magic' numbers meaningful names, then using those meaningful names throughout the code. Commented Nov 26, 2017 at 3:37

1 Answer 1

3

You are accessing it all wrong.

The access in the called function would be

 (*ppData)[i].a = (i+1)*10;

(*ppData) = The Data_t* variable.

Now we allocated 3 memory. And that we are accessing them. And each of which contains structure elements we are accessing them like that. (.a or .b) etc.

Whenever you get confused like this, consider step by step.

Like here suppose you allocated it properly. Now think what is *ppData? It is of type Data_t*.

Now think that you have *ppData with you in a variable called ptr.

So what you do now if you have allocated something to ptr? You access it like this

ptr[i] --> i-th allocated memory

What happens if you consider that ptr[i] is of type Data_t. So you access its elements like this

ptr[i].a or ptr[i].b etc.

Now here we did same only now we do it over *ppData.

Sign up to request clarification or add additional context in comments.

1 Comment

Oh yes I really missed that point. Really like your step by step approach. Thank You.

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.