处理数组大小

发布于 2024-10-04 13:39:11 字数 926 浏览 7 评论 0原文

我碰巧问了自己一个关于 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 技术交流群。

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

发布评论

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

评论(3

巴黎盛开的樱花 2024-10-11 13:39:11

mystruct array[20] 示例开始就是错误的。由于该函数接收指针类型,而不是数组类型,因此它无法推断出数组中的元素数量。当您执行 sizeof(arr) 时,您实际上获得了 mystruct* 的大小。

可以使用模板来编写以数组作为参数的函数,但如果我没记错的话,C++中建议的方法是使用向量

接收数组作为参数的“方法”是编写如下内容:

template <int N> void somefunction(int (&v)[N]);

编辑更正了函数声明。哎呀。

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 a mystruct* when you perform sizeof(arr).

You can use templates to write functions which take arrays as parameters, but the suggested way in C++ is to use vectors, if I am not wrong.

The "way" to receive arrays as parameters would be to write something like:

template <int N> void somefunction(int (&v)[N]);

EDIT corrected the function declaration. oops.

不气馁 2024-10-11 13:39:11
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
   }
}

本例中 arr 是什么类型? mystruct*!它的大小很可能是 4 或 8。如果您想将静态/自动分配的数组(不是新的)传递给保留大小的函数,以便您的技巧起作用,请通过 REFERENCE 传递!

template <int N>
void dosome(mystruct (& arr) [N]) {
    for (int i = 0; i < N; i++) {
      // Do something . No overflow will occur
   }
}

还要注意这一点

int a[20];
sizof a; //equals to 20*sizeof(int)
int* b = new int [20];
sizeof b; //equals to sizeof pointer, most likely 4 
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
   }
}

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!

template <int N>
void dosome(mystruct (& arr) [N]) {
    for (int i = 0; i < N; i++) {
      // Do something . No overflow will occur
   }
}

Also note this

int a[20];
sizof a; //equals to 20*sizeof(int)
int* b = new int [20];
sizeof b; //equals to sizeof pointer, most likely 4 
半山落雨半山空 2024-10-11 13:39:11

sizeof 是一个编译时运算符。这里它只计算指针的大小。

sizeof is a compile-time operator. And here it computes only the size of a pointer.

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