-1

I have created a 2d array and initialized to 0 in C++.

int n;
cin>>n;
int ar[n][n]={0};

for(int i=0;i<n;i++)
{
    for(int j=0;j<n;j++)
    {
        cout<<ar[i][j]<<" ";
    }
    cout<<endl;
}

When I print this array, it prints random garbage values in the first row as shown below

0 0 4294220 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0

Why is this happening. I have read about variable length arrays here. But I can't find any explanation why is this happening.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
RaKo
  • 49
  • 1
  • 2
  • 11
  • 4
    `int ar[n][n]={0}` is not allowed in C++. Add `-pedantic-errors` to you compiler flags to stop the compiler from excepting this C extension. – NathanOliver May 25 '22 at 16:27
  • 4
    With VLAs (as they are a compiler extension) there is no guarentee `int ar[n][n]={0};` will zero the array you will need to check the compiler's documentation. – Richard Critten May 25 '22 at 16:28
  • 1
    I don't agree with the dupe target [Initialize large two dimensional array in C++](https://stackoverflow.com/questions/3460074/initialize-large-two-dimensional-array-in-c). According to top answer, `= { 0 }` should initialzie all elements to `0`, but it doesn't. Duplicate or answer should target VLAs specifically. – Yksisarvinen May 25 '22 at 16:28
  • 1
    @Yksisarvinen `= { 0 };` will work for fixed size (compile time constant) arrays, but you will need to read the compiler docs for it's implementation of VLAs. We can't check because we don't know which compiler is being used. – Richard Critten May 25 '22 at 16:29
  • 1
    @RaKo - since you are using a compiler-specific extension, please add your compiler and version into your question. – Yksisarvinen May 25 '22 at 16:31
  • 4
    The better fix is to use `std::vector`. It was made so C++ doesn't need VLAs. – Goswin von Brederlow May 25 '22 at 16:34
  • 2
    Or much better: Don't rely on VLA. Though with dynamic allocation it's usually best/most efficient to fall back to a 1D array (std::vector!) and do manual index calculations (`row * colCount + col`) – which the double indexing on the 2D array does just alike under the hoods... – Aconcagua May 25 '22 at 16:35

1 Answers1

3

You declared a variable length array

int n;
cin>>n;
int ar[n][n]={0};

However variable length arrays are not a standard C++ feature. Variable length arrays are conditionally supported by C compilers.

Nevertheless you may not initialize a variable length array in its declaration.

A simple way to set to zero elements of the array is to use the C standard function memset. For example

#include <cstring>

//...

memset( ar, 0, n * n * sizeof( int ) );

Or you could use the standard algorithm std::fill declared in the header <algorithm> for example in a range-based-for loop.

Otherwise you can use the standard container std::vector<std::vector<int>>.

For example

std::vector<std::vector<int>> ar( n, std::vector<int>( n ) );

In this case all elements after the declaration will be zero-initialized.

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