声明整型数组 [英] Declaring array of int
问题描述
有这两个声明之间有什么区别?
INT X [10];
VS
为int * x =新INT [10];
我想以前的声明(如后者)是一个指针的声明和两个变量可以被同等对待。这是否意味着他们本质上是一样的吗?
#包括LT&;&iostream的GT;INT Y [10];
无效DoSomething的()
{
INT×〔10〕;
为int * Z = INT新[10];
//做一些有趣的事情 删除[】Z;
}诠释的main()
{
做一点事();}
INT X [10];
- 创建的堆栈上10整数大小的数组结果。
- 你不必直接删除此内存,因为它消失的堆栈开卷结果。
- 这是范围仅限于功能 DoSomething的()
的
INT Y [10];
- 创建对BSS /数据段10的整数大小的数组结果。
- 你不必直接删除此内存结果。
- 因为它被声明全球
是访问gloobally 的
为int * Z = INT新[10];
- 大小分配10个整数堆的动态数组并将此内存的地址返回到以Z
结果。
- 你有使用它后显式删除此动态内存。使用:的
删除[】Z;
Is there any difference between these two declarations?
int x[10];
vs.
int* x = new int[10];
I suppose the former declaration (like the latter one) is a pointer declaration and both variables could be treated the same. Does it mean they are intrinsically the same?
#include<iostream>
int y[10];
void doSomething()
{
int x[10];
int *z = new int[10];
//Do something interesting
delete []z;
}
int main()
{
doSomething();
}
int x[10];
- Creates a array of size of 10 integers on stack.
- You do not have to explicitly delete this memory because it goes away as stack unwinds.
- It's scope is limited to the function doSomething()
int y[10];
- Creates a array of size of 10 integers on BSS/Data segment.
- You do not have to explicitly delete this memory.
- Since it is declared global
it is accessible gloobally.
int* z = new int[10];
- Allocates a dynamic array of size 10 integers on heap and returns the address of this memory to z
.
- You have to explicitly delete this dynamic memory after using it. using:
delete []z;
这篇关于声明整型数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!