Home > Mobile >  Problem in C with pointers, struct in struct and malloc
Problem in C with pointers, struct in struct and malloc

Time:10-01

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.

CodePudding user response:

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.

CodePudding user response:

In your not working example, it appears that you assigned the return of malloc to the wrong variable. Initializing everything and leaking memory does not seem to be the intended behavior.

Whatever it was supposed to be assigned to most likely has a wild pointer, causing whatever tries to use it to fail in an unpredictable manner.

  • Related