This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Optimization difference between C and C++

I  noticed a optimization difference between compiling a simple source code with ARM GCC in C and C++. The C++ version seems to optimize a lot less stack usage.

To demonstrate this problem, I compiled the following code with arm-none-eabi-gcc version gcc-arm-none-eabi-8-2018-q4-major-win32, first only -O2, then with -O2 -x c++ :

#include <stdio.h>
#include <stdint.h>

struct TestStruct
{
    uint32_t field1;
    uint32_t field2;
    uint32_t field3;
    uint32_t field4;
} ;

struct TestStruct initStructure(uint32_t f1, uint32_t f2, uint32_t f3, uint32_t f4)
{
    struct TestStruct myStruct;
    myStruct.field1 = f1;
    myStruct.field2 = f2;
    myStruct.field3 = f3;
    
    myStruct.field4 = f4;

    printf("Temp Address %lx", &myStruct);

    return myStruct;
}

void doStuff(struct TestStruct myStruct)
{
    printf("f1 = %d, f2 = %d, f3 = %d, f4 = %d", myStruct.field1, myStruct.field2, myStruct.field3, myStruct.field4);
}

int main(void)
{
    doStuff(initStructure(1,2,3,4));
    doStuff(initStructure(11,22,33,44));
    doStuff(initStructure(11,12,13,14));
    doStuff(initStructure(21,22,23,24));
}

Here are link to demonstrate the problem :

https://godbolt.org/z/PGCp2y Pure C

Line 44, stack usage is

sub sp, sp, #28

https://godbolt.org/z/7XfsQR C++

Line 37, stack usage is

sub sp, sp, #76
Adding multiple calls to the doStuff function in C does not change stack usage, but in C++ it does.
I am curious on why this difference in optimization exists. Is it a bug or is there C++ specification against such optimization ? Or is there an optimization option I am not aware of that would change this behavior ?
Thanks in advance