处理数组大小
我碰巧问了自己一个关于 C++ 中数组的问题。 好吧,我们都知道数组是某种东西的固定集合,我说固定是因为在定义数组时需要声明数组长度。 好吧,让我们考虑一个例子:
char myarray[10] = {'\0'};
int sz = sizeof(myarray); // It is supposed to be 10
嗯,这是正确的,10 是 sizeof 返回的数字。这可以由编译器完成,因为他知道为该变量放置了多少空间。
现在考虑在这种情况下会发生什么:
void dosome(mystruct* arr) {
int elements = sizeof(arr)/sizeof(mystruct);
for (int i = 0; i < elements; i++) {
// Do something hoping no overflow will ever occur
}
}
很好......但我认为它可能容易溢出。如果我将一个以“正常”方式创建的数组传递给这个函数,一切都应该没问题:
mystruct array[20];
dosome(array);
没问题。但如果我这样做:
mystruct* array = (mystruct*)malloc(80*sizeof(mystruct));
dosome(array);
会发生什么? 我想了解 sizeof 的行为方式,这个函数是在编译时评估的,对吗???好吧,当我使用的不是数组而是像这样的数据块这样非常麻烦的东西时会发生什么?此外,我可以通过再次调用 malloc 来重新分配它,并要求执行一些操作来再次处理该数据块。它会起作用吗? 我可以亲自尝试一下,但我会得到一些关于 sizeof 行为的确切答案。
谢谢。
I happened to ask myself a question about arrays in c++.
Well, we all know that arrays are fixed collections of something, I say fixed because it is necessary to declare array length when defining arrays.
Well, let's consider an example:
char myarray[10] = {'\0'};
int sz = sizeof(myarray); // It is supposed to be 10
Well, it is correct, 10 is the number returned by sizeof. This can be done by the compiler because he knows how much space it placed for that variable.
Now consider what happens in this situation:
void dosome(mystruct* arr) {
int elements = sizeof(arr)/sizeof(mystruct);
for (int i = 0; i < elements; i++) {
// Do something hoping no overflow will ever occur
}
}
Nice... but I suppose it can be overflow prone. If I pass to this function an array I created in a "normal" way, everything should be fine:
mystruct array[20];
dosome(array);
No problem. But if I do this:
mystruct* array = (mystruct*)malloc(80*sizeof(mystruct));
dosome(array);
WHAT HAPPENS???????????????????
I would like to understand how sizeof behaves, this function is evaluated at compile time right??? ok, what happens when I use not an array, but something very cumbersome like a block of data like that one? furthermore, I could realloc it woth another call to malloc and ask to dosome to process that datablock again. Will it work?
I could try it physically, but I would get some exact answer about the behavioir of sizeof.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从
mystruct array[20]
示例开始就是错误的。由于该函数接收指针类型,而不是数组类型,因此它无法推断出数组中的元素数量。当您执行sizeof(arr)
时,您实际上获得了mystruct*
的大小。您可以使用模板来编写以数组作为参数的函数,但如果我没记错的话,C++中建议的方法是使用
向量
。接收数组作为参数的“方法”是编写如下内容:
编辑更正了函数声明。哎呀。
it's wrong starting from the
mystruct array[20]
example. Because the function receives a pointer type, and not an array type, it cannot deduce the number of elements in the array. you are actually getting the size of amystruct*
when you performsizeof(arr)
.You can use templates to write functions which take arrays as parameters, but the suggested way in C++ is to use
vector
s, if I am not wrong.The "way" to receive arrays as parameters would be to write something like:
EDIT corrected the function declaration. oops.
本例中 arr 是什么类型?
mystruct*
!它的大小很可能是 4 或 8。如果您想将静态/自动分配的数组(不是新的)传递给保留大小的函数,以便您的技巧起作用,请通过 REFERENCE 传递!还要注意这一点
What type does arr have in this example?
mystruct*
! And it's size is most likely 4 or 8. If you want to pass statically/automatically allocated arrays (not new'd) to functions preserving the size so that your trick works, pass by REFERENCE!Also note this
sizeof 是一个编译时运算符。这里它只计算指针的大小。
sizeof is a compile-time operator. And here it computes only the size of a pointer.