Kavindu Vindika
Kavindu Vindika

Reputation: 2737

Assignment of structure in C

Following function of convolution, args pointer of the relevant struct is passed and then I need to assign it to another struct of same kind. Is it correct to assign as given below or is it required to initialize the memory for actual args.

typedef struct
{
    int batchSize;
    int filters;
    int chan;
    int inSize;
    int kSize;
    int stride;
    float *padded_inp;
    float *kernal;
    float *conv_out;
    int convPad;
    int b; // IMAGE NUMBER
} compute_convFWD;

void *convolution(void *args)
{
    compute_convFWD *actual_args = args; // is this a correct assignment ?
    ...
}

Upvotes: 0

Views: 141

Answers (3)

AdamF
AdamF

Reputation: 2930

args pointer of the relevant struct is passed and then I need to assign it to another struct of same kind. Is it correct to assign as given below or is it required to initialize the memory for actual args.

    compute_convFWD *actual_args = args; // is this a correct assignment ?
    ... }

Yes it is correct. but your question is some how unclear.

if you are passing void*, and you are intend to operate on this pointer, then no need to allocate via malloc() any buffer, just cast or assign the void* like you did and do your function work on actual_args pointer.

if you dont want to change the original void* pointer, then you function signature should be changed to:

void *convolution(const void *args) 

And then you need to allocate memory for the copy of *args, (this could be on the stack of function convolution or on the heap. it depends on the size of the struct and your function demands. (you can copy part of *args again it depends on your convolution logic!).

for allocating on the stack of function convolution:

compute_convFWD actual_args = *((compute_convFWD*)args); // this initializing can be done with memset also

for allocating on the heap:

compute_convFWD* p_actual_args = (compute_convFWD*)malloc(sizeof(*(compute_convFWD*)args));

*p_actual_args = *((compute_convFWD*)args); // this initializing can be done with memset also

Again if you are working on few data members of the original struct, you can allocate just for the data members size that you are manipulating (no need to copy all the struct members in this case)

Upvotes: 0

vmt
vmt

Reputation: 860

To copy the struct, you should do (either)

compute_convFWD *convolute(const void *args) {
    compute_convFWD *conv = malloc(sizeof *conv);
    if (!conv) return NULL; // Check malloc
    memcpy(conv, args, sizeof *conv);
    return conv;
}

Or:

void convolute(compute_convFWD *conv, const void *args) {
    memcpy(conv, args, sizeof *conv);
}

And a cherry on top, some would call this... well, say less than pretty.

compute_convFWD convolute(const void *args) {
    compute_convFWD conv = *(const compute_convFWD *)args;
    return conv;
}

EDIT: Of course, other possibilities exist as well, but these are the most straightforward ones.

Upvotes: 3

JCWasmx86
JCWasmx86

Reputation: 3583

void* pointers are automatically converted to every other pointer type (except function pointers), you only have to initialize the memory, if the caller didn't do it.

Upvotes: 0

Related Questions