1

There is one where you just write array[rowSize][colSize]. Another where you declare it as an array of pointers to arrays using new. (From How do I declare a 2d array in C++ using new? )

int** ary = new int*[rowCount];
for(int i = 0; i < rowCount; ++i)
  ary[i] = new int[colCount];

There should be one using malloc. Are there any more? What are the pros/cons of each of them? How about their speed of execution/processing?

(This is an interview question. So, more than just suggesting the most optimal method, I need to know what each of these methods do)

Community
  • 1
  • 1
Bharg
  • 311
  • 1
  • 2
  • 15
  • 2
    Use `std::vector`. `` – Baum mit Augen Jan 21 '17 at 02:04
  • Anything you can do with `new`, you can do with `malloc`, a `static_cast`, and some placement `new`s. – Travis Gockel Jan 21 '17 at 02:19
  • @BaummitAugen: `std::vector` doesn't make a very good 2-D array, by itself. – Ben Voigt Jan 21 '17 at 02:29
  • @BenVoigt Even the trivial `std::vector>` that an easy google search will find is better than the approach in the OP and the better flat array won't be too hard to find either. – Baum mit Augen Jan 21 '17 at 02:38
  • 1
    @BaummitAugen: Nested vectors have their uses, but they really suck at trying to be two-dimensional arrays. – Ben Voigt Jan 21 '17 at 02:43
  • @BenVoigt The point of my comments was not to advocate nested vectors over their flat equivalent, but to advocate `std::vector` over "raw arrays". So what I'm trying to say: Nested vector > `int**` and `std::vector` > `int*`, which is independent of the choice between `malloc` and `new` appearing in the question. – Baum mit Augen Jan 21 '17 at 02:52
  • @Baun: Well, in my (somewhat expert) opinion, `int**` is better than `vector >`. The first doesn't structurally guarantee rectangularity, but if you create it that way, it stays rectangular. Definitely not smary, you'd want `unique_ptr` (like `vector` but enforces that its size can't be changed after creation). In other questions I've showed how to set up a dynamically-sized 2D array that is contiguous like a static 2-D array, permits 2-D subscripting, and cleans itself up. – Ben Voigt Jan 21 '17 at 03:04
  • 1
    I like a 1D array with a wrapper that makes it look 2D. – user4581301 Jan 21 '17 at 03:58

2 Answers2

0

I do not fully understand the question here, but I can tell you the differences between the two. When using new (or malloc) the variables stay off the stack. When declaring the variable like:

int iarray[10][10];

it uses stack space. The downside to using the new operator is that you must remember to use the delete [] operator, too.

  • You're missing a discussion of context. If you declare it like that at file scope, it has `static` lifetime, and on typical implementations, is placed in the `.bss` or `.ibss` segment not the stack. Inside a function, it has automatic lifetime, which yes, on typical implementations, goes on the stack. Inside a class, it takes on the storage class of the instance of the containing class, which can be placed in static storage, on the stack, the heap, inside yet another object... Saying "it uses stack space" is 100% too simplistic. – Ben Voigt Jan 21 '17 at 02:28
  • Yes, I tried to keep it simple... I hope I didn't ruffle your feathers. – PROWARE technologies Jan 21 '17 at 04:27
0

There are different types:

1. This is an array where each element is an array itself:

int array[rowSize][colSize];

It is like this:

typedef int A[colSize];
A array[rowSize];

where sizeof(A) is colSize*sizeof(int) and sizeof(array) is rowSize*sizeof(A)

indexes of elements in memory for a[3][3]:

|0,0|0,1|0,2|1,0|1,1|1,2|2,0|2,1|2,2|

2. This instead is a pointer to a pointer:

int** ary

where ary may refer to array of pointers where each element may refer to any size array of int. size of pointer is machine dependent.

It initialized in example as array of poiner. And after that each element of it is initialized newely created array.

For your example:

int** ary = new int*[rowCount];
for(int i = 0; i < rowCount; ++i)
  ary[i] = new int[colCount];

have rowCount and colCount equal to 3 and 3, than allocated arrays and indexes in memory:

|0|1|2| ... |0,0|0,1|0,2| ... |1,0|1,1|1,2| ... |2,0|2,1|2,2|
 | | |       ^                 ^                 ^
 | | --------                  |                 |
 | ----------------------------                  |
 ------------------------------------------------
oklas
  • 7,935
  • 2
  • 26
  • 42
  • User @Lamar Latrell make edition just now which was rejected by my edition in same time. I apply suggested changes manually. Thanks. – oklas Jan 21 '17 at 03:07