Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

converting from struct to float*

I have a struct

typedef struct
{
    float   m[4][4];
} myMatrix;

because of some need of the program I need to convert this to float*

I do it something like

if(! g_Fvar16)
    g_Fvar16 = (float*)malloc(sizeof(float) * 16);
memcpy(&g_Fvar16, &struct_var, sizeof(float)*16);
return g_Fvar16;

this is one simple function. Now, from where I call this function, program crashes on accessing these values. g_Fvar16 is float*

sizeof(struct_var) is 64 and so is the amount of memory allocated.

Can't I simply treat the copied memory as float*? I thougt this would be the fastest..

like image 379
Adorn Avatar asked Mar 22 '23 12:03

Adorn


1 Answers

g_fVer16 is already a pointer, so you have to write

memcpy(g_Fvar16, &struct_var, sizeof(float)*16);

instead of

memcpy(&g_Fvar16, &struct_var, sizeof(float)*16);

(note the first &)

like image 73
ensc Avatar answered Mar 28 '23 05:03

ensc