Reputation: 49
I have a problem with memory allocation in C and LVGL. The first part is the definitions.
typedef struct
{
unsigned char Widgetcount;
unsigned char index;
lv_obj_t * btn[];
}AssetRADIOBUTTON;
typedef struct{
lv_obj_t * tab;
AssetRADIOBUTTON * Radio1;
}AssetSettingsSome;
typedef struct{
lv_obj_t * ScreenMenuModule;
unsinged char radioCOUNT;
AssetSettingsSome Some;
}GUI_STRUCT_MODULES;
Now for initialization, if I call the memory allocation in subfunction, which works, in the subsubfunction with present code, it doesnt work. Code which works:
void CreateRadioButton(AssetRADIOBUTTON * Radio,lv_obj_t * tab,unsigned char RadioCount)
{
Radio->Widgetcount = RadioCount;
for(unsigned char i=0;i<RadioCount;i++)
Radio->btn[i] = lv_checkbox_create(tab);
Radio->index = 0;
}
void CreateDialog(GUI_STRUCT_MODULES * Settings)
{
Settings->radioCOUNT = 4;
Settings->Some.Radio1 = malloc(sizeof(*Settings->Some.Radio1) + Settings->radioCOUNT * sizeof(*Settings->Some.Radio1->btn));
CreateRadioButton(Settings->Some.Radio1,Settings->ECG.tab,4);
}
void main(void)
{
static GUI_STRUCT_MODULES GUI_MODULES;
CreateDialog(&GUI_MODULES);
}
Code Which doesnt work
void CreateRadioButton(AssetRADIOBUTTON * Radio,lv_obj_t * tab,unsigned char RadioCount)
{
Radio = malloc(sizeof(*Radio) + RadioCount * sizeof(*Radio->btn));
Radio->Widgetcount = RadioCount;
for(unsigned char i=0;i<RadioCount;i++)
Radio->btn[i] = lv_checkbox_create(tab);
Radio->index = 0;
}
void CreateDialog(GUI_STRUCT_MODULES * Settings)
{
CreateRadioButton(Settings->Some.Radio1,Settings->ECG.tab,4);
}
void main(void)
{
static GUI_STRUCT_MODULES GUI_MODULES;
CreateDialog(&GUI_MODULES);
}
Sorry for a bit long MVP.
Upvotes: 1
Views: 187
Reputation: 4145
Here's what is not working
void CreateRadioButton(AssetRADIOBUTTON * Radio, lv_obj_t * tab, unsigned char RadioCount)
{
Radio = malloc(sizeof(*Radio) + RadioCount * sizeof(*Radio->btn));
You are storing the address of the allocated memory in Radio
, which is a local variable. When you call CreateRadioButton(Settings->Some.Radio1...)
you're just passing a pointer whose value you don't even look at. What you need to do, is tell your function where that pointer is, so it can be modified.
So, change the function signature so that it takes a pointer to a pointer, and pass the address of the pointer you want to change:
void CreateRadioButton(AssetRADIOBUTTON ** Radio,lv_obj_t * tab,unsigned char RadioCount)
{
*Radio = malloc(sizeof(AssetRADIOBUTTON ) + RadioCount * sizeof(lv_obj_t));
(*Radio)->Widgetcount = RadioCount;
for(unsigned char i=0;i<RadioCount;i++)
(*Radio)->btn[i] = lv_checkbox_create(tab);
(*Radio)->index = 0;
}
...
CreateRadioButton(&Settings->Some.Radio1,Settings->ECG.tab,4);
Note the use of the &
operator to get the address of the Radio1
pointer, and in CreateRadioButton
the use of the *
operator to dereference the Radio
pointer-to-pointer, to get a pointer-to-AssetRADIOBUTTON
.
If this syntax is too cumbersome, consider the following alternative.
AssetRADIOBUTTON* p = *Radio;
p->Widgetcount = RadioCount;
for(unsigned char i=0;i<RadioCount;i++)
p->btn[i] = lv_checkbox_create(tab);
p->index = 0;
This creates a new variable, but with any decent compiler the resulting code will be the same.
Upvotes: 1