H
hall
I have a question regarding where memory is allocated when arrays are
created. I'll illustrate this by example. I may be wrong on some
details, do feel free to correct me.
The code piece:
int p[3][3];
Creates a 2dimensional array. p can be thought of as a pointer,
containing the adres of the first element in the array. The memory is
allocated in one sequence, so
p[x][y];
would mean: 'the integer at adress p + (x + 3*y)*sizeof(int)'. So far i
understand how memory is allocated, but what happens when I dynamically
allocate a 2d array?
int **n;
*n=new int[3];
for (int i=0; i<3; i++) n=new int[3];
Here, the memory can't be in a sequence since the compiler at line (2)
impossably can know how much memory will be allocated for each of the
pointers p. So my best guess for the memory allocation here is that
line (2) allocates an array of three pointer-to-int and stores the
adress of the first element in p. In line (3), each of these pointers in
p are set to point at the first element of an array of three int:s.
These three int-arrays need not be allocated anywhere near each other,
right? The memory would therefor be allocated in a very different way
than the simple int p[3][3] array, and i can accept this as I am now
dealing with pointers and not an array, but what does
n[x][y]
mean? how is this interpreted? Has the [] operators been overloaded or
am I totally wrong in my guess for where memory is allocated?
Also, the dynamically callocated array would need mor memory, wouldn't
it? apart form the 9 stored integers (18 bytes), it would also require
4*4 bytes for the pointers n and n (on a Win32 machine, the memory
adress is 4 bytes, isn't it?)
regards
hall
created. I'll illustrate this by example. I may be wrong on some
details, do feel free to correct me.
The code piece:
int p[3][3];
Creates a 2dimensional array. p can be thought of as a pointer,
containing the adres of the first element in the array. The memory is
allocated in one sequence, so
p[x][y];
would mean: 'the integer at adress p + (x + 3*y)*sizeof(int)'. So far i
understand how memory is allocated, but what happens when I dynamically
allocate a 2d array?
int **n;
*n=new int[3];
for (int i=0; i<3; i++) n=new int[3];
Here, the memory can't be in a sequence since the compiler at line (2)
impossably can know how much memory will be allocated for each of the
pointers p. So my best guess for the memory allocation here is that
line (2) allocates an array of three pointer-to-int and stores the
adress of the first element in p. In line (3), each of these pointers in
p are set to point at the first element of an array of three int:s.
These three int-arrays need not be allocated anywhere near each other,
right? The memory would therefor be allocated in a very different way
than the simple int p[3][3] array, and i can accept this as I am now
dealing with pointers and not an array, but what does
n[x][y]
mean? how is this interpreted? Has the [] operators been overloaded or
am I totally wrong in my guess for where memory is allocated?
Also, the dynamically callocated array would need mor memory, wouldn't
it? apart form the 9 stored integers (18 bytes), it would also require
4*4 bytes for the pointers n and n (on a Win32 machine, the memory
adress is 4 bytes, isn't it?)
regards
hall