I work with embedded stuff, so, I use special C compiler C30 for MCUs. I'm confused about its behavior with temporary structures.
But, maybe I don't know something, and it's intended behavior?
Consider little code:
typedef struct {
int a;
int b;
} T_TestStruct1;
void test1(T_TestStruct1 *p_test_struct)
{
/* do something */
}
And these calls to test1()
:
void some_function()
{
{
T_TestStruct1 par = {
.a = 1,
.b = 1,
};
test1(&par);
}
{
T_TestStruct1 par = {
.a = 2,
.b = 2,
};
test1(&par);
}
}
Everything is OK here: just one instance of T_TestStruct1
is allocated in the stack. But I like to use shorter expressions:
void some_function()
{
test1(&(T_TestStruct1){
.a = 1,
.b = 1,
});
test1(&(T_TestStruct1){
.a = 2,
.b = 2,
});
}
Then, both structures {.a = 1, .b = 1}
and {.a = 2, .b = 2}
are allocated in the stack. But, in my opninion, they should not be. Only one instance is actually needed.
Just by chance, I tried that:
void some_function()
{
{
test1(&(T_TestStruct1){
.a = 1,
.b = 1,
});
}
{
test1(&(T_TestStruct1){
.a = 2,
.b = 2,
});
}
}
Result is the same.
So, is it intended behavor, or is it a bug in the compiler?