DevC++ 上出现了一个奇怪的 sizeof() 错误
我知道我不应该使用 Dev-C++,但它在学校是强制性的,所以我对此无能为力。
主题是 C/C++ 中的指针,在测量整数数组的长度时出现错误。请参阅下面的代码:
// POINTER
# include<iostream>
# include<string.h>
using namespace std;
int main(){
//neues Feld anlegen
int *a = new int[5];
a[0] = 12;
a[1] = 5;
a[2] = 43;
a[3] = -12;
a[4] = 100;
// Feld füllen
for(int i = 0; i<sizeof(a);i++){
cout<<a[i]<<"\n"<<endl;
}
cout<<sizeof(a);
system("pause");
return 0;
}
sizeof() 返回 4 而不是 5...有什么想法吗?
I know I shouldn't use Dev-C++ but it's mandatory in school so I can't do anything about it.
Topic is pointers in C/C++ and a bug occurred while measuring the length of an integer array. See the code below:
// POINTER
# include<iostream>
# include<string.h>
using namespace std;
int main(){
//neues Feld anlegen
int *a = new int[5];
a[0] = 12;
a[1] = 5;
a[2] = 43;
a[3] = -12;
a[4] = 100;
// Feld füllen
for(int i = 0; i<sizeof(a);i++){
cout<<a[i]<<"\n"<<endl;
}
cout<<sizeof(a);
system("pause");
return 0;
}
The sizeof() returns 4 and not 5...Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
这不是一个错误,因为它返回
a
本身的大小(类型为int*
- 32 位构建上的 4 个字节),而不是数组的长度。请注意 - 其他人说大小取决于操作系统,这只说对了一半。这实际上取决于构建。
sizeof
是一个编译时构造。It is not a bug because its returning the size of
a
itself (which is of typeint*
- 4 bytes on a 32 bit build), not the length of the array.Please note - others say that the size is dependent on the operating system, which is half-true. It really depends on the build.
sizeof
is a compile-time construct.首先,您不应该认为“奇怪”行为的罪魁祸首是编译器:大多数时候错误是您的。
事实上,您的 sizeof 指令告诉您 int* 的大小是多少,这是设计使然,因此这不是编译器的错误,而是您的误解。无法使用
sizeof
来判断动态分配数组的大小。First of all, you shouldn't think that the culprit of a "strange" behaviour is the compiler: most of the time the bug is yours.
In fact, your
sizeof
instruction tells you what's the size of anint*
and that's by design, so it's not a compiler's fault but your misunderstanding. There's no way to usesizeof
to tell the size of a dynamically allocated array.本例中的 a 是一个指针,指针的大小取决于编译器。
你正在做 sizeof(int*) 。
事实上,您使用的是 32 位编译器,并且您的编译器\系统指针是 32 位的。
您无法从指针中获取使用 new 分配的对象的大小,sizeof 是在编译时评估的,而不是在运行时评估的。
要获取数组的大小,您应该这样做...
sizeof 返回以字节为单位的大小,而不是数组上的元素数。
该代码相当于写
a in this case is a pointer, a size of a pointer depends on the compiler.
You are doing sizeof(int*).
Indeed you are using a 32 bit compiler, and for your compiler\system pointers are 32 bits.
You cannot get the size of an object allocated with new from its pointer, sizeof is evaluated at compile time, not at runtime.
To obtain the size of the array you should do...
sizeof returns the size in bytes not the number of elements on an array.
That code is equivalent to write
sizeof 不会告诉您数组大小。它告诉您数据类型的大小,在本例中为 int*,在您的平台上为 4 个字节。无论数组元素的数量有多少,它始终为 4。
sizeof does not tell you an array size. It tells you the size of a datatype, in this case, an int*, which on your platform is 4 bytes. It will always be 4 regardless of the number of array elements.
局部变量
a
是指针类型(int*
)。在 32 位操作系统中,指针大小为 4 个字节。Local variable
a
is a pointer type (int*
). Pointer size is 4 bytes in 32bit operating systems.相当于:
这是可变的,具体取决于平台。在大多数 x32 平台上,它将是 4 个字节,而在 x64 平台上,它将是 8 个字节。
is equivalent to:
Which is variable, depending on the platform. On most x32 platforms, it will be 4 bytes, and on x64 platforms 8 bytes.
您在这里还有一个误解,这
意味着您将 a 声明为指向
int
类型数组的指针。事实上并非如此。int a*
表示您正在声明一个指向int
的指针。new int[5]
表示您正在为 5 个int
类型分配内存空间。int a* = new int[5];
表示您正在分配内存来存储 5 个 int,并将 int 点 a 设置为指向第一个元素。如果你声明了一个数组:
那么你可以:
also the misconception that you have here is that
means that you are declaring a as a pointer to an array of
int
types. This is infact not the case.int a*
means that you are declaring a pointer to anint
.new int[5]
means that you are allocating memory space for 5int
types.int a* = new int[5];
means that you are allocating memory for storing 5 ints and setting the int point a to point to the first element.If you had declared an array:
then you could:
更简单的方法来做你想做的事:
我不确定这是否通常被认为是“好的风格”,但它会修复你的代码。
easier way to do what you want:
I'm not sure if this is generally considered "good style" but it will fix your code.
您只能获得指针的大小,但不能获得数组的大小。如果将此代码编译为 64 位,您将得到另一个结果。
我通常使用下面的代码:
You just get the size of a pointer but not the size of the array. If you compile this code as 64-bit, you will get another result.
I usually use the code below: