跳至主要内容

【转】深入理解sizeof

[本文中int占4字节,short占2字节]

1.0 回答下列问题:[答案在文章末尾]

1. sizeof(char) =                           

2. sizeof 'a'   =                           

3. sizeof "a"   =                        

4. strlen("a")) =

  如果你答对了全部四道题,那么你可以不用细看下面关于sizeof的论述。如果你答错了部分题目,那么就跟着我来一起探讨关于sizeof的用法了。  

  对于前面的题目,我想一般有一定C基础的同志应该不会答错1和4题。至于第2题,我想应该要清楚sizeof是求字符串所占的内存。"a"在内存中的表现为a\0,别忘了末尾的\0也占一个字节呢。至于第2题,可能有些人会惊讶了。C 语言中,字符常数是int 型, 因此 sizeof('a') 是 sizeof(int), 这是另一个与 C++ 不同的地方。既然字符常数是int 型,那么int就可以存放4个字符,我们可以得到sizeof 'abcd'为 4。  

1.1 回答以下题目[答案在文章末尾]

short (*ptr[100])[200];

1. sizeof(ptr)           =

2. sizeof(ptr[0])        =

3. sizeof(*ptr[0])       =

4. sizeof((*ptr[0])[0])) =   

  是不是又开始晕了。这里我们定义了一个100个指针数组,每个指针均指向有200个元素的数组,其内存占用为200*sizeof(short)字节。那么这100个数组指针的大小sizeof(ptr)为100*sizeof(short*)。接着,指针数组的第一个指针ptr[0]指向第一个数组,所以这个指针ptr[0]的大小实际上就是一个普通指针的大小,即sizeof(short*)。*ptr[0]指向第一个数组的起始地址,所以sizeof(*ptr[0])实际上求的是第一个组的内存大小200*sizeof(short)。(*ptr[0])[0])是第一个数组的第一个元素,因为是short型,所以这个元素的大小sizeof((*ptr[0])[0]))等价于sizeof(short)。

1.2 回答以下题目[答案在文章末尾]

#include <stdio.h>

#pragma pack(push)

#pragma pack(2)

typedef struct _fruit
{
  char          apple;
  int           banana;
  short         orange;  
  double        watermelon;
  unsigned int  plum:5;
  unsigned int  peach:28;
  char*         tomato;
  struct fruit* next;    
} fruit;

#pragma pack(4)
 
typedef struct _fruit2
{
  char           apple;
  int            banana;  
  short          orange;
  double         watermelon;
  unsigned int   plum:5;
  unsigned int   peach:28;  
  char*          tomato;
  struct fruit2* next;    
} fruit2; 

#pragma pack(pop)

int main(int argc, char *argv[])
{
  printf("fruit=%d,fruit2=%d\n",sizeof(fruit),sizeof(fruit2));
}

问题:打印结果为什么呢?

如果你回答错误,那么你对数据结构的对齐还没有吃透。这里#pragma pack(2)强制设置编译器对齐属性为2,所以第一个数据结构以2对齐,sizeof(fruit)=(sizeof(apple)+1)+sizeof(banana)+sizeof(orange)+sizeof(watermelon)+((plum:5bit+peach:28bit+15bit)/8bit)+sizeof(tomato)+sizeof(next)(注意式子中1 和 15bit 表示补齐内存,使其以2对齐,),既sizeof(fruit)=(sizeof(char)+1)+sizeof(int)+sizeof(short)+sizeof(double)+sizeof(char*)+sizeof(struct fruit*)。第一个数据结构声明完了之后,又使用#pragma pack(4)强制设置编译器对齐属性为4,所以同理,可以得到sizeof(fruit2)=(sizeof(char)+3)+sizeof(int)+(sizeof(short)+2)+sizeof(double)+((5bit+28bit+31bit)/8bit)+sizeof(char*)+sizeof(struct fruit2*)。

注:#pragma pack(push)保存默认对齐,#pragma pack(pop)恢复默认对齐。

----------------------------------------答案:

1.0: 1,4,2,1

1.1: 400,4,400,2

1.2: fruit=30,fruit2=36

评论

此博客中的热门博文

【转】smb协议栈使用示例

/*  * * uncdownload.c  * *  * * Utility for downloading files from SMB shares using libsmbclient  * *  * * Copyright(C) 2006 Sophos Plc, Oxford, England.  * *  * * This program is free software; you can redistribute it and/or modify it under the terms of the  * * GNU General Public License Version 2 as published by the Free Software Foundation.  * *  * * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without  * * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  * * See the GNU General Public License for more details.  * *  * * You should have received a copy of the GNU General Public License along with this program; if not,  * * write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA  * *  * */ # include < sys / types . h > # include < sys / time . h > # include ...

【转】Ether Types

Ether Types (last updated 2008-09-09) NOTE: Please see [RFC5342] for current information and registration procedures. This registry will be revised soon and will be replaced with up-to-date information. Many of the networks of all classes are Ethernets (10Mb) or Experimental Ethernets (3Mb). These systems use a message "type" field in much the same way the ARPANET uses the "link" field. If you need an Ether Type, contact: IEEE Registration Authority IEEE Standards Department 445 Hoes Lane Piscataway, NJ 08854 Phone +1 732 562 3813 Fax: +1 732 562 1571 Email: <ieee-registration-authority& ieee.org > http://standards.ieee.org/regauth/index.html The following list of EtherTypes is contributed unverified information from various sources. Another list of EtherTypes is maintained by Michael A. Patton and is accessible at: <URL: http://www.cavebear.com/CaveBear/Ethernet/ > <URL: ftp://ftp.cavebear.com/pub/Ethernet-codes > Assign...

【转】移植Linux的应用软件到Windows

一.前言     Linux 拥有丰富各种源代码资源,但是大部分代码在 Windows 平台情况是无法正常编译的。 Windows 平台根本无法直接利用这些源代码资源。如果想要使用完整的代码,就要做移植工作。因为C/C++ Library的不同和其他的一些原因,移植C/C++代码是一项困难的工作。本文将以一个实际的例子(Tar)来说明如何把 Linux 代码移植到 Windows 平台上。移植过程将尽量少修改代码,以便代码的运行逻辑不会发生任何变动。保留绝大部分软件主要功能。    二.准备工作    Tar是 Linux 平台下面一个打包工具。移植这样一个程序到 Windows 平台需要做那些工作呢?    首先是一些准备工作,在 Windows 平台上面安装上Cygwin的最新版本,在Cygwin中安装好GCC等开发工具。 同样也需要一个 Windows 开发环境。可以使用最新版本Visual Studio, Microsoft Visual Studio .NET 2003。从 www.gnu.org 上取得Tar的最新源代码,版本是1.13。在Cygwin下面解开tar-1.13.tar.gz.源代码包。注意请不要在 Windows 下面使用WINRAR或者WINZIP来解压缩。 WINRAR和WINZIP在解压缩某些tar.gz包的时候会有问题。使得解包之后的目录和文件出现异常。如果是源代码包将有可能不能在Cygwin下面正确编译。解开压缩包之后,进入 tar-1.13目录,在当前的目录下面输入./configure命令,运行完毕之后,再次输入make命令。开始编译tar的Cygwin版本。编译基本上不会有问题,进入src目录,可以看到新编译好的Tar程序tar.exe。    Cygwin是一个API层的 Linux 模拟环境。如果能够在Cygwin下面编译,运行。实际上也就是能在 Windows 下面编译和运行,只是需要有一层中间API模拟某些 Linux 特有的操作。简单的判断一个 Linux 程序能不能移植到 Windows 平台下面,就是看是否能在Cygwin下面编译源代码,并运行程序。...