Struct structure will increase program overhead

Source: Internet
Author: User

Struct structure will increase program overhead

On the one hand, the struct structure can enhance the management of variables and increase the readability of the program, but on the other hand, the structure will also increase the overhead of the program.

See the following code:

struct TEST_S{int a;int b;float c;};int _tmain(int argc, _TCHAR* argv[]){TEST_S ts;int a;int b;int c;ts.a = 100;ts.b = 200;ts.c = 300.f;a = 100;b = 200;c = 300.f;return 0;}
Use disassembly tools,

ts.a = 100;ts.b = 200;ts.c = 300.f;

Assign values to the three variables of the struct. In fact, the assembly code in Debug is:

mov         dword ptr [ts],64h mov         dword ptr [ebp-0Ch],0C8h  movss       xmm0,dword ptr ds:[0BA5858h]  movss       dword ptr [ebp-8],xmm0  
From the assembly code, we can see that in addition to the first variable of the struct, the next variable needs to add an offset based on the header address of the struct, rather than the struct, as shown below:

a = 100;b = 200;c = 300.f;
The Assembly Code is as follows:

mov         dword ptr [a],64h  mov         dword ptr [b],0C8h  mov         dword ptr [c],12Ch 
Obviously, one step of address Subtraction is missing.
However, in the case of Release, the compiler optimizes the program by default. The compiled code Under optimization cannot be viewed, so it cannot identify the actual overhead.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.