跳至主要内容

【转】深入理解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...

【转帖】berkeleyDB安装

我们以redhat9为例开始介绍其安装过程: 从其sleepycat公司官网的下载页 http://dev.sleepycat.com/downloads/releasehistorybdb.html 获得其安装包,现在已经更新到4.4.20.   #tar zxfv db-4.x.tgz   #cd db-4.x/build_unix   1. 如果以gcc编译的话,进行以下操作:   #vi /dist/configure 在最前面添加:CC gcc   #../dist/configure   #make   #make install   默认状态,berkeleyDB的lib和include将被安装到/usr/local/BerkeleyDB/下,需要更改这个路径的话,可以将 #../dist/configure一步加上选项--prefix,例如:#../dist/configure --prefix=/opt/BerkeleyDB.   #vi /etc/ld.so.conf 并将berkeleyDB的lib路径加到该文件的最后一行,这样才能找到并加载它的库文件.ld.so.conf是什么东西?它就是系统动态链接库的配置文件。此文件内,存放着可被LINUX共享的动态链接库所在目录的名字(系统目录/lib,/usr/lib除外),各个目录名间以空白字符(空格,换行等)或冒号或逗号分隔。   #ldconfig   2. 如果以armgcc编译的话,进行以下操作:   先安装armgcc,对我说就是在/mnt/setup/program/FFT/FFT-2410光盘-V3.0/linux开发/linux交叉编译器/tool或者在gmail中附件里armgcc目录下(除了cross-armv4l-qtE-custom-1.5-3mz.i386.rpm,其他都装)   #cp /mnt/setup/program/FFT/FFT-2410光盘-V3.0/linux开发/linux交叉编译器/tool /usr/tmp/   #cd /usr/tmp/tool   #rpm -ivh *   #ldc...