Foreword - I seem to have a solution, but I am not sure it is correct because I do not understand how compiler handles it. And yes, I performed research, which did not convince me in reliable and correct result.
This is C (not CPP). Language option is -std=gnu99.
Problem: I have structure
struct my_structure { uint8_t my_array[256]; };
Then I define the memory location for the struct
struct my_structure my_structure_data { .my_array[0 ... 255] = 0 };
This method is expected to have all 256 locations of my_array of the my_structure_data to be filled with zeroes. The format of the instruction is unambiguous and I would be very surprised if it would result in something else.
GCC gives me warning (pedantic mode is set deliberately)
warning: ISO C forbids specifying range of elements to initialize [-Wpedantic]
Ok, I am searching for the replacement, and found that
struct my_structure my_structure_data { .my_array = {0} };
gives no warnings. .my_array[256] = {0}; gives an error, saying I am trying to initialize 257th element of the array and it does not exist (logically).
So the question(s)
- what
.my_array = {0};actually does, the confusing for me is no index part of the array. How does compiler know that {0} is not a value for first element, but value for all elements in the array? - how portable is it? What standard version defines that behavior?
- can you propose better/more reliable way for array initialization in the static structure footprint?
Selected solution
I have chosen to explicitly initialize array. Should be problematic if array size would be bigger
struct my_structure my_structure_data { .my_array = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};
Whatever assumption there're, whatever platform I compile for, I now sure all elements are 0.
[0 ... 255]was a thing in C. There is no..or...operator in C. You would need to extent the language for that..my_array = {0}will set all elements to0but.my_array = {123}will not set all elements to123. Instead only first element is123and all remaining will hold value0.struct my_structure my_structure_data { .my_array = {0}; }is not valid initialization statement in C, because it is missing=. Either you are using some compiler extensions or some other language compiler (possibly C++?). If this is not the case, then please check that code you have posted is correct, and edit your question if it's not.memset.