C++ int *array = new int[n]; 这个函数实际上在做什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5776529/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
int *array = new int[n]; what is this function actually doing?
提问by pauliwago
I am confused about how to create a dynamic defined array:
我对如何创建动态定义的数组感到困惑:
int *array = new int[n];
I have no idea what this is doing. I can tell it's creating a pointer named array that's pointing to a new object/array int? Would someone care to explain?
我不知道这是在做什么。我可以说它正在创建一个名为 array 的指针,该指针指向一个新的对象/数组 int?有人愿意解释吗?
回答by ANisus
newallocates an amount of memory needed to store the object/array that you request. In this case n numbers of int.
new分配存储您请求的对象/数组所需的内存量。在这种情况下,n 个整数。
The pointer will then store the address to this block of memory.
然后指针将地址存储到该内存块。
But be careful, this allocated block of memory will not be freed until you tell it so by writing
但要小心,这个分配的内存块不会被释放,直到你通过写来告诉它
delete [] array;
回答by Nawaz
int *array = new int[n];
It declares a pointer to a dynamic array of type int
and size n
.
它声明了一个指向类型int
和大小的动态数组的指针n
。
A little more detailed answer: new
allocates memory of size equal to sizeof(int) * n
bytes and return the memory which is stored by the variable array
. Also, since the memory is dynamically allocated using new
, you've to deallocate it manually by writing (when you don't need anymore, of course):
更详细的答案:new
分配大小等于sizeof(int) * n
字节的内存并返回由变量存储的内存array
。此外,由于内存是使用 动态分配的new
,您必须通过写入手动解除分配(当然,当您不再需要时):
delete []array;
Otherwise, your program will leak memory of at least sizeof(int) * n
bytes (possibly more, depending on the allocation strategy used by the implementation).
否则,您的程序将泄漏至少sizeof(int) * n
字节的内存(可能更多,取决于实现使用的分配策略)。
回答by aeon
The statement basically does the following:
该语句主要执行以下操作:
- Creates a integer array of 'n' elements
- Allocates the memory in HEAP memory of the process as you are using newoperator to create the pointer
- Returns a valid address (if the memory allocation for the required size if available at the point of execution of this statement)
- 创建一个包含“n”个元素的整数数组
- 当您使用new运算符创建指针时,在进程的 HEAP 内存中分配内存
- 返回有效地址(如果在执行此语句时为所需大小分配的内存可用)
回答by DhruvPathak
It allocates space on the heap equal to an integer array of size N, and returns a pointer to it, which is assigned to int* type pointer called "array"
它在堆上分配等于大小为 N 的整数数组的空间,并返回一个指向它的指针,该指针被分配给称为“数组”的 int* 类型指针
回答by sgokhales
It allocates that much space according to the value of n and pointer will point to the array i.e the 1st element of array
它根据 n 的值分配那么多空间,指针将指向数组,即数组的第一个元素
int *array = new int[n];
回答by LLS
In C/C++, pointers and arrays are (almost) equivalent.
int *a; a[0];
will return *a
, and a[1];
will return *(a + 1)
在 C/C++ 中,指针和数组(几乎)是等价的。
int *a; a[0];
会回来*a
,a[1];
会回来*(a + 1)
But array can't change the pointer it points to while pointer can.
但是数组不能改变它指向的指针,而指针可以。
new int[n]
will allocate some spaces for the "array"
new int[n]
将为“数组”分配一些空间
回答by Android M
The new
operatoris allocating space for a block of n
integers and assigning the memory address of that block to the int*
variable array
.
该new
操作者为块分配空间n
的整数以及分配该块到的存储器地址int*
的变量array
。
The general form of new as it applies to one-dimensional arrays appears as follows:
应用于一维数组的 new 的一般形式如下所示:
array_var = new Type[desired_size];
回答by Engineero
As of C++11, the memory-safe way to do this (still using a similar construction) is with std::unique_ptr
:
从 C++11 开始,执行此操作的内存安全方法(仍然使用类似的构造)是使用std::unique_ptr
:
std::unique_ptr<int[]> array(new int[n]);
This creates a smart pointer to a memory block large enough for n
integers that automatically deletes itself when it goes out of scope. This automatic clean-up is important because it avoids the scenario where your code quits early and never reaches your delete [] array;
statement.
这将创建一个指向一个足够大的内存块的智能指针,n
当它超出范围时会自动删除自己。这种自动清理很重要,因为它避免了您的代码提前退出并且永远不会到达您的delete [] array;
语句的情况。
Another (probably preferred) option would be to use std::vector
if you need an array capable of dynamic resizing. This is good when you need an unknown amount of space, but it has some disadvantages (non-constant time to add/delete an element). You could create an array and add elements to it with something like:
std::vector
如果您需要能够动态调整大小的数组,则使用另一个(可能是首选)选项。当您需要未知量的空间时,这很好,但它有一些缺点(添加/删除元素的时间不固定)。您可以创建一个数组并向其中添加元素,例如:
std::vector<int> array;
array.push_back(1); // adds 1 to end of array
array.push_back(2); // adds 2 to end of array
// array now contains elements [1, 2]