I have a structure:
typedef struct mystruct_s {
uint8_t bufferA[1024];
uint8_t bufferB[1024];
} mystruct;
mystruct mystructA;
mystruct mystructB;
for( i = 0 ; i < 1024 ; i++ )
{
mystructA.bufferA[i] = 0x01;
mystructA.bufferB[i] = 0x01;
mystructB.bufferA[i] = 0x01;
}
In my program I have accessed both buffer members in mystructA but only bufferA in mystructB.
If member bufferB in mystructB is not accessed will the compiler optimize it out to save memory?
I know this may seem ambiguous due to the nature of different compilers.
For instance if i use ARM C/C++ compiler for STM32F4 under Keil what would it do?
If member bufferB in mystructB is not accessed will the compiler optimize it out to save memory?
No, the compiler is not allowed to change the memory layout of structs. You have told it to reserve 1024 bytes so it must do so. It cannot "partially optimize" a struct: it will have to either optimize away the whole struct allocation or do nothing.
This is not ambiguous or system-dependent, all standard compilers must do this. However, mystructB.bufferB
will contain garbage values.
Related to optimization:
Generally, it is not a good idea to "touch" RAM memory all over the place from several places inside the same loop. At each lap of the loop you read 3 different areas that are not adjacent. This might block the CPU from effectively using data cache, forcing it to write directly to RAM at each lap of the loop.
Compilers do generally not take data cache in account even with all optimizations enabled, since the nature of cache memory is very system-specific.
Depending on how the data cache of your MCU works, you might actually be able to significantly increase performance by creating more loops:
for( i = 0 ; i < 1024 ; i++ )
mystructA.bufferA[i] = 0x01;
for( i = 0 ; i < 1024 ; i++ )
mystructA.bufferB[i] = 0x01;
for( i = 0 ; i < 1024 ; i++ )
mystructB.bufferA[i] = 0x01;
Now the data is accessed in linear sequences which will result in optimal cache use. The order of the loops matter, it should be the same as the order of allocation.
Alternatively, use memset() since you are dealing with bytes.