0

I want to generate asserts at compile time, checking whether each element within a struct is initialized or not. I was wondering how I could generate a for loop of such static assert statements at compile time given that the assert is not a preprocessor command.

The issue was that uninitialized elements within such a struct of constants was causing undefined behavior in the system.

Tried modifying GCC compiler flags -Wuninitialized or -Wmaybe-uninitialized do not detect when an element within an array of structs is not filled.

Followed the following thread Static assert in C

_Static_assert(fl_Button_List_SA[0].Button_constants_S.Button_Pressed_output != unintializedVariable.Button_constants_S.Button_Pressed_output ,"Struct is uninitialized.");

the fl_Button_List_SA is the array of structs. the Button_constants_S is a struct containing constants only. Button_pressed_output is an element defined as constant containing the output when the button is pressed.

unintializedVariable is a struct of the same type which was kept uninitialized.

The above assertion is currently returning the following error ' error: expression in static assertion is not constant ' but I will manage to get past this. However for more info the button struct inside the array is consisting of a struct containing two structs one containing only constants and the other containing only variables. And given that the structs themselves aren't constants this must be why this issue is coming up.

I would just like to generate code that goes through all the elements of each struct inside the array and checks that the elements are initialized.

As an example of an initialization:

//struct definition
typedef struct Button_properties_t
{
 Button_constants Button_constants_S;  
 Button_variables Button_variables_S;  
} Button_properties;  


Button_properties Button_list[numberOfButtons] =   
{
    [0].Button_constants_S.pressed_output=2u, 
 [0].Button_constants_S.DTC_number=First_Button, //Enum  
...
...

}

What I am expecting is a rust like compile time error that if for example during initialization of button number [100] the programmer fails to define a DTC number for that button a compile time error is raised.

[100].Button_constants_S.pressed_output = 4u,
//[100].Button_constants_S.DTC_number uninitialized 
...
...
[101].Button_constants_S.pressed_output = 5u,
[101].Button_constants_S.DTC_number = Fifth_Button, //Enum
...
Timbino
  • 1
  • 2
  • 1
    Please post an example of initialization. If one array element is not explicitly initialized, it is implicitly initialized with zeros. – KamilCuk Jan 04 '23 at 16:31
  • 1
    Comparing something against something else that wasn't initialized doesn't make sense. Please show a _minimal_ example of code you'd want to fail a build, your description of structures within structures is confusing. – Mat Jan 04 '23 at 16:38
  • *"I would just like to generate code that goes through all the elements of each struct inside the array and checks that the elements are initialized."* You can't do this I'm afraid. Uninitialized variable value is not determined so you can't compare to it (you can but the result of the comparison is undefined). – 0___________ Jan 04 '23 at 16:38
  • If an uninitialized variable has a determinable value then it would be initialized. – 0___________ Jan 04 '23 at 16:41
  • I can't compile your code. What are `Button_variables` and `Button_constants` and `numberOfButtons`? `error: expression in static assertion is not constant ' but I will manage to get past this` How will you manage to get past this? Overall, I believe what you are asking is not possible. You can't detect an uninitialized variable. Accessing array is not a constant expression. You would have to rewrite your code to macros and do a lot of macro fencing. Or use C++. – KamilCuk Jan 05 '23 at 08:28
  • Button variables and button constants structs which aren't defined in the code snippet I'm providing. It should be clear that the code I am providing isn't compilable. I'm not facing a compilation error, I want to induce an error in case the programmer fails to initialize an element within a struct. In this example I'm proving Button_properties to express how this array is being initialized and the scenario where the compilation should fail is when an element is uninitialized. – Timbino Jan 05 '23 at 08:46
  • Just create a unit test to check and be done with it. – Andrew Henle Jan 05 '23 at 23:32
  • Thanks @AndrewHenle that's acceptable. – Timbino Jan 13 '23 at 07:44

0 Answers0