2

I'm working on 2D array in C++. I'm having trouble connecting the concepts of arrays and pointers. I know that they are related in terms of memory allocation and accessing elements. For example

int *arr;
int num = arr + 1*sizeof(int);

is the same as

int arr[];
int num = arr[1];

I'm trying to find the same connection between 2D arrays and pointers Here's my code

void printGrid(int **arr) {
 for (int i = 0; i < 10; i++) {
  for (int j = 0; j < 10; j++) {
   cout << setw(3);
   cout << arr[i][j] << " ";
  }
  cout << endl;
 }
}

int main() {
 int **grid;
 srand(time(NULL));
 for (int i = 0; i < 10; i++) {
  for (int j = 0; j < 10; j++) {
   grid[i][j] = rand() % 11;
  }
 }
 printGrid(grid);
}

When I compile this, it compiles. When I then try to run it, I get a segfault. Could someone please point out the error in my code?

Thanks SO

6
  • 2
    "int num = arr + 1*sizeof(int);" is NOT the same thing as arr[1]. Commented Mar 20, 2016 at 21:01
  • 2
    there is no memory allocated to **grid. It is just a pointer. allocate space for it or set it to some size e.g. malloc or declare int grid[10][10]; Commented Mar 20, 2016 at 21:04
  • 1
    There's no 2D array in your code. You have a pointer to a pointer, which is an entirely different thing. Commented Mar 20, 2016 at 21:05
  • Please correct me if I'm wrong but to my understanding, array elements are stored consecutively in memory. arr points to the 0th element of the array. To access the first element, we then have to go (sizeof(int)) locations forward in memory. To access the second element, we must go 2*(sizeof(int)) locations forward in memory. Is that not right? Commented Mar 20, 2016 at 21:06
  • Since arr is a pointer to ints, adding 1 to the pointer advances to the next int sized entry, automatically. Commented Mar 20, 2016 at 21:11

3 Answers 3

3
int **grid;
 srand(time(NULL));
 for (int i = 0; i < 10; i++) {
  for (int j = 0; j < 10; j++) {
   grid[i][j] = rand() % 11;
  }
 }

Where is the part which should allocate memory for the dynamic array? And possibly also for its elements? To remedy this you could have done

// Allocating memory for array and elements
int **grid = new int*[10];
for (int i = 0; i < 10; i++) {
  grid[i] = new int[10];
}
// Now fill the array as you had in your code
//
...
// Deletion part
for (int i = 0; i < 10; i++) {
  delete[] grid[i];
}
delete[] grid;

Also,

I'm working on 2D array in C++. I'm having trouble connecting the concepts of arrays and pointers. I know that they are related in terms of memory allocation and accessing elements. For example

int *arr; int num = arr + 1*sizeof(int);

is the same as

int arr[]; int num = arr[1];

No they are not the same. This would be same though:

int x[] = {0, 2, 3};
int *arr = x;
int num = *(arr + 1); //Look up pointer arithmetic; same as num=arr[1];
Sign up to request clarification or add additional context in comments.

1 Comment

Ok. I see. How exactly do I allocate memory for a 2D array?
1

One big difference between int ** ptrptr and int arr[X][Y] is that ptrptr is a pointer to an int pointer, so it can hold a variable length of int pointers, each of which can represent different sized arrays like:

ptrptr (ptrptr points to the beginning of three different sized arrays) ▼ address_1: 0 1 2 3 address_2: 4 5 address_3: 6 7 8

For the arr variable, the array occuppies contiguous memory, and thus it can be visualized as a rectangle, so think of it like each row must have the same number of elements.

Comments

1

grid is an uninitialized pointer. Attempting to store anything through that pointer will result in undefined behaviour, such as segmentation failure.

Your code would need to look like:

 grid = new (int*) [10];
 for (int i = 0; i < 10; i++) {
     grid[i] = new int[10];
     for (int j = 0; j < 10; j++) {
         grid[i][j] = rand() % 11;
     }
}

(And you should delete the memory you allocated when you are finished)

for (int i = 0; i < 10; i++) {
    delete[] grid[i];
}
delete[] grid;

1 Comment

Thanks. Just a follow-up question, if ou don't mind, how do I delete this 2D dynamically alloccated array. I know that for non-array variables, I have to write delete var; and for arrays I have to write delete [] arr; How do do the same to a 2D array? Thanks

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.