当前位置:编程学习 > C/C++ >>

C++笔试题中非常常见的sizeof问题

sizeof 是一个操作符(operator),其作用返回一个对象或数据类型所占的内存字节数。
实例
[cpp] 
#include "stdafx.h" 
 
#include <stdlib.h> 
#include <iostream> 
using namespace std; 
void* Fun1() 

    printf("void\n"); 
    return NULL; 

char Fun2() 

    char c='c'; 
    printf("char c='c'"); 
    return c; 

int Fun3() 

    int i=2; 
    printf("int i=2"); 
    return i; 

struct strc0 
{  
}; 
struct strc00 
{  
    int getA(); 
}; 
struct strc000 
{  
    int* getA() 
    { 
        return NULL; 
    } 
}; 
struct strc1 

    int a; 
    char b; 
}; 
struct strc2 

    char b; 
    int a; 
}; 
struct strc3 

    char b; 
    int a; 
    double c; 
}; 
struct strc4 

    double c; 
    char b; 
    int a; 
}; 
struct strc5 

    char b; 
    double c; 
    int a; 
}; 
struct strc6 

    int a; 
    double c; 
    char b; 
}; 
struct strc7 

    int a; 
    double c; 
    char b; 
    char d; 
}; 
class classA0 

}; 
class classA00 

    int GetA(); 
    char GetB(); 
}; 
class classA000 

    int GetA() 
    { 
        return 1; 
    } 
    char GetB() 
    { 
        return 'c'; 
    } 
}; 
class classA1 

    int a; 
    double b; 
}; 
class classA2 

    char b; 
    double c; 
    int a; 
}; 
class classA3 

    char b; 
    double c; 
    int a; 
    char d; 
}; 
int _tmain(int argc, _TCHAR* argv[]) 
{   
    //基本数据类型 
    printf("%d\n",sizeof(char));// 1 
    printf("%d\n",sizeof(int));// printf("%d\n",sizeof(2)); 4  
    printf("%d\n",sizeof(double));// printf("%d\n",sizeof(2.12)); 8  
    printf("%d\n",sizeof(string));// 32 
 
    //函数:看其返回值,与内部申请的变量无关,且不调用函数 
    printf("%d\n",sizeof(Fun1()));// 4  //没有调用函数 
    printf("%d\n",sizeof(Fun2()));// 1 
    printf("%d\n",sizeof(Fun3()));// 4 
 
    //指针:占用内存字节数都为4 
    printf("%d\n",sizeof(void*));//4 
    printf("%d\n",sizeof(int*));// 4 
 
    //数组:其值是(下标*数据类型),当数组为参数时,则沦为了指针 
    int a1[4]; 
    char a2[]="abcd";//char a2[4]="abcd";//数组界限溢出 
    char a3[6]="abcd\0";//说明“\0”是一个字符(转义字符) 
    char a4[20]="abc"; 
    printf("%d\n",sizeof(a1));// 4*4=16 
    printf("%d\n",sizeof(a2));// 5,字符末尾还存在一个NULL的结束符 
    printf("%d\n",sizeof(a3));//6 
    printf("%d\n",sizeof(a4));//20,说明给a4分配了20个字节的内存空间,不论是否填充满 
    printf("%d\n",sizeof(a4[10]));//1,a4[4]是一个char类型的数据 
 
    //结构体,涉及到字节对齐的问题,而字节对齐的细节与编译器实现有关。 
    //结构体的总大小为结构体最宽基本类型成员大小的整数倍 
    //结构体每个成员相对于结构体首地址的偏移量(offset)都是成员大小的整数倍 
    //自己总结的:结构体的大小=最宽基本类型成员大小+其前后丈量的个数。其成员函数不占用内存 
    printf("%d\n",sizeof(strc0));//1,空的结构体占内存为1 
    printf("%d\n",sizeof(strc00));//1,在结构体内什么的函数不占内存 
    printf("%d\n",sizeof(strc000));//1,在结构体内定义的函数不占内存,不论其返回值是什么。 
    printf("%d\n",sizeof(strc1));//8,说明结构体中存在字符对齐的现象 
    printf("%d\n",sizeof(strc2));//8 
    printf("%d\n",sizeof(strc3));//不是24,而是16 
    printf("%d\n",sizeof(strc4));//不是24,而是16 
    printf("%d\n",sizeof(strc5));//不是16,而是24 
    printf("%d\n",sizeof(strc6));//不是16,而是24 
    printf("%d\n",sizeof(strc7));//24 
  
    //类:与结构体一样。类的大小=最宽基本类型成员大小+其前后丈量的个数。且成员函数不占用内存 
    printf("%d\n",sizeof(classA0));//1 
    printf("%d\n",sizeof(classA00));//1,说明申明的函数不占内存 
    printf("%d\n",sizeof(classA000));//1,说明定义的函数不占内存 
    printf("%d\n",sizeof(classA1));//16,说明类中也存在字符对齐的现象 
    printf("%d\n",sizeof(classA2));//24 
    printf("%d\n",sizeof(classA3));//24 
 
    system("pause"); 
    return 0; 

最后总结:
类/结构体的大小=最宽成员类型+其前后丈量的个数。且成员函数不占用内存,不论其是否有返回值。
补充:软件开发 , C++ ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,