sizeof Data alignment Issues

Source: Internet
Author: User

#include <iostream>using namespacestd;structa1{intA; Static intb; };structa2{intA; Charc;};structa3{floatA; Charc;};structa4{floatA; intb; Charc;};structa5{DoubleD; floatA; intb; Charc;};voidMain () {cout<<sizeof(A1) <<Endl; cout<<sizeof(A2) <<Endl; cout<<sizeof(A3) <<Endl; cout<<sizeof(A4) <<Endl; cout<<sizeof(A5) <<Endl;}

The analysis is as follows:

A1:sizeof only calculates the size allocated in the stack, and B is a static variable, which is located in the global data area, so it does not occupy the space of the stack, so the size of A1 is only the space occupied by a, that is, 4;

A2: Data alignment, int size is 4;char size is 1, extension is 4, so A2 is 8;

A3: Data alignment: Float size is 4;char size 1, extension is 4, so A3 is 8;

A4: Data alignment: Float size is 4;int size is 4;char size is 1, extension is 4, so A3 is 12;

A5: Data alignment: Double size for 8;float size of 4,int size for 4,float and int for 8;char size of 1, consider alignment, expand to 8; so A4 is 24;

Reprinted from: http://blog.csdn.net/tangbo1987/article/details/6766918

sizeof Data alignment Issues

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.