相关疑难解决方法(0)

27
推荐指数
4
解决办法
11万
查看次数

结构的内存大小不同?

为什么第一种情况下不是12?测试:最新版本的gcc和clang,64位Linux

struct desc
{
    int** parts;
    int nr;
};
Run Code Online (Sandbox Code Playgroud)

sizeof(desc); Output: 16

struct desc
{
    int** parts;
};
Run Code Online (Sandbox Code Playgroud)

sizeof(desc); Output: 8

struct desc
{
    int nr;
};
Run Code Online (Sandbox Code Playgroud)

sizeof(desc); Output: 4

c memory memory-management

3
推荐指数
1
解决办法
783
查看次数

C/C++中的对象/结构对齐

#include <iostream>

using namespace std;

struct test
{
    int i;
    double h;
    int j;
};

int main()
{
    test te;
    te.i = 5;
    te.h = 6.5;
    te.j = 10;

    cout << "size of an int: " << sizeof(int) << endl; // Should be 4
    cout << "size of a double: " << sizeof(double) << endl; //Should be 8
    cout << "size of test: " << sizeof(test) << endl; // Should be 24 (word size of 8 for double)

    //These two …
Run Code Online (Sandbox Code Playgroud)

c++ g++

1
推荐指数
1
解决办法
491
查看次数

标签 统计

c ×2

c++ ×1

g++ ×1

memory ×1

memory-management ×1

sizeof ×1

struct ×1