C + + Note memory alignment

Source: Internet
Author: User

1#include <iostream>2 3 #pragmaPack (4)4 5 structa{6 A () {}7~A () {}8     intM1;//4 bytes9     intm2; 4 bytesTen }; One structb:a{ A B () {} -~B () {} -     intM1;//4 bytes the     Charm2; -     Static Charm3; - }; - structc{ + C () {} -     Virtual~C () {}//64-bit system is 8 bytes//32-bit system is 4 bytes +     intM1; 4 bytes A      Shortm2; at }; -  - intMain () - { -Std::cout <<sizeof(A) <<Std::endl; -Std::cout <<sizeof(B) <<Std::endl; inStd::cout <<sizeof(C) <<Std::endl; -Std::cout <<sizeof(int*) <<Std::endl; to  +     return 0; -}
    • In a 32-bit system

sizeof (A) = 4 + 4 = 8 byte

sizeof (B) = sizeof (A) + 4 + 1 (requires memory alignment) = Byte

sizeof (C) = virtual function pointer size 4 + 4 + 2 (requires memory alignment) = Byte

    • In a 64-bit system

sizeof (A) = 4 + 4 = 8 byte

sizeof (B) = sizeof (A) + 4 + 1 (requires memory alignment) = Byte

sizeof (C) = virtual function pointer size 8 + 4 + 2 (requires memory alignment) = Byte

C + + Note memory alignment

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.