0

When we create an array, for example int arr[2][4];, its size becomes 2x4x4 = 32, but if we do this dynamically, its size becomes more then 32.

For instance, I wrote this code:

#include <iostream>
#include <stdlib.h>

using namespace std;
// arr[4][2]
int main(){
    int **arr;
    arr = (int**)malloc(4*sizeof(int*));
    
    int i;
    for(i=0;i<4;i++){
        *(arr+i) = (int*)malloc(2*sizeof(int));
    }
    arr[3][1] = 4;
    cout << arr[3][1];


    return 0;
}

There are 8 + 4x8 + 4x2x4 = 72 bytes allocated. That's why I thought the program is doing different and I wonder how it does this.

7
  • 1
    Because, in a true array, there are no lookups for each row: all the elements are contiguous. Commented May 26, 2021 at 23:03
  • 2
    In C++ you should use new, not malloc(). Commented May 26, 2021 at 23:05
  • What do you mean "not dynamic"? Your code does dynamic allocation. A static array definition just reserves a contogous array of data elements at it's scope (local, or global), why would you expact these sizes to be equal ? Commented May 26, 2021 at 23:07
  • 1
    @πάνταῥεῖ I think the title means he's wondering about int arr[2][4]; Commented May 26, 2021 at 23:09
  • 1
    Yes, but because the elements are contiguous, the compiler needs only the base address to compute the location of any element. But when you allocated the array of pointers, the allocation for each row has no proximity to the other rows, and so a separate pointer was used for each row. Commented May 26, 2021 at 23:42

2 Answers 2

3

What you've done here isn't exactly a 2D array although it behaves like one syntactically. What you instead have is an array of pointers, and each of those pointers points to an array of int.

If you want to allocate an actually 2D array you would do so like this:

int (*arr)[2] = malloc(4 * sizeof *arr);
Sign up to request clarification or add additional context in comments.

1 Comment

What if I want to allocate a 3D array?
1

int arr[4][2] allocates a single block of memory, with indexes laid out like this:

0,0 0,1
1,0 1,1
2,0 2,1
3,0 3,1

Each element is 4 bytes to hold an int.

In your dynamic allocation method, you have 8 bytes for the arr variable itself. Then the first malloc() allocates an array like this:

ptr0 ptr1 ptr2 ptr3

This is 4x8 bytes.

Then you call malloc(2 * sizeof(int)) 4 times, which allocates 4x2x4 bytes.

4 Comments

The first thing you said "int arr[4][2] allocates a single block of memory" is okay but then how arr behaves like a pointer of pointer. If it behaves like a pointer of pointer, I should be able to do it dynamically.
If an array is used in a context where a pointer is needed, its value decays to a pointer to its first element.
So if you write arr+1, it acts like an int[2]* pointer. Or if you pass it as a function argument.
@AhmetTalhaYıldırım: You may want to read this: What is array to pointer decay?

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.