zoukankan      html  css  js  c++  java
  • c/c++操作符之sizeof

    1、基本数据类型

    常见的基本数据类型有void、bool、char、int、short(int)、long(int)、float、double、long long、long double等。同类型有符号与无符号所占字节数相同,故在此不做区分。

    类型 大小
    void 0 byte
    bool 1 byte
    char 1 byte
    short(int) 2 bytes
    long(int) 4 bytes
    int 4 bytes
    float 4 bytes
    double 8 bytes
    long long 8 bytes
    long double 8 bytes

    2、指针

    在32位机器下,指针的大小是4字节。

    下面有个例子,

    char* str="string";

    sizeof(str)=_______

    我们通常想到的答案是6或者是7。以为要求的是字符串的大小,但其实,str是一个字符指针,答案应该是4。

    3、字符串

    求字符串的大小时,很容易出错。因为常常会忽略字符串都是以""结尾的。

    上面那个例子,如果改为

    char str[]="string";

    sizeof(str)=_______

    那么正确答案应该是7。

    但是如果指定了字符串的长度,例如

    char str[10]="string";

    sizeof(str)=_______

    那么答案则是10。

    如果指定的字符串的长度小于字符串本身的长度,则会出错。

    char str[6]="string";

    sizeof(str)=_______

    在VS2010下,提示错误,数组界限溢出。

    4、结构体

    struct A

    {

       int a;

       char b;

       float c;

       double d;

     };

    sizeof(A)=_______

    在默认对齐方式下,答案为24。

    5、类

    class B

    {

       int b;

       B();

       ~B(); 

    };

    sizeof(B)=_______

    答案为4。计算方式与结构体相同。

    6、函数

    等于函数返回值的大小。

  • 相关阅读:
    ‘Host’ is not allowed to connect to this mysql server
    centos7安装mysql
    further configuration avilable 不见了
    Dynamic Web Module 3.0 requires Java 1.6 or newer
    hadoop启动 datanode的live node为0
    ssh远程访问失败 Centos7
    Linux 下的各种环境安装
    Centos7 安装 python2.7
    安装scala
    Centos7 安装 jdk 1.8
  • 原文地址:https://www.cnblogs.com/log-a/p/3595307.html
Copyright © 2011-2022 走看看