Baltatu Andrei Mircea
Baltatu Andrei Mircea

Reputation: 27

Multidimensional arrays memory storage overview

Lately I started learning pointers and references in c++ (not just the usual use of them,but all kinds of ways,I don't want to have problems with them in near future).

From my perspective a 1d static allocated array is similar to a int* const pointer. My problem is that ,when I allocate dynamically with a pointer, the pointer itself has other memory address than the first element,which is not the case in a 1d array. This is an example

int a[5];
cout<<&a<<" "<<&a[0]<<"\n";
int* t=new int[10];
cout<<&t<<" "<<&t[0]<<"\n";

The output is:

0x6afed0 0x6afed0
0x6afecc 0xcb0e40

I can't think how the 1d array is stored,at 1 block of 4 bytes you can store either a value or an address. I tried also with 2d arrays,and there are more intersections.

q = new int*[10];
for (i=0;i<10;i++)
    q[i] = new int[i+1];
cout<<&q<<" "<<&q[0]<<" "<<&q[0][0]<<"\n";
int b[10][10];
cout<<&b<<" "<<&b[0]<<" "<<&b[0][0]<<"\n";

Output:

0x6afee4 0xe40e40 0xe41240
0x6afd54 0x6afd54 0x6afd54

If someone would enlighten me and explain how these arrays are stored,I would be thankful.

EDIT:To be more specific, I know and it's logical how the array dynamically created with new keyword by the pointer is stored. But how &a and &a[0] have the same values,if a should store the address of a[0] .That block of memory(&a) stores what then?the value of a[0] or the address of a[0]. Hope I was clear in what my problem is.

Upvotes: 0

Views: 248

Answers (4)

Aadil Bhatti
Aadil Bhatti

Reputation: 81

t is a pointer declared on the stack which holds the address of an array on the heap. &t gets the address of t, AKA the location of t on the stack. If you want to access the first member of the array, you can either do *t or t[0]. C++ automatically dereferences and does pointer arithmetic based on the type of t when you use the bracket notation.

However, &t[0] essentially gets you the address of t[0], which is why it is different from &t, as &t lives on the stack while &t[0] lives on the heap. So if you want the address of t[0], you can use that notation, otherwise stick to t[0] if you just want the values.

Upvotes: 1

kfsone
kfsone

Reputation: 24249

Arrays are first-class entities of the language, so when you declare an array variable

int a[10];

the compiler knows the dimensions of the array. When you allocate a dynamic storage space

int* p = new int[10];

Only you and the allocator know how large this space is.

In the array version, a is the array. It has an address, but that address is held by the compiler during compilation. In the allocation pardigm, p is the address of the data, which won't be known until run time.

What causes people to be confused is that by and large the two seem interchangeable: you can do pointer and array math with both, use either where a pointer is expected:

void f(int*);

int a1 = a[1];
f(a);
int p1 = p[1];
f(p);

This is by design, it's something called array-pointer-equivalence, emphasis mine:

Much of the confusion surrounding arrays and pointers in C can be traced to a misunderstanding of this statement. Saying that arrays and pointers are ''equivalent'' means neither that they are identical nor even interchangeable. What it means is that array and pointer arithmetic is defined such that a pointer can be conveniently used to access an array or to simulate an array. In other words, as Wayne Throop has put it, it's ''pointer arithmetic and array indexing [that] are equivalent in C, pointers and arrays are different.'')

Specifically, the cornerstone of the equivalence is this key definition:

A reference to an object of type array-of-T which appears in an expression decays (with three exceptions) into a pointer to its first element; the type of the resultant pointer is pointer-to-T.

Upvotes: 1

NathanOliver
NathanOliver

Reputation: 180500

An array is an object that contains a contiguous block of N objects where N is some constant size. The address of the array is the start of the array so the address of the first object and the addresss of the array are the same thing.

A pointer to an array is a unique object. Since it is it's own object it has its own address separate from the address that it holds. so with int* t=new int[10]; &t is the address of t where &t[0] is the address of the first element of the array that was allocated by new.

Upvotes: 1

Rakete1111
Rakete1111

Reputation: 48938

&t is taking the address of the pointer t, not the address of the first element. The address of the first element is just t (as t is an int*, which points to the block of memory).

Upvotes: 0

Related Questions