普通数组声明与动态数组声明

huangapple go评论64阅读模式
英文:

Normal array declaration vs. dynamic array declaration

问题

我刚开始学习C++。我学习了声明数组的简单方式,现在我对以下代码的用法感到困惑:

int* foo = new int[n];

以及它与以下方式的区别:

int foo[n];

我尝试使用代码进行测试,但找不到任何区别。我从资料中了解到,使用 "new" 需要在不再需要内存时手动释放它。在这种情况下,使用 "new" 或动态内存分配没有任何优势。我是否遗漏了什么?

我尝试运行以下代码:

#include <iostream>

int main() {
  int n;
  std::cout << "数组大小" ;

  std::cin >> n ;
  std::cout << n ;

  int foo[n]; //行 A
  // int* foo = new int[n]; //行 B
  foo[6] = 30;
  
  std::cout<<foo[6]<<std::endl;
}

注释掉行 B 来运行行 A,或者反之,都会得到完全相同的结果。

英文:

I just started learning C++. I learned the easy way of declaring arrays and now I'm confused about the usage of

int* foo = new int[n];

and how it is different from

int foo [n];

I tried testing with code but couldn't find any difference. I read from sources that using "new" requires me to manually de-allocate the memory after I don't need it anymore. In that case, there is no advantage in using "new" or dynamic memory allocation at all. Am I missing something here?

I tried running this:

#include &lt;iostream&gt;

int main() {
  int n;
  std::cout &lt;&lt; &quot;array size&quot; ;

  std::cin &gt;&gt; n ;
  std::cout &lt;&lt; n ;

  int foo [n]; //line A
  // int* foo = new int[n]; //line B
  foo[6] = 30;
  
  std::cout&lt;&lt;foo[6]&lt;&lt;std::endl;
}

Commenting out line B to run line A, or vice versa, gave the exact same result.

答案1

得分: 0

以下是翻译好的部分:

有几种不同的方式。首先,让我们谈谈这个:

int n = 10;
int array[n];

这不是 ANSI C++ 标准的一部分,可能不被所有编译器支持。你不应该依赖它。想象一下这段代码:

int n = 10;
int array[n];
n = 20;

array 有多大?

现在,这是你可以做的方式(但仍然有问题):

int n = 10;
int * array = new int[n];

现在,这是合法的。但以后你必须记得:

delete [] array;
array = nullptr;

现在还有另外两个不同之处。第一个在栈上分配空间。第二个在堆上分配空间,它在你删除它之前是持久存在的。因此,第二个可以从函数返回数组,但第一个不行,因为它在函数退出时消失。

然而... 强烈不建议使用任何一种方式。你应该使用容器类代替。

#include <array>

std::array<int, n> array;

这样做的优点:

  1. 它是标准的,你可以依赖它
  2. 你不需要记得释放它
  3. 它提供范围检查
英文:

There are several ways these are different. First, let's talk about this one:

int n = 10;
int array[n];

This is not part of the ANSI C++ standard and may not be supported by all compilers. You shouldn't count on it. Imagine this code:

int n = 10;
int array[n];
n = 20;

How big is array?

Now, this is the way you can do it (but it's still problematic):

int n = 10;
int * array = new int[n];

Now, that is legal. But you have to remember later to:

delete [] array;
array = nullptr;

Now, there are two other differences. The first one allocates space on the stack. The second allocates space on the heap, and it's persistent until you delete it (give it back). So the second one could return the array from a function, but the first one can't, as it disappears when the function exits.

HOWEVER... You are strongly, strongly discouraged from doing either. You should instead use a container class.

#include &lt;array&gt;

std::array&lt;int, n&gt; array;

The advantages of this:

  1. It's standard and you can count on it
  2. You don't have to remember to free it
  3. It gives you range checking

huangapple
  • 本文由 发表于 2023年2月9日 02:55:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75390485.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定