像int a [10] [10]这样的数组分配与在c ++中使用new关键字分配数组有什么区别?

时间:2018-09-18 03:14:12

标签: c++ arrays new-operator

我目前正在使用Windows 10和 gcc版本6.3.0(MinGW.org GCC-6.3.0-1)
代码1

#include <bits/stdc++.h>
using namespace std;

int main(){
    int** a = new int*[100000];
    for(int i = 0; i < 100000; ++i)
        a[i] = new int[1000];
    cout << "Array allocation Ok!!!\n";
    return 0;
}

//Output
Array allocation Ok!!!

代码2

#include <bits/stdc++.h>
using namespace std;

int main(){
    int arr[100000][1000];
    cout << "Array allocation Ok!!!\n";
    return 0;
}

//I got no Output, it just return the console back to me

有人建议我,区别可能是在代码2行中 i i + 1 是连续的(行主要表示),即int arr[1000][100]int arr[100000]。但是在代码1中,列条目本质上是连续的,但行不是连续的。但是,如果是这种情况,那么指针算术呢?

1 个答案:

答案 0 :(得分:0)

简而言之,就是在堆栈上分配a [10] [10],在堆上分配使用new分配的数组。