今天遇到一个sizeof使用失误的地方,顺便把sizeof的用法整理一下。
在msdn上关于sizeof的介绍如下:
The sizeof Operator
The sizeof operator gives the amount of storage, in bytes, required to store an object of the type of the operand. This operator allows you to avoid specifying machine-dependent data sizes in your programs.
sizeof操作符以字节形式给出了其操作数的存储大小,避免指定和机器相关的数据大小,可以提高程序的可移植性。
sizeof unary-expression
sizeof ( type-name )
sizeof(表达式) 表达式不能是位字段,不完全类型或者函数名。
sizeof(标识符)
sizeof(类型)
(搜了一下不完全类型:不完全类型指具有未知存储大小的数据类型,如未知存储大小的数组类型、未知内容的结构或联合类型、void类型等。)
Remark
The operand is either an identifier that is a unary-expression, or a type-cast expression (that is, a type specifier enclosed in parentheses). The unary-expression cannot represent a bit-field object, an incomplete type, or a function designator. The result is an unsigned integral constant. The standard header STDDEF.H defines this type as size_t.When you apply the sizeof operator to an array identifier, the result is the size of the entire array rather than the size of the pointer represented by the array identifier.
操作数是一个数组名时,结果是整个数组的大小,而不是数组名(当成指针)的大小。
例如:
(1)int arr[100];
sizeof(arr) 结果是100而不是4
(2)char string[] = "hello";
sizeof(string) 结果是 6(数组中实际上还在最后存了'/0',表示字符串的结束)
当然在这里strlen(string) 结果是 5,使用的时候不要混淆了。
When you apply the sizeof operator to a structure or union type name, or to an identifier of structure or union type, the result is the number of bytes in the structure or union, including internal and trailing padding. This size may include internal and trailing padding used to align the members of the structure or union on memory boundaries. Thus, the result may not correspond to the size calculated by adding up the storage requirements of the individual members.
如果操作数是结构或者联合的话就要考虑为了字节对齐而填充的字节,sizeof操作得到的结果是他们所占的实际空间,包括了内部数据和填充字节。这种情况下,简单地把内部成员的大小相加就会得到和sizeof不同的结果。
例如:
typedef struct{
int a;
int b;
char c;
}TEST;
sizeof(TEST) 结果是12而不是9(需要加上填充在c后的三个字节)
If an unsized array is the last element of a structure, the sizeof operator returns the size of the structure without the array.
根据上面这一段内容,试了一下
typedef struct{
int a;
int b;
int arr[];
}TEST;
sizeof(TEST) 结果是8,如果把arr放在a和b之间编译不过,问题是这么定义有什么用呢?以后留意。
buffer = calloc(100, sizeof (int) );This example uses the sizeof operator to pass the size of an int, which varies among machines, as an argument to a run-time function named calloc. The value returned by the function is stored in buffer.
static char *strings[] ={
"this is string one",
"this is string two",
"this is string three",
};
const int string_no = ( sizeof strings ) / ( sizeof strings[0] );
In this example, strings is an array of pointers to char. The number of pointers is the number of elements in the array, but is not specified. It is easy to determine the number of pointers by using the sizeof operator to calculate the number of elements in the array. The const integer value string_no is initialized to this number. Because it is a const value, string_no cannot be modified.
msdn的文档基本上已经说的很全面了,有时候费尽心思去找论坛,搜帖子,不如直接看原始的文档来的直接。
现在说说今天遇到的情况。
写了类似如下所示的一个函数:
void func(int a[]) {
int size;
size = sizeof(a);
...
}
a只是一个形参,类型为指针,所以无论传什么结果都是指针的大小4(不同机器可能不同) 。
知道函数参数传递的方式和sizeof的用法就不会出现这样的低级错误了,汗。