Problem in C with pointers, struct in struct and malloc

vahid ajal

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.

Tim Randall

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Problem with setting the array of pointers in the struct using malloc

From Dev

Pointers - Casting as Struct Pointer + Malloc

From Dev

nested struct in c, problem with addressing pointers

From Dev

pointers to struct with malloc-expression expected error C

From

malloc for struct and pointer in C

From Dev

c malloc array of struct

From Dev

Trouble with struct malloc in C

From Dev

Using malloc with struct in C

From Dev

Struct and Pointers C++

From Dev

malloc with C struct in C++

From Dev

c++ malloc for array in struct

From Dev

C - malloc in a loop with struct with pointer

From Dev

malloc sizeof a typedef struct in C

From Dev

How to correctly malloc a struct in C

From Dev

C malloc segmentation fault struct

From Dev

C programming. Pointers to struct

From Dev

Array assignment with struct pointers in C

From Dev

Pointers to a struct inside a struct

From Dev

Problem entering values to a struct with double pointers

From Dev

C++ into C# pointers to a struct inside a struct of same type

From Dev

Malloc 'ing array of struct in struct

From Java

In c accessing first element of the struct using address of the struct and pointers

From Dev

Struct node having a array of pointers to struct node in C

From Dev

Typedef struct pointers correctly pointing to typedef struct (C)

From Dev

Can not declare array of pointers to struct inside struct in C

From Dev

Beginner to C - Malloc Not Required For String In Struct

From Dev

Generate code for nested struct pointers in C

From Dev

c copy struct with pointers from stack to heap

From Dev

Allocate and Delete memory for pointers in a struct C++

Related Related

HotTag

Archive