1

I would like to allocate memory for arrays that are members of a struct I need to use, inside a function that takes the struct as an argument.

arg->A.size=(int*) malloc(N*sizeof(int));

will not compile (request for member 'size' is something not a structure.

arg->A->size=(int*) malloc(N*sizeof(int));

will throw a segmentation fault error

Any help will be appreciated. Here is the code, thanks:

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

// struct A 
struct A {
    int dim;                // dimensions
    int* size;              // points per dim array
    double* val;            // values stored in array
    int total;              // pow(size,dim) 
};

// struct B that uses A
struct B {
    int tag;
    struct A* A;
};

int function_AB(struct B* B);

int main(void){
    struct B B;
    function_AB(&B);

    return 0;
}

int function_AB(struct B* arg){
    int N=10;
    arg->tag=99;
    printf("tag assigned = %d \n", arg->tag);
    arg->A->size=(int*) malloc(N*sizeof(int));

    return 0;
}

3 Answers 3

1

You simply haven't allocated memory for struct A *A. Before assigning anything to A->size you would first need to do something like

B->A = malloc(sizeof(struct A));
Sign up to request clarification or add additional context in comments.

Comments

1

The second case is correct, but crashes because the A inside the B declared in main has not been assigned a value. You probably want something like

struct A A;
struct B B;
B.A = &A;

function_AB(&B);

Comments

0

When you have structure pointer in another structure, first you need to allocate memory for that. then allocate the memory for structure members!

struct B {
    int tag;
    struct A* A;
};

Here A is a pointer to a structure called A. First allocate memory for this, then allocate memory for the elements of struct A

arg->A = malloc(sizeof(struct A));

then do-

arg->A->size = malloc(N*sizeof(int));

Try the following changes in your int function_AB(struct B* arg)-

int function_AB(struct B* arg){
int N=10;
arg->tag=99;
printf("tag assigned = %d \n", arg->tag);

arg->A = malloc(sizeof(struct A)); // First allocate the memory for struct A* A; 

arg->A->size = malloc(N*sizeof(int)); // Allocate the memory for struct A members
arg->A->val = malloc(N*sizeof(double));

// do your stuff

// free the allocated memories here
free(arg->A->size);
free(arg->A->val);
free(arg->A);

return 0;
}

And don't cast the result of malloc()!

3 Comments

So far I think Sathish was right (extending the change already suggested by downhillFromHere on the first answer). Implementing it inside the function is exactly what I was looking for, so that I specifically use arg->A = malloc(sizeof(struct A)) instruction regardless of prior/after assignment of values. I still would use malloc int and double casting for size and val. Is there any reason to avoid doing so? I still have to figure out about whether I must use free for A, size and val before the function return. I will update if something goes wrong. Thank you very much.
@mae In c malloc will allocate the memory and return the appropriate pointer to the allocated memory! If you found this answer is helpful don't forgot to validate it!
You're right. Bad practice due to migration from C++ on which it is actually needed.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.