开发者

Difference between two methods of creating of 2d array

开发者 https://www.devze.com 2023-04-09 08:05 出处:网络
What is the difference between two arrays definitions? Are they realized different in memo开发者_如何学Pythonry?

What is the difference between two arrays definitions? Are they realized different in memo开发者_如何学Pythonry?

 int var = 5;
 int (*p4)[2] = new int [var][2]; // first 2d array

 int** p5 = new int*[var];  // second 2d array
 for(int i = 0; i < var; ++i){
     p5[i] = new int[2];
 }   


Yes, they're very different. The first is really a single array; the second is actually var+1 arrays, potentially scattered all over your RAM. var arrays hold the data, and one holds pointers to the var data arrays.


The first is an ordinary, fully contiguous array, the second is also known as jagged array or lliffe vector and can e.g. be used to represent triangular structures.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号