I come, I see, I conquer

                    —Gaius Julius Caesar

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::


VC++6.0环境下的基本数据类型大小:

size of bool: 1 bytes
size of char: 1 bytes
size of unsigned char:  1 bytes
size of wchar_t: 2 bytes
size of short: 2 bytes
size of int: 4 bytes
size of unsigned int/unsigned: 4 bytes
size of size_t: 4 bytes
size of long: 4 bytes
size of unsigned long: 4 bytes 
size of float: 4 bytes
size of double: 8 bytes
size of long double: 8 bytes


1. What is the output of the following code?

#include <iostream>
using namespace std;

struct {
} A1;

struct {
    
short a1;
    
short a2;
    
short a3;
} A2;

struct{
    
long a1;
    
short a2;
} A3;

struct A4 {
    
char a, b;
    
int c;
};

int main()
{
    cout
<< sizeof(A1) <<endl; //1 bytes
    cout<< sizeof(A2) <<endl; //6 bytes
    cout<< sizeof(A3) <<endl; //8 bytes
    cout<< sizeof(A4) <<endl; //8 bytes

    
return 0;
}



2. 以下代码的输出结果是什么?

#include <iostream>
using namespace std;

class C1 {
};

class C2 {
    
char a, b;
};

class C3 {
public:
    
int a;
    
static int b;
    C3();
    
~C3();
};

class C4 {
public:
    
int a;
    
char b;
    C4();
    
~C4();
};

class C5 {
public:
    
short a, b, c, d;
    
char e; 
};

class C6 {
public:
    
float a;
    
int b;
    
char c;
};

class C7 {
public:
    
double a;
    
float b;
    
int c;
    
char d;
};

int main()
{
    cout
<< sizeof(C1) <<endl; //1 bytes
    cout<< sizeof(C2) <<endl; //2 bytes
    cout<< sizeof(C3) <<endl; //4 bytes
    cout<< sizeof(C4) <<endl; //8 bytes
    cout<< sizeof(C5) <<endl; //10 bytes
    cout<< sizeof(C6) <<endl; //12 bytes
    cout<< sizeof(C7) <<endl; //24 bytes

    
return 0;
}


3. 一个空类占多少空间?多重继承的空类呢?

#include <iostream>
using namespace std;

class A
{

};

class A2
{

};

class B : public A
{

};

class C : public virtual B
{

};

class D : public A, public A2
{

};

int main()
{
    cout
<< sizeof(A) <<endl; //1 bytes
    cout<< sizeof(B) <<endl; //1 bytes
    cout<< sizeof(C) <<endl; //4 bytes
    cout<< sizeof(D) <<endl; //1 bytes

    
return 0;
}



4. 写出以下代码的输出结果?

#include <stdio.h>

/* 测试用函数作sizeof的参数 */
int f1();
short f2();
void f3()
{
    
char a, b, c;
    
int x, y, z;
}

/* 数组退化为指针 */
void test(int x[], char y[])
{
    printf(
"%d \n"sizeof(x)); //4
    printf("%d \n"sizeof(y)); //4
}

int main()
{
    
int x[10]={0};
    
char y[10]="sizeof";
    
void *p;
    
int **z[2][3];
    
int (*q)[3];
    
int (*p2)(int,int);

    test(x, y);

    
/* sizeof计算数组的大小 */
    printf(
"%d \n"sizeof(x)); //40
    printf("%d \n"sizeof(y)); //10

    printf(
"%d \n"sizeof(f1())); //4
    printf("%d \n"sizeof(f2())); //2
    
//printf("%d \n", sizeof(f3())); /* sizeof不能用于无返回值函数 */

    printf(
"%d \n"sizeof(p)); //4
    printf("%d \n"sizeof(z)); //24
    printf("%d \n"sizeof(q)); //4
    printf("%d \n"sizeof(p2)); //2

    
return 0;
}



5. 以下代码的输出结果是多少?

#include <stdio.h>

struct A 

    
char m:4;
};

struct B 

    
char m:4;
    
char n:4;
};

//#pragma pack(1)
struct C 

    
char m:4;
    
char n:4;
    unsigned 
short i:8;
    unsigned 
long j;
};

struct D 

    unsigned 
short i:8;
    unsigned 
long j;
};

main()
{
    printf(
"sizeof(A)=%d \n"sizeof(A)); //sizeof(A)=1
    printf("sizeof(B)=%d \n"sizeof(B)); //sizeof(B)=1
    printf("sizeof(C)=%d \n"sizeof(C)); //sizeof(C)=8
    printf("sizeof(D)=%d \n"sizeof(D)); //sizeof(D)=8


注意:
(1) sizeof操作符的结果类型为size_t,它在头文件中的typedef为unsigned int类型。
(2) sizeof是运算符,strlen是函数。
(3) sizeof可以用类型做参数,strlen只能用char *做参数,且必须以'\0'结尾。sizeof还可以用函数做参数。
(4) 数组做sizeof的参数不退化,传递给strlen就退化为指针。
(5) sizeof操作符不能返回被动态分配的数组或外部的数组的尺寸。
(6) 数组作为参数传递给函数时传递的是指针而不是数组,传递的是数组的首地址。如:fun(char [])等价于fun(char *)。在C++里传递数组永远都是传递指向数组首元素的指针,编译器不知道数组的大小。如果想在函数内知道数组的大小,需要在进入函数后用memcpy将数组拷贝出来,长度由另一个形参传进去。如:

fun(unsigned char *p, int len)
{
    unsigned 
char *buf = new unsigned char[len+1];
    memcpy(buf, p, len);
}


(7) 计算结构变量的大小就必须讨论数据对齐问题。为了使CPU存取的速度最快,C++在处理数据时经常把结构变量中的成员的大小按4或8的倍数计算,这就叫数据对齐(data alignment)。这样做可能会浪费一些内存,但在理论上CPU速度快了。
(8) sizeof操作符不能用于函数类型、不完全类型或位字段。不完全类型指具有未知存储大小数据的数据类型,如未知存储大小的数组类型、未知内容的结构或联合类型、void类型等。

posted on 2008-10-12 09:08  jcsu  阅读(490)  评论(0编辑  收藏  举报