sizeof Operator/sizeof运算符

sizeof unary-expression//sizeof 一元运算符

sizeof ( type-name ) //sizeof (数据类型名)

Remarks


The result of the sizeof operatoris of type size_t, an integral type defined in the include fileSTDDEF.H. This operator allows you to avoid specifying machine-dependent datasizes in your programs.

The operand to sizeof canbe one of the following:

·        A typename. To use sizeof with a type name, the name must beenclosed in parentheses.

·        Anexpression. When used with an expression, sizeof can bespecified with or without the parentheses. The expression is not evaluated.

When the sizeof operatoris applied to an object of type char, it yields 1. When the sizeof operatoris applied to an array, it yields the total number of bytes in that array, notthe size of the pointer represented by the array identifier. To obtain the sizeof the pointer represented by the array identifier, pass it as a parameter to afunction that usessizeof. For example:

说明


sizeof操作符的结果类型是size_t,其中size_t是包含在头文件STDDEF.H中的一个整数类型定义。在程序中,sizeof操作符可以帮你具体指定与特定机器相关的数据大小。

sizeof的操作数可以是下面的类型之一:

一个类型的名称。使用sizeof和类型名来计算一个数据类型的大小时,类型名必须用括号括起来。如sizeof(type-name),这个红色的括号不能省略。

表达式。当用sizeof来计算一个表达式的大小时,这个表达式可以用也可以不用括号括起来。在进行sizeof运算中,这个表达式是没有被运算执行的。当sizeof的运算对象是char类型时,它的返回值为1(即占用一字节内存)。当sizeof作用于一个数组时,返回的值是这个数组所占用的总字节数大小,而不是指向这个数组的指针标识符的大小。为了获得指向数组的指针所占的内存字节数,可以把它作为一个参数传递给usesizeof函数来获取。实例如下:

Example


// expre_sizeof_Operator.cpp

// compile with: /EHsc

#include <iostream>

 

size_t getPtrSize( char *ptr )

{

   return sizeof(ptr );

}

using namespace std;

int main()

{

   char szHello[] ="Hello, world!";

   cout  << "The size of a char is: "

         <<sizeof( char )

         <<"\nThe length of " << szHello << " is: "

         <<sizeof szHello

         <<"\nThe size of the pointer is "

         <<getPtrSize( szHello ) << endl;

}

SampleOutput


The size of a char is: 1

The length of Hello, world! is: 14

The size of the pointer is 4

When the sizeof operatoris applied to a classstruct, or union type,the result is the number of bytes in an object of that type, plus any paddingadded to align members on word boundaries. The result does not necessarilycorrespond to the size calculated by adding the storage requirements of theindividual members. The /Zp compiler option and the pack pragma affect alignment boundariesfor members.

The sizeof operator neveryields 0, even for an empty class.

The sizeof operator cannotbe used with the following operands:

·        Functions.(However, sizeof can be applied to pointers to functions.)

·        Bitfields.

·        Undefinedclasses.

·        Thetype void.

·        Dynamicallyallocated arrays.

·        Externalarrays.

·        Incompletetypes.

·        Parenthesizednames of incomplete types.

When the sizeof operatoris applied to a reference, the result is the same as if sizeof hadbeen applied to the object itself.

If an unsized array is the last element ofa structure, the sizeof operator returns the size of thestructure without the array.

The sizeof operator isoften used to calculate the number of elements in an array using an expressionof the form:

sizeof array / sizeof array[0]

当sizeof运算符适用于类(像C++中的string 是个字符串类类型)结构联合类型时,返回值是该类型的对象的字节数,再加上字边界成员对齐所填充的字节数的总字节数。sizeof的计算结果并不一定会与单个数据成员按其数据类型大小填充进来的总字节数对应。应为编译本身,预处理pragma指令包以及字边界对齐填充都会对最终计算结果产生影响。即使对于一个空类进行运算,sizeof运算符的值不会为0。下面的操作数不能参与sizeof运算符运算。

函数(但是sizeof可以计算函数指针的大小)

位字段

未定义的类

void类型

动态分配的数组

Externalarrays

不完全类型

括号内的不完全类型的名称。

当sizeof运算符用来计算一个引用时,相当于用sizeof来运算一个对象一样。

如果把一个可变大小的数组的一个元素看作一个结构体元素,那么sizeof操作符返回的是这一个结构体元素的大小,而不是整个数组的大小。

sizeof运算符通常被用来计算一个数组中的元素个数,使用如下形式的表达:

sizeof array / sizeof array[0]


sizeof Operator  //sizeof 操作符

sizeof expression //sizeof表达式

The sizeof keyword gives the amount ofstorage, in bytes, associated with a variable or a type (including aggregatetypes). This keyword returns a value of type size_t.The expression is either anidentifier or a type-cast expression (a type specifier enclosed in parentheses).Whenapplied to a structure type or variable, sizeof returns the actual size, whichmay include padding bytes inserted for alignment. When applied to a staticallydimensioned array, sizeof

returns the size of the entire array. Thesizeof operator cannot return the size of dynamically allocated arrays orexternal arrays.

译文:关键字sizeof 是用来计算一个变量或一个类型(包括复合类型)在存储中所占用的字节数,并将这个字节数作为返回值返回。这个表达式可以是一个标识符或者是一个强制类型转换的表达式。sizeof用于计算一个结构类型或变量时,返回这个类型或变量在内存中占用的实际大小(包括因字边界对齐而填充的字节数在内)。当sizeof用于计算一个静态变量或数组时,返回整个数组所占用的大小。sizeof操作符不能返回动态分配数组和externalarrays的大小。

然后再看一下对strlen是如何定义的:

strlen

Get the length of a string.

Routine Required Header:

strlen <string.h>

size_t strlen( const char *string );

Parameter

string:Null-terminatedstring

Libraries

All versions of the C run-time libraries.

Return Value

Each of these functions returns the numberof characters in string, excluding the terminal NULL. No return value isreserved to indicate an error.

Remarks

Each of these functions returns the numberof characters in string, not including the terminating null character. wcslenis a wide-character version of strlen; the argument of wcslen is awide-character string. wcslen and strlen behave identically otherwise.

strlen()函数

获取一个字符串的长度

需要头文件<string.h>

使用方法:

size_t strlen(const char *string);

参数

string是以空字符结尾的字符串

库文件:c运行时库的所有版本

返回值

这个函数返回字符串中所有字符的个数,不包括结尾那个空字符。如果没有返回值则表示出错了。

说明

这个函数返回字符串中所有字符的个数,不包括结尾那个空字符。wcslen是strlen的宽字符版本,wcslen的参数是一个宽字符的字符串。wcslen和strlen的行为是一样的。

二、由几个例子说开去。

第一个例子

char* ss = "0123456789";

sizeof(ss) 结果4 ===》ss是指向字符串常量的字符指针

sizeof(*ss) 结果1 ===》*ss是第一个字符

char ss[] = "0123456789";

sizeof(ss) 结果11 ===》ss是数组,计算到\0位置,因此是10+1

sizeof(*ss) 结果1 ===》*ss是第一个字符

char ss[100] = "0123456789";

sizeof(ss) 结果是100===》ss表示在内存中的大小 100×1

strlen(ss) 结果是10===》strlen是个函数内部实现是用一个循环计算到\0为止之前

int ss[100] = "0123456789";

sizeof(ss) 结果400 ===》ss表示再内存中的大小 100×4

strlen(ss) 错误 ===》strlen的参数只能是char*且必须是以''\0''结尾的

char q[]="abc";

char p[]="a\n";

sizeof(q),sizeof(p),strlen(q),strlen(p);

结果是4 3 3 2     

第二个例子

class X

{

int i;

int j;

char k;

};

X x;

cout<<sizeof(X)<<endl; 结果 12 ===》内存补齐

cout<<sizeof(x)<<endl; 结果 12 同上

第三个例子

char szPath[MAX_PATH]

如果在函数内这样定义,那么sizeof(szPath)将会是MAX_PATH,但是将szPath作为虚参声明时(voidfun(char szPath[MAX_PATH])),sizeof(szPath)却会是4(指针大小)

三、sizeof深入理解。

1.sizeof操作符的结果类型是size_t,它在头文件中typedef为unsigned int类型。该类型保证能容纳实现所建立的最大对象的字节大小。

2.sizeof是算符,strlen是函数。

3.sizeof可以用类型做参数,strlen只能用char*做参数,且必须是以''\0''结尾的。sizeof还可以用函数做参数,比如:

short f();

printf("%d\n", sizeof(f()));

输出的结果是sizeof(short),即2。

4.数组做sizeof的参数不退化,传递给strlen就退化为指针了。

5.大部分编译程序 在编译的时候就把sizeof计算过了 是类型或是变量的长度这就是sizeof(x)可以用来定义数组维数的原因

char str[20]="0123456789";

int a=strlen(str); //a=10;

int b=sizeof(str); //而b=20;

6.strlen的结果要在运行的时候才能计算出来,时用来计算字符串的长度,不是类型占内存的大小。

7.sizeof后如果是类型必须加括弧,如果是变量名可以不加括弧。这是因为sizeof是个操作符不是个函数。

8.当适用了于一个结构类型时或变量,sizeof 返回实际的大小, 当适用一静态地空间数组,sizeof 归还全部数组的尺 寸。sizeof 操作符不能返回动态地被分派了的数组或外部的数组的尺寸

9.数组作为参数传给函数时传的是指针而不是数组,传递的是数组的首地址,如:

fun(char [8])

fun(char [])

都等价于fun(char *) 在C++里传递数组永远都是传递指向数组首元素的指针,编译器不知道数组的大小如果想在函数内知道数组的大小, 需要这样做:进入函数后用memcpy拷贝出来,长度由另一个形参传进去

fun(unsiged char *p1, int len)

{

 unsigned char* buf = new unsigned char[len+1]

 memcpy(buf, p1, len);

}

有关内容见:C++ PRIMER5

10.计算结构变量的大小就必须讨论数据对齐问题。为了CPU存取的速度最快(这同CPU取数操作有关,详细的介绍可以参考一些计算机原理方面的书),C++在处理数据时经常把结构变量中的成员的大小按照4或8的倍数计算,这就叫数据对齐(dataalignment)。这样做可能会浪费一些内存,但理论上速度快了。当然这样的设置会在读写一些别的应用程序生成的数据文件或交换数据时带来不便。MSVC++中的对齐设定,有时候sizeof得到的与实际不等。一般在VC++中加上#pragma pack(n)的设定即可.或者如果要按字节存储,而不进行数据对齐,可以在Options对话框中修改Advanced compiler页中的Data alignment为按字节对齐。

11.sizeof操作符不能用于函数类型,不完全类型或位字段。不完全类型指具有未知存储大小的数据类型,如未知存储大小的数组类型、未知内容的结构或联合类型、void类型等。如sizeof(max)若此时变量max定义为int max(),sizeof(char_v) 若此时char_v定义为char char_v [MAX]且MAX未知,sizeof(void)都不是正确形式

四、结束语

sizeof使用场合。

1.sizeof操作符的一个主要用途是与存储分配和I/O系统那样的例程进行通信。例如: 

void *malloc(size_t size), 

size_t fread(void * ptr,size_t size,size_t nmemb,FILE * stream)。

2.用它可以看看一类型的对象在内存中所占的单元字节。

void * memset(void * s,int c,sizeof(s))

3.在动态分配一对象时,可以让系统知道要分配多少内存。

4.便于一些类型的扩充,在windows中就有很多结构内型就有一个专用的字段是用来放该类型的字节大小。

5.由于操作数的字节数在实现时可能出现变化,建议在涉及到操作数字节大小时用sizeof来代替常量计算。

6.如果操作数是函数中的数组形参或函数类型的形参,sizeof给出其指针的大小。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值