0

I was solving a problem on sum of submatrices, I declared my 2d-array as

   int a[i][j] ; //i =number of rows and j = number of columns

my code executed properly. But when I saw the solution i saw these lines :

 int **arr = new int * [n] ;
 for (int i = 0 ; i < n  ; i++)
    {
     arr[i]=new int [m];
    } 
 // n -> number of rows and  m -> number of columns.

I understand the code. why the solution(given on some random website) is using pointers. If we can do it using the above normal declaration. It will make the code faster or something?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Laxman Patel
  • 1
  • 1
  • 4
  • there are a bunch of things that changes, for example if you see those arrays as a matrix, and you want to swap the first and the second row (for example), with the second solution, you just need to swap the 2 pointers, in your solution, you can't and so you have to swap each element of the 2 rows – Alberto Sinigaglia Jun 13 '20 at 19:32
  • 1
    This code should (IMHO) just be using a `std::vector` in the first place and forget about the manual memory management and pointer nonsense. At least until a profiler proves that doing so is a significant performance bottleneck. – Jesper Juhl Jun 13 '20 at 19:32
  • 2
    The `int a[i][j] ;` version is using a variable length array (VLA) which is *not* part of the C++ language (though some compilers support VLAs as an extension). – Adrian Mole Jun 13 '20 at 19:32
  • 1
    Because `int a[i][j];` is not legal in C++. It's hit or miss whether or not a compiler has an extension that supports variable length arrays. That said, using `new`'s not much better. Prefer to [use something like this](https://stackoverflow.com/a/2076668/4581301). – user4581301 Jun 13 '20 at 19:33

3 Answers3

2

This declaration

int a[i][j] ; //i =number of rows and j = number of columns

requires that the variables i and j would be compile-time constants. Variable length arrays is not a standard C++n feature though some compilers can have their own language extensions that include this feature.

The second problem that exists is if sizes of an array are too big then the compiler can be unable to create such an array with automatic storage duration.

So if i and j are not compile-time constants or are too big then you have to allocated memory dynamically yourself or you can use the standard container std::vector.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

If we can do it using the above normal declaration. It will make the code faster or something? no all in above code you create an array in the stack it will be deleted if the function out of scope it will be removed automatically

the second is created in heap it will still in the heap until u delete it by self

AhmedSeef
  • 13
  • 2
  • The top option can be much, much faster. Stack allocation is cheap and it is all one contiguous block so the cache usage can be much more effective. On the downside `m*n` can get big, fast, and blow the top off the stack. – user4581301 Jun 13 '20 at 19:38
  • [Which is faster: Stack allocation or Heap allocation](https://stackoverflow.com/questions/161053/which-is-faster-stack-allocation-or-heap-allocation). Every `new`ed allocation is a different block somewhere in memory and possibly nowhere near the others. This leads to [What is a “cache-friendly” code?](https://stackoverflow.com/questions/16699247/what-is-a-cache-friendly-code). The third point is just math. Depending on how much stack is available, `m` and `n` of a couple hundred could cause an overflow. – user4581301 Jun 13 '20 at 20:03
0

Both ways of array declaration are useful in different use cases. The declaration:

int a[i][j];

declares an array statically and it uses stack memory to store the array or we can say that the memory is allocated at the runtime. This type of array declaration requires you to pass the value 'n' and the size of the array can not be altered after it's been declared. That's where it has a drawback as you can not increase the size if you want to store some more elements. Also if you stored less elements than 'n', then the remaining memory gets wasted. The other type of declaration:

int *a = new int[n];                      //For 1D array

creates a dynamically allocated memory. In other words, in this type of declaration memory allocation takes place only when an element is placed in the array or at the runtime. This allocation ensures that there is no memory wastage and stores the elements of the array in the heap memory. But in this type of declaration, you also need to deallocate the memory otherwise it may cause memory leaks because C++ has nothing like a garbage collector.

Madhur Vashistha
  • 329
  • 1
  • 2
  • 7