C++访问静态数组与新数组之外的边界?

发布于 2024-12-21 08:24:42 字数 699 浏览 2 评论 0原文

为什么下面的代码可以编译运行。

我创建了一个整数数组并将其大小指定为 10,为什么当我在 for 循环内尝试访问数组外部的元素时,程序不返回错误?

另外,我想了解 new 的概念,我的用法是否正确(我没有分配 *arrays 需要多少内存),我的理解是 new 允许我创建动态大小的数组(这意味着我可以在运行时无限期地增加该数组的大小,如果我错了,请纠正我)。如果这是正确的,我使用 new 或只是分配 array[]; 有什么区别?因为两者显然都允许我在运行时增加数组的大小,如本例所示。我了解作用域、堆栈和堆的差异,因此假设这两个变量仅在 main 中声明,并使用以下代码作为示例。

http://ideone.com/Tbud1

#include <iostream>
using namespace std;

int main()
{
int array[10];
int *arrays;
arrays = new int();
for (int i=0; i<450; i++)
{
   arrays[i] = i;
   cout << arrays[i] << " ";
   array[i] = i;
   cout << array[i] << endl;
}
return 0;
}

Why does the following code compile and run.

I have created an integer array and assigned its size to 10, why then does the program not return an error that I am trying to access an element outside the array, when inside the for loop?

In addition, I would like to understand the concept of new, is my usage correct(I have not assigned how much memory *arrays needs), it is my understanding that new allows me to create an array of dynamic size (meaning that I can increase the size of this array indefinitely during runtime, correct me if I am wrong). If this is correct what is the difference in me using new or simply just allocating array[]; since both apparently allow me to increase the size of my array at runtime as can be seen with this example. I know about scope, stack and heap differences, so assume that both variables are only declared in main and use the following code as the example.

http://ideone.com/Tbud1

#include <iostream>
using namespace std;

int main()
{
int array[10];
int *arrays;
arrays = new int();
for (int i=0; i<450; i++)
{
   arrays[i] = i;
   cout << arrays[i] << " ";
   array[i] = i;
   cout << array[i] << endl;
}
return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

倦话 2024-12-28 08:24:42

访问超出数组范围的元素会导致未定义的行为
在这种有保证的未定义行为情况下,编译器不需要执行任何特定操作。
任何事情都可能发生,您的程序可能会崩溃或不崩溃,或者表现出不稳定的行为,这是标准所允许的。

参考:

C++ 标准第 1.3.24 节规定:

允许的未定义行为范围从完全忽略结果不可预测的情况,到在翻译或程序执行期间以环境特有的记录方式表现(无论是否发出诊断消息),到终止翻译或执行(并发出诊断消息)。

您对 new 的理解不正确。 new 允许您在自由存储(也称为堆)上分配固定数量的内存。如果扩展已分配内存的范围,它不会分配额外的内存。您需要确保必须分配足够的内存,以免超出界限。

在你的程序中,你试图分配等于 10 个数组元素的内存,所以你应该这样做:

arrays = new int[10];

另外,一旦你使用完分配的内存,不要忘记调用 delete[] ,否则会导致内存泄漏。

delete []arrays;

如果您需要一个根据您的使用情况自动增加的数据结构,C++ 以 std::向量

Accessing elements beyond the bounds of an array results in Undefined Behavior.
Compilers do not need to do anything specific in such guaranteed Undefine Behavior cases.
And potentially anything might happen, your program might crash or not or show erratic behavior and this is allowed by the Standard.

Reference :

C++ Standard section 1.3.24 states:

Permissible undefined behavior ranges from ignoring the situation completely with unpredictable results, to behaving during translation or program execution in a documented manner characteristic of the environment (with or without the issuance of a diagnostic message), to terminating a translation or execution (with the issuance of a diagnostic message).

Your understanding of new is not correct. new allows you to allocate a fixed amount of memory on the freestore (a.k.a heap). It does not allocate additional memory if you extend the bounds of the allocated memory. It is up to you to ensure that you have to allocate enough memory so that you do not exceed the bounds.

In your program you are trying to allocate memory equal to 10 array elements so you should be doing:

arrays = new int[10];

Also, do not forget to call delete[] once you are done using the allocated memory or it results in a memory leak.

delete []arrays;

If you need a data structure which increases automatically as per your usage, C++ provides that in the form of std::vector.

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