在 C 中使用 sizeof() 运算符时的奇怪情况
我正在使用以下代码测试 C/C++ 中 sizeof 运算符的使用:
#include <ctype.h> /* Character types */
#include <stdio.h> /* Standard buffered input/output */
#include <stdlib.h> /* Standard library functions */
#include <string.h> /* String operations */
#include <sys/types.h> /* Data types */
#include <sys/wait.h> /* Declarations for waiting */
#include <unistd.h>
void main()
{
char a[100];
char *des = malloc(100*sizeof(char));
strcpy(des,"abcded\0");
printf("%d\n",sizeof(des));
printf("%d\n",sizeof(a));
free(des);
}
为什么此程序输出:
4
100
与以下内容相反:
100
100
I was testing the use of sizeof operator in C/C++ with this code:
#include <ctype.h> /* Character types */
#include <stdio.h> /* Standard buffered input/output */
#include <stdlib.h> /* Standard library functions */
#include <string.h> /* String operations */
#include <sys/types.h> /* Data types */
#include <sys/wait.h> /* Declarations for waiting */
#include <unistd.h>
void main()
{
char a[100];
char *des = malloc(100*sizeof(char));
strcpy(des,"abcded\0");
printf("%d\n",sizeof(des));
printf("%d\n",sizeof(a));
free(des);
}
Why does this program output:
4
100
As opposed to:
100
100
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
因为,尽管有很多人断言,指针和数组在 C 语言和 C 语言中并不是 100% 可以互换的。 C++。这是差异之一的明显例子。
指针的大小为 4 字节(或任何特定于平台的大小),无论它指向多少分配的内存。
数组的大小就是数组的大小。
Because, despite the assertions of many people, pointers and arrays are NOT 100% interchangeable in C & C++. This is a clear example of one of the differences.
The size of a pointer is 4-bytes (or whatever platform specific size), regardless of how much allocated memory it may point to.
The size of an array is the size of the array.
sizeof(des)
返回指针的大小 - 在您的系统上为 4。它不返回它指向的已分配空间的大小。sizeof(des)
returns the size of the pointer - which is 4 on your system. It doesn't return the size of the allocated space it points to.等价于以下内容:
换句话说,
sizeof
是一个运算符,它对您传递给它的表达式的类型进行操作。因此,当您编写sizeof(des)
时,表达式的 type 为char*
,因此它会打印sizeof(char*)< /code> 在您的系统上是
4
。但在sizeof(a)
的情况下,表达式的类型为char[100]
,因此它会打印sizeof(char[100])
即100
。这里讨论了
sizeof
的一个更有趣的案例:is equivalent to the following:
In other words,
sizeof
is an operator which operates on the type of the expression which you passed to it. So when you writesizeof(des)
the type of the expression ischar*
, hence it printssizeof(char*)
which is4
on your system. But in case ofsizeof(a)
, the type of the expression ischar[100]
, hence it printssizeof(char[100])
which is100
.A more interesting case with
sizeof
is discussed here:因为它返回指向已分配内存的指针des的大小。
Because it returns the size of the pointer des which points to the allocated memory.
sizeof
运算符产生编号。类型的字节数。sizeof des
给出 100,因为des
的类型是char[100]
,一个 char 总是占用 1 个字节,而且有 100 个字节。sizeof a
给出 4,因为a
的类型是char*
,并且 char 指针在您的平台上占用 4 个字节(这是典型的32 位平台。)因此,sizeof 仅适用于其操作数的类型(注意,sizeof 是一个运算符,它不是一个函数/宏)。 sizeof 不与 malloc 交互,并且
a
指向 100 个动态分配字节的开头这一事实是无关紧要的。The
sizeof
operator yields the no. of bytes for a type.sizeof des
gives 100, because the type ofdes
is achar[100]
, a char always takes 1 byte, and there's 100 of them.sizeof a
gives 4 because the type ofa
is achar*
, and a char pointer takes 4 bytes on your platform (which is typical on 32 bit platforms.)So, sizeof just works on the type of it's operand (note, sizeof is an operator, it is not a function/macro). sizeof does not interact with malloc, and the fact that
a
points to the start of 100 dynamically allocated bytes is irrelevant.我将稍微详细说明阿贝伦基的评论。您期望这两个 typedef 产生等效的类型吗?
这两个怎么样?
对于这两种情况,你的答案显然是否定的。是的,您可以编译
des=a
,因为a
生成一个指针,但a
实际上是一个数组。特别是,您还知道最后一个 typedef 没有为指针保留内存。因此,
a
大致具有char * const
类型,这意味着a
是指向非常量字符的常量指针,因此a=des
给出类型错误。我不知道所说的错误消息是否会抱怨数组、const 或左值。I'll elaborate slightly upon abelenky's comment. Do you expect these two typedefs to product equivalent types?
How about these two?
Your answer is clearly no in both cases. Yes, you could compile
des=a
becausea
yields a pointer, buta
is actually an array.In particular, you also know this last typedef reserves no memory for a pointer. As a result,
a
has roughly the typechar * const
meaninga
is a constant pointer to non-constant characters, and thusa=des
gives a type error. I donno if said error message will complain about arrays,const
, or an lvalue though.答案很简单。
des 的类型是“char *”或字符的地址。因此,您使用 sizeof 向编译器询问的问题是“我需要多少内存(以字节为单位)来存储字符的地址?”编译器响应 4,因为您的程序和编译器具有 32 位(4 字节)寻址。当您在数组上使用 sizeof 时,您会问一个不同的问题,即“我需要多少内存(以字节为单位)来存储包含 100 个项目的字符数组?”这里的答案是 100 个项目 * 1 字节 = 100 字节。
The answer to this is very simple.
The type of des is "char *" or the address of a character. So the question you are asking the compiler by using sizeof is "How much memory (in bytes) do I need to store the address of a character?" The compiler responds 4 because your program and compiler has 32bit (4 bytes) addressing. When you use sizeof on an array you are asking a different question which is "How much memory (in bytes) do I need to store a character array of 100 items?" The answer here is 100 items * 1 byte = 100 bytes.