About Memory Alignment __VC

Source: Internet
Author: User

http://blog.csdn.net/linlingzhao/article/details/7517953

Once took over a network video monitoring program, mainly the upper layer of software by sending control commands to obtain Network video board video data and control network video board, this should be a relatively simple process!
All the developments are going well, test time, the problem is to get the video data stream data is wrong, I confused, the entire program flow, with the debugger step by step, to send video instructions, return instructions operation error results, how?
I am the packet that is encapsulated according to the operation instruction of the video, memory display packet content is not wrong, is a night, I kept looking for "error", only by chance, I put sizeof () This instruction directly replaced, the packet all with a byte of data copy, the program successfully executed! At this time, I'm getting dizzy!
The packet size of the data sent to the network board is not at all the size of the actual control packet!
This is when I think of a man, Stanley B. Lippman, the book he wrote "Inside object Modale", has mentioned such things, the compiler in order to improve the efficiency of the CPU, the struct structure will be optimized, the use of sizeof It is possible to derive the size value of the struct structure on different computers. I have seen it before, but I did not pay any attention to it.
This is my understanding, C + + compiler in order to achieve the best CPU performance, will be optimized for the memory structure of the struct, this is to achieve the CPU data transmission bus throughput value, the data transmission bus of each computer is not the same, such as 32-bit computer data transmission value is 4 bytes, The 64-bit computer data transfer is 8 bytes, so that struct, by default, the compiler will align data to the struct structure ((32-bit) 4 multiples or (64-bit) 8, as shown in the following code:

#include

typedef struct TAGPACKAGE {

int intvalue;

Char chValue1;

Char chValue2;

} PACKAGE, * lp_package;

int main ()

{

PACKAGE PACKAGE;

Std::cout << "sizeof (Package) =" << sizeof (package) << Std::endl; 8

return 0;

}



The result of the program execution is 8 (sizeof (int) + sizeof (char) + sizeof (char) + alignment value) of 8. (My machine is 32-bit, 4*2=8) Everything is a compiler to get dizzy, but why the previous written web programs do not have this situation? The same packet transmission, I did not prevent the compilation of forced alignment, I guess it is the operating system is the same (all Winx system) is not a problem, And now the network video board is embedded Linux, the processing of packets is pure data (without the alignment of data) is sensitive, so it will appear such a problem! The solution to this situation is to prevent the compiler from aligning the packets, because when the data is aligned, the compiler will plug in some meaningless data to align the structure, and use #pragma to force the compiler to produce the specified data alignment, as shown in the following code

#pragma pack (push)

#pragma packs (1)

typedef struct TAGPACKAGE {

int intvalue;

Char chValue1;

Char chValue2;

} PACKAGE, * lp_package;

#pragma pack (POP)

typedef struct TAGPACKAGE2 {

int intvalue;

Char chValue1;

Char chValue2;

} PACKAGE2, * LP_PACKAGE2;

int main ()

{

PACKAGE PACKAGE;

PACKAGE2 Package2;

Std::cout << "sizeof (Package) =" << sizeof (package) << Std::endl; 6

Std::cout << "sizeof (Package2) =" << sizeof (Package2) << Std::endl; 8

return 0;

}



The memory is aligned when the specified packet is compiled, so that the compiler does not have to optimize its data!
Summarize:
C + + compilers always do a lot of things behind the scenes! network data processing or when dealing with embedded devices, this should be more careful!
My blog Http:\\blog.csdn.net\laiboy

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.