zoukankan      html  css  js  c++  java
  • [C++] sizeof(struct)

    关于对结构体求sizeof。需要考虑一下几点:

    1、内存对齐

    2、机器的位数(32 or 64)

    3、是否含有虚函数

    4、继承关系

    5、static不归入sizeof

    6、普通成员函数与sizeof无关

    以32位系统为例

    Exp 1

    空类:占用1个字节。

    class Base {};
    class Base 
    {
    public:
        Base();
        ~Base();
    };
    sizeof(Base) = 1

    Exp 2

    虚函数:占用4个字节

    class Base
    {
    public:
        Base();
        virtual ~Base();
    private:
        int a;
        char *p;
    };
    sizeof(Base) = 4 + 4 + 4 = 12

    Exp 3

    继承关系 n + sizeof(Base)

    static不归入sizeof统计

    class Derive : public Base
    {
    public:
        Derive();
        ~Derive();
    private:
        static int st;
        int d;
        char *p
    };
    sizeof(Derive) = sizeof(Base) + 4 + 4 = 20

    the size of var in 32 or 64 bit system.

    32:
    char : 1
    char* : 4
    short int : 2
    int : 4
    unsigned int : 4
    float : 4
    double : 8
    long : 4
    long long : 8
    unsigned long : 4
    64:
    char : 1
    char* : 8
    short int : 4
    int : 4
    unsigned int : 4
    float : 4
    double : 8
    long : 8
    long long : 8
    unsigned long : 8
  • 相关阅读:
    tcp/ip的通俗讲述(转)
    linux中的read_link
    浅拷贝和深拷贝
    JAVA的动态代理Jdk实现方式
    友元函数
    孤儿进程、僵尸进程
    waitpid()函数
    wait()函数
    dup2函数
    exec族函数
  • 原文地址:https://www.cnblogs.com/immjc/p/8525632.html
Copyright © 2011-2022 走看看