1

I am trying to create a 2D dynamic array using pointers:

int** Grid;
Grid = new int*[5];
for (int i=0;i<5;i++)   { Grid[i] = new int[5]; }
std::cout << Grid[4][300] << std::endl;   // output: -17891602
std::cout << Grid[5][0] << std::endl;     // access violation

I need the array to be of a specific size, which is why I am not using a static array. As you can see, although I am making a 5x5 array, debugger did not give error when trying to read Grid[4][300]. Can someone please enlighten me as to why is this so?

This only appears to be so for the 2nd dimension. (Trying to read Grid[5][0] would give the access violation error) Correct me if I am wrong, but technically I am really creating an array of pointers?

I expect an exception for Grid[5][0]. What I really don't get is why no error for Grid[4][300]. Can someone advise me how do I have a dynamic array with, say dimension of 5x5 ?

3 Answers 3

1

You never initialized Grid[5], in your for-loop you checked for i < 5.
If i is under 5 then it will never be used as 5 inside your for-loop.

Just remember that arrays count 0 as a position.

This means that int arr[5] could only be indexed with 0-4, anything more or less would get an access violation.


The reason why [300] isn't throwing an access violation is because your indexing a pointer, rather than a defined array.


Also, don't forget to initialize the values in your array (with most probably 0). You might not want unexpected numbers like -1994021 showing up in your data.
memset is pretty good at doing this.

Sign up to request clarification or add additional context in comments.

2 Comments

yes, i am expecting an exception for Grid[5][0]. What I really don't get is why not for Grid[4][300]. My follow up question will be how do I have a defined array? I cannot use a static array as I need the size of the array to be determined by user-end.
If it's determined user-end then just keep the length. If you try use Grid[4][300] and 300 is over the length then make your own error. "The reason why [300] isn't throwing an access violation is because your indexing a pointer, rather than a defined array."
1

The problem is that an array counts 0 aswell. So the very first memory space in the array would be [0][0], provided you initialized it with for (int i = 0; i < Size; ++i)

Consider this code:

#include <iostream>

using namespace std;

int main () {
    int Size = 5;
    int **Grid;
    Grid = new int* [Size];
    for (int i = 0; i < Size; ++i)
        Grid[i] = new int [Size];

    for (int i = 0; i < Size; ++i)
        for (int j = 0; j < Size; ++j)
            cin >> Grid[i][j];

    cout << Grid[4][4];
    return 0;
}

If you put this into your IDE and compile it, it should print the value of the very last element. That is because you have the following places of the array:

Grid[0][0];
Grid[0][1];
Grid[0][2];
Grid[0][3];
Grid[0][4];
...
Grid[4][4];

Also keep in mind in dynamic allocation of any kind of an array, you must also provide a delete [].

So the code I placed would correctly look like this:

#include <iostream>

using namespace std;

int main () {
    int Size = 5;
    int **Grid;
    Grid = new int* [Size];
    for (int i = 0; i < Size; ++i)
        Grid[i] = new int [Size];

    for (int i = 0; i < Size; ++i)
        for (int j = 0; j < Size; ++j)
            cin >> Grid[i][j];

    cout << Grid[4][4];

    for (int i = 0; i < Size; ++i)
        delete [] Grid[i];
    delete [] Grid;
    return 0;
}

This is because you are working with a Stack. -> http://en.wikipedia.org/wiki/Stack_%28abstract_data_type%29

I hope this clarifies some things for you. Best of luck!

Comments

0

In c and c++ there is no compilation error as well as runtime error if you tried to access a larger index than declared
Eg.

int a[10];
std::cout<< a[20]; // will print some garbage value

So this answers why std::cout << Grid[4][300] << std::endl; is giving output: -17891602 as the second dimension is just array of integers.

Whereas if you declare an array of pointers and tried to access a greater index, then you will get either access violation error or segmentation fault at runtime
Eg.

int *a;
a = new int[5];
std::cout<< a[20]; // will produce segmentation fault

So this answers why std::cout << Grid[5][0] << std::endl; or any value in first dimention greater than declared value will produce an error as the first dimention should be a pointer to some legal location holding an integer value
But while trying to access a greater index which might produce a garbage value which will be an illegal address, thus resulting in segmentation fault.
Hope this helps.

1 Comment

@twenty49 the pleasure is all mine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.