在 C 中使用 sizeof() 运算符时的奇怪情况

发布于 2024-12-08 23:53:35 字数 858 浏览 0 评论 0原文

我正在使用以下代码测试 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

虫児飞 2024-12-15 23:53:35

因为,尽管有很多人断言,指针和数组在 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.

久而酒知 2024-12-15 23:53:35

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.

春庭雪 2024-12-15 23:53:35
printf("%d\n",sizeof(des));
printf("%d\n",sizeof(a));

等价于以下内容:

printf("%d\n",sizeof(char*));      //prints size of a pointer
printf("%d\n",sizeof(char[100]));  //size of a char array of size 100

换句话说,sizeof 是一个运算符,它对您传递给它的表达式的类型进行操作。因此,当您编写 sizeof(des) 时,表达式的 typechar*,因此它会打印 sizeof(char*)< /code> 在您的系统上是 4。但在 sizeof(a) 的情况下,表达式的类型为 char[100],因此它会打印 sizeof(char[100])100

这里讨论了 sizeof 的一个更有趣的案例:

printf("%d\n",sizeof(des));
printf("%d\n",sizeof(a));

is equivalent to the following:

printf("%d\n",sizeof(char*));      //prints size of a pointer
printf("%d\n",sizeof(char[100]));  //size of a char array of size 100

In other words, sizeof is an operator which operates on the type of the expression which you passed to it. So when you write sizeof(des) the type of the expression is char*, hence it prints sizeof(char*) which is 4 on your system. But in case of sizeof(a), the type of the expression is char[100], hence it prints sizeof(char[100]) which is 100.

A more interesting case with sizeof is discussed here:

走野 2024-12-15 23:53:35

因为它返回指向已分配内存的指针des的大小。

Because it returns the size of the pointer des which points to the allocated memory.

囍笑 2024-12-15 23:53:35

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 of des is a char[100], a char always takes 1 byte, and there's 100 of them.

sizeof a gives 4 because the type of a is a char*, 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.

等待我真够勒 2024-12-15 23:53:35

我将稍微详细说明阿贝伦基的评论。您期望这两个 typedef 产生等效的类型吗?

typedef char *string;
typedef char string[100];

这两个怎么样?

typedef struct { char *s; ... } header_t;
typedef struct { char s[100]; ... } header_t;

对于这两种情况,你的答案显然是否定的。是的,您可以编译 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?

typedef char *string;
typedef char string[100];

How about these two?

typedef struct { char *s; ... } header_t;
typedef struct { char s[100]; ... } header_t;

Your answer is clearly no in both cases. Yes, you could compile des=a because a yields a pointer, but a 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 type char * const meaning a is a constant pointer to non-constant characters, and thus a=des gives a type error. I donno if said error message will complain about arrays, const, or an lvalue though.

无需解释 2024-12-15 23:53:35

答案很简单。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文