为什么 C++ 中需要动态数组?

发布于 2024-11-08 05:54:02 字数 175 浏览 4 评论 0原文

我不明白动态数组的必要性。据我所知,到目前为止,需要动态数组,因为人们无法总是知道运行时需要多大的数组大小。

但肯定有人能做到这一点吗?:

cin >> SIZE;
int a[SIZE];

那么动态数组和 new 运算符有什么大不了的呢?

I don't understand the need for dynamic arrays. From what I understand so far, dynamic arrays are needed because one cannot always tell what size of array will be needed at runtime.

But surely one can do this?:

cin >> SIZE;
int a[SIZE];

So what's the big deal with dynamic arrays and the new operator?

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

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

发布评论

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

评论(6

掩耳倾听 2024-11-15 05:54:02

首先,这是一个编译器扩展,而不是标准 C++。其次,该数组是在堆栈上分配的,而operator new是从堆上分配的,这是两个非常不同的地方,极大地影响了数组的生命周期。如果我想返回该数组,该代码有什么用?第三,如果你想调整它的大小,你会怎么做?

Firstly, that is a compiler extension and not Standard C++. Secondly, that array is allocated on the stack, whereas operator new allocates from the heap, which are two very different places that drastically affects the lifetime of the array. What use is that code if I want to return that array? Thirdly, what are you gonna do if you want to resize it?

驱逐舰岛风号 2024-11-15 05:54:02

SIZE 是一个变量,这意味着它的值可以修改。根据定义,数组的大小既不能增长也不能缩小。因此,它的大小需要是一个编译时间常数。

SIZE is a variable which means it's value can be modified. Array by definition, can neither grow nor shrink in it's size. So, it's size needs to be a compile time constant.

红ご颜醉 2024-11-15 05:54:02
cin >> SIZE; int a[SIZE];

我的一些用户很难使用鼠标,您希望他们告诉我的应用程序要分配多少内存?

cin >> SIZE; int a[SIZE];

Some of my users have a hard enough time using a mouse, and you want them to tell my application how much memory to allocate?

七度光 2024-11-15 05:54:02

动态数组非常方便......假设您不断生成对象,并且您不知道可能会生成多少个对象(即,您可以从某人在提示符下输入答案或从网络套接字等获得多少输入.)?您要么必须预测需要什么合适的大小,要么必须编写硬限制代码。两者都很痛苦,并且在对数组进行硬限制的情况下,可能会导致为手头的工作分配过多的内存,以便尝试覆盖可能遇到的每种可能的情况。即使这样,您也可能会发生缓冲区溢出,从而造成安全漏洞和/或崩溃。使对象能够动态分配新内存,同时保持对象之间的关联,以便您具有恒定时间访问(而不是像链表那样的线性时间访问),这是一个非常非常方便的工具。

Dynamic arrays are very handy ... suppose you keep generating objects, and you have no idea how many objects might be generated (i.e., how much input might you get from someone typing an answer at a prompt, or from a network socket, etc.)? You'd either have to make predictions on what appropriate sizes are needed, or you're going to have to code hard-limits. Both are a pain and in the case of hard-limits on arrays, can lead to allocating way too much memory for the job at hand in order to try and cover every possible circumstance that could be encountered. Even then you could incur buffer-overruns which create security holes and/or crashes. Having the ability for an object to dynamically allocate new memory, yet keep the association between objects so that you have constant-time access (rather than linear-time access like you'd get with a linked-list), is a very, very handy tool.

风启觞 2024-11-15 05:54:02

在 C 的早期,当您第一次进入函数时,堆栈上会产生空间,并且堆栈的大小保持不变,直到您调用另一个函数或返回为止。这就是为什么所有变量都需要在函数顶部声明,以及为什么需要在编译时知道数组大小,以便编译器可以保留已知数量的堆栈。

虽然C++放宽了变量声明的限制,但它仍然保留了在编译时了解堆栈要求的限制。我不知道为什么。

正如评论中的链接所述,C 最终允许动态大小的数组。这是在 C 和 C++ 分离之后发生的,因此 C++ 并没有自动获得该功能。在 C++ 中支持它作为扩展的情况并不罕见。

In the early days of C, space was made on the stack when you first entered a function and the size of the stack was untouched until you called another function or returned. This is why all variables needed to be declared at the top of a function, and why array sizes needed to be known at compile-time, so the compiler could reserve a known amount of stack.

While C++ loosened the restrictions of variable declaration, it still kept the restriction of knowing the stack requirements at compile time. I don't know why.

As noted in the link in the comments, C finally allowed for dynamic size arrays. This came after C and C++ were split, so C++ didn't automatically gain that capability. It's not uncommon to find it supported as an extension in C++.

焚却相思 2024-11-15 05:54:02

我个人更喜欢:

std::cin >> size;
std::vector a(size);

稍后,正如其他人提到的,你可以做类似的事情

std::cin >> size;
a.resize(size);

......但是,这可能是关键点,如果你不想,你就不必这样做。如果您的要求和约束可以通过静态大小的数组/向量/其他数据结构来满足,那就太好了。请不要认为您对其他人的要求和限制了解得足够多,无法从他们身上删除一个非常有用的工具。

Personally I prefer:

std::cin >> size;
std::vector a(size);

Later, as others have mentioned, you could do something like ...

std::cin >> size;
a.resize(size);

...but, and this is probably the key point, you don't have to if you don't want to. If your requirements and constraints are such that they can be satisfied with static size arrays / vectors / other data structures then great. Please don't think you know enough about everybody else's requirements and constraints to remove from them a very useful tool.

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