Cpp类型字节数

来自个人维基
跳转至: 导航搜索
#include <stdio.h>
#include <iostream>

class A{
};

using namespace std;

int main(int argc, char *argv[]) {
    A a;
    printf("empty a size: %ld \n",sizeof(a));
 
    cout<<"short:"<<sizeof(short)<<endl;
    cout<<"int:"<<sizeof(int)<<endl;
    cout<<"unsigned int:"<<sizeof(unsigned int)<<endl;
    cout<<"long:"<<sizeof(long)<<endl;
    cout<<"char:"<<sizeof(char)<<endl;
    cout<<"float:"<<sizeof(float)<<endl;    
    cout<<"double:"<<sizeof(double)<<endl;
    return 0;
}

gcc编译 [gcc版本为gcc (Ubuntu/Linaro 4.4.5-15ubuntu1) 4.4.5, linux版本为 2.6.38.2 x86_64]:

$:gcc -o temp temp.cpp -lstdc++ 

执行结果:

empty a size: 1 
short:2
int:4
unsigned int:4
long:8
char:1
float:4
double:8