C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Question on memory allocation process of a 2D array

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
KK
Guest





PostPosted: Thu Nov 18, 2004 12:49 am    Post subject: Question on memory allocation process of a 2D array Reply with quote



Greetings,
I am trying to see how the process of memory allocation is exactly
acheived for a two dimensional array. Apparently, there are two cases
I have to understand - static and dynamic. From the understanding I
have so far, the process should be exactly the same in both cases.
Here is the example that proves me wrong.

/* Example to illustrate the processes of memory
allocation of a two dimensional array */
#include<iostream>
using namespace std;
int main(){
/*Static memory allocation */
cout<<"Case : static array allocation:n";
char A[2][3];
cout<<"&A["<<0<<"] = "<<(void *)(&A[0])< cout<<"A[0] ="<<(void *)A[0]<
/*Dynamic memory allocation */
cout<<"Case : dynamic array allocation:n";
char **B=new char*[2];
for(int i = 0; i < 2; i++)
B[i]= new char[3];
cout<<"&B["<<0<<"] = "<<(void *)(&B[0])< cout<<"B[0] ="<<(void *)B[0]< return 0;
}

The output of the program is as follows,
Case : static array allocation:
&A[0] = 0012FF78
A[0] = 0012FF78

Case : dynamic array allocation:
&B[0] = 00320930
B[0] = 00320968

As you might have observed &A[0]==A[0] (static case) where as
&B[0]!=B[0](dynamic case). Could anyone kindly reason out this
inconsistent behaviour?
Thank you in advance,
-KK

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
James Hopkin
Guest





PostPosted: Fri Nov 19, 2004 12:53 pm    Post subject: Re: Question on memory allocation process of a 2D array Reply with quote



[email]kewlkarun (AT) yahoo (DOT) com[/email] (KK) wrote in message news:<c8fd5039.0411171317.2b51f5f8 (AT) posting (DOT) google.com>...
Quote:
Greetings,
I am trying to see how the process of memory allocation is exactly
acheived for a two dimensional array. Apparently, there are two cases
I have to understand - static and dynamic. From the understanding I
have so far, the process should be exactly the same in both cases.
Here is the example that proves me wrong.


The two are not equivalent, the way you have written it. The static
part of your example defines a genuine two dimensional array (in your
case, six contiguous elements). The dynamic part allocates a one
dimensional array of pointers. (Kernighan and Ritchie "The C
Programming Language", 5.9, has a good illustration of the
difference).

The following allocates an equivalent dynamic array:

char (*B)[3] = new char[2][3];

Note, however that B is a pointer to the first array in an array of
arrays, whereas A *is* an array of arrays.

In other words, &A[0] corresponds to B; A[0] corresponds to *B.


James

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Thomas Richter
Guest





PostPosted: Fri Nov 19, 2004 12:53 pm    Post subject: Re: Question on memory allocation process of a 2D array Reply with quote



Hi,

Quote:
From the understanding I
have so far, the process should be exactly the same in both cases.
Here is the example that proves me wrong.

Sure, but your types are different, so why do you expect the same
behaivour?

Quote:
/* Example to illustrate the processes of memory
allocation of a two dimensional array */
#include using namespace std;
int main(){
/*Static memory allocation */
cout<<"Case : static array allocation:n";
char A[2][3];

Here "A" is a two-dimensional array.


Quote:
cout<<"&A["<<0<<"] = "<<(void *)(&A[0])< cout<<"A[0] ="<<(void *)A[0]<
/*Dynamic memory allocation */
cout<<"Case : dynamic array allocation:n";
char **B=new char*[2];

Here "B" is a pointer to a one-dimensional array of pointers.
Note that this is a *different* type than that of the A object.
There is just an implicit conversion that converts "A" objects
to pointer objects so that dereferencing them with [] works as
expected.

Quote:
for(int i = 0; i < 2; i++)
B[i]= new char[3];
cout<<"&B["<<0<<"] = "<<(void *)(&B[0])<

Above, you're printing the address of the A array and the address of
its first element. This is indeed the same as the array starts with
its first element in the memory allocated for it. For the B type,
you're printing the address of the pointer that points to the array of
pointers, i.e. &B is a char ***.

Quote:
cout<<"B[0] ="<<(void *)B[0]<

Here's a bug in your program: You should release all memory you've
allocated. Even if you're just testing, it's good practise to start
with a high disipline.

Quote:
return 0;
}

As you might have observed &A[0]==A[0] (static case) where as
&B[0]!=B[0](dynamic case). Could anyone kindly reason out this
inconsistent behaviour?

Well, you're doing different things, so you should expect different
things; A and B are different types, and completely differently
layed out arrays. While A is a plain and simple array, B is a
pointer to an array of pointers each pointing to an array. Thus,
different results due to different layouts. What *is* confusing is
that you are able to use both types in the same way, i.e. with [][].
However, even though these operations look the same, they do something
completely different.

So long,
Thomas


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Steven Kilby
Guest





PostPosted: Fri Nov 19, 2004 12:59 pm    Post subject: Re: Question on memory allocation process of a 2D array Reply with quote

Quote:
/*Static memory allocation */
cout<<"Case : static array allocation:n";
char A[2][3];
cout<<"&A["<<0<<"] = "<<(void *)(&A[0])< cout<<"A[0] ="<<(void *)A[0]<

This is contiguous.

Quote:
/*Dynamic memory allocation */
cout<<"Case : dynamic array allocation:n";
char **B=new char*[2];
for(int i = 0; i < 2; i++)
B[i]= new char[3];
cout<<"&B["<<0<<"] = "<<(void *)(&B[0])< cout<<"B[0] ="<<(void *)B[0]<

This is not. The variable B has a value that is the address of the memory
allocated with new char*[2]. &B[0] is that same address. B[0] has a value
that is the address of the memory allocated with new char[3]. This is a
different value than the address OF B[0] which was allocated with the new
char*[2].


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Branimir Maksimovic
Guest





PostPosted: Fri Nov 19, 2004 2:54 pm    Post subject: Re: Question on memory allocation process of a 2D array Reply with quote

[email]kewlkarun (AT) yahoo (DOT) com[/email] (KK) wrote in message news:<c8fd5039.0411171317.2b51f5f8 (AT) posting (DOT) google.com>...
Quote:
Greetings,
I am trying to see how the process of memory allocation is exactly
acheived for a two dimensional array. Apparently, there are two cases
I have to understand - static and dynamic. From the understanding I
have so far, the process should be exactly the same in both cases.
Here is the example that proves me wrong.

........
/*Dynamic memory allocation */
cout<<"Case : dynamic array allocation:n";
char **B=new char*[2];

not the same thing; you are allocating array of pointers in
this case

Quote:
for(int i = 0; i < 2; i++)
B[i]= new char[3];
cout<<"&B["<<0<<"] = "<<(void *)(&B[0])< cout<<"B[0] ="<<(void *)B[0]< return 0;

you should do this:
char(*B)[2][3] = (char(*)[2][3])new char[6];
cout<<"&B["<<0<<"] = "<<(void*)(&(*B)[0])< cout<<"B["<<0<<"] = "<<(void*)(*B)[0]<
Greetings, Bane.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Tom Widmer
Guest





PostPosted: Fri Nov 19, 2004 3:03 pm    Post subject: Re: Question on memory allocation process of a 2D array Reply with quote

On 17 Nov 2004 19:49:17 -0500, [email]kewlkarun (AT) yahoo (DOT) com[/email] (KK) wrote:

Quote:
Greetings,
I am trying to see how the process of memory allocation is exactly
acheived for a two dimensional array. Apparently, there are two cases
I have to understand - static and dynamic. From the understanding I
have so far, the process should be exactly the same in both cases.
Here is the example that proves me wrong.

/* Example to illustrate the processes of memory
allocation of a two dimensional array */
#include using namespace std;
int main(){
/*Static memory allocation */
cout<<"Case : static array allocation:n";
char A[2][3];
cout<<"&A["<<0<<"] = "<<(void *)(&A[0])< cout<<"A[0] ="<<(void *)A[0]<
/*Dynamic memory allocation */
cout<<"Case : dynamic array allocation:n";
char **B=new char*[2];
for(int i = 0; i < 2; i++)
B[i]= new char[3];
cout<<"&B["<<0<<"] = "<<(void *)(&B[0])< cout<<"B[0] ="<<(void *)B[0]<

If you want the dynamic allocation to be "equivalent", you should do:

cout<<"Case : dynamic array allocation:n";
typedef char array[2];
char (*B)[2]= new array[3];
cout<<"&B["<<0<<"] = "<<(void *)(&B[0])< cout<<"B[0] ="<<(void *)B[0]<

Quote:
return 0;
}

The output of the program is as follows,
Case : static array allocation:
&A[0] = 0012FF78
A[0] = 0012FF78

Case : dynamic array allocation:
&B[0] = 00320930
B[0] = 00320968

As you might have observed &A[0]==A[0] (static case) where as
&B[0]!=B[0](dynamic case). Could anyone kindly reason out this
inconsistent behaviour?

The inconsistency is in your code - your auto case had an array of 3
arrays of 2 chars, whereas your dynamic case had a dynamic array of 3
dynamic arrays of char, each of size 2. With the above change, you end
up with a dynamic array of 3 arrays of 2 chars, which is roughly
equivalent to the auto case.

Tom

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Chris Uzdavinis
Guest





PostPosted: Fri Nov 19, 2004 3:22 pm    Post subject: Re: Question on memory allocation process of a 2D array Reply with quote

[email]kewlkarun (AT) yahoo (DOT) com[/email] (KK) wrote in message news:<c8fd5039.0411171317.2b51f5f8 (AT) posting (DOT) google.com>...
Quote:
Greetings,
I am trying to see how the process of memory allocation is exactly
acheived for a two dimensional array. Apparently, there are two cases
I have to understand - static and dynamic. From the understanding I
have so far, the process should be exactly the same in both cases.
Here is the example that proves me wrong.

/* Example to illustrate the processes of memory
allocation of a two dimensional array */
#include using namespace std;
int main(){
/*Static memory allocation */
cout<<"Case : static array allocation:n";
char A[2][3];
cout<<"&A["<<0<<"] = "<<(void *)(&A[0])< cout<<"A[0] ="<<(void *)A[0]<

Declaring A here, it is a real array. The memory for all the array is
contiguous. Even though it's a two dimensional array, accessing A[0]
returns to you an array of 3 characters. That array happens to begin
at the same address, since this is all allocated directly on the
stack.


Quote:
/*Dynamic memory allocation */
cout<<"Case : dynamic array allocation:n";
char **B=new char*[2];
for(int i = 0; i < 2; i++)
B[i]= new char[3];
cout<<"&B["<<0<<"] = "<<(void *)(&B[0])< cout<<"B[0] ="<<(void *)B[0]< return 0;
}

Here, B is *not* an array. It is a pointer, and the value you made it
point to is the address returned by your first call to new[]. You are
comparing the address of a pointer against the address stored in that
pointer.

--
Chris

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Steven Kilby
Guest





PostPosted: Fri Nov 19, 2004 3:26 pm    Post subject: Re: Question on memory allocation process of a 2D array Reply with quote

Quote:
/*Static memory allocation */
cout<<"Case : static array allocation:n";
char A[2][3];
cout<<"&A["<<0<<"] = "<<(void *)(&A[0])< cout<<"A[0] ="<<(void *)A[0]<

This is contiguous.

Quote:
/*Dynamic memory allocation */
cout<<"Case : dynamic array allocation:n";
char **B=new char*[2];
for(int i = 0; i < 2; i++)
B[i]= new char[3];
cout<<"&B["<<0<<"] = "<<(void *)(&B[0])< cout<<"B[0] ="<<(void *)B[0]<

This is not. The variable B has a value that is the address of the memory
allocated with new char*[2]. &B[0] is that same address. B[0] has a value
that is the address of the memory allocated with new char[3]. This is a
different value than the address OF B[0] which was allocated with the new
char*[2].


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Steven Kilby
Guest





PostPosted: Fri Nov 19, 2004 3:27 pm    Post subject: Re: Question on memory allocation process of a 2D array Reply with quote

Quote:
/*Static memory allocation */
cout<<"Case : static array allocation:n";
char A[2][3];
cout<<"&A["<<0<<"] = "<<(void *)(&A[0])< cout<<"A[0] ="<<(void *)A[0]<

This is contiguous.

Quote:
/*Dynamic memory allocation */
cout<<"Case : dynamic array allocation:n";
char **B=new char*[2];
for(int i = 0; i < 2; i++)
B[i]= new char[3];
cout<<"&B["<<0<<"] = "<<(void *)(&B[0])< cout<<"B[0] ="<<(void *)B[0]<

This is not. The variable B has a value that is the address of the memory
allocated with new char*[2]. &B[0] is that same address. B[0] has a value
that is the address of the memory allocated with new char[3]. This is a
different value than the address OF B[0] which was allocated with the new
char*[2].


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Allan W
Guest





PostPosted: Fri Nov 19, 2004 4:08 pm    Post subject: Re: Question on memory allocation process of a 2D array Reply with quote

[email]kewlkarun (AT) yahoo (DOT) com[/email] (KK) wrote
Quote:
I am trying to see how the process of memory allocation is exactly
acheived for a two dimensional array.

C++ doesn't directly support two-dimensional arrays, but it does support
an array of arrays, which has much the same effect.

Quote:
Apparently, there are two cases
I have to understand - static and dynamic. From the understanding I
have so far, the process should be exactly the same in both cases.
Here is the example that proves me wrong.

/* Example to illustrate the processes of memory
allocation of a two dimensional array */
#include using namespace std;
int main(){
/*Static memory allocation */
cout<<"Case : static array allocation:n";
char A[2][3];
cout<<"&A["<<0<<"] = "<<(void *)(&A[0])< cout<<"A[0] ="<<(void *)A[0]<

A is an array of 2 arrays of 3 characters each.

Quote:
Case : static array allocation:
&A[0] = 0012FF78
A[0] = 0012FF78


Quote:
/*Dynamic memory allocation */
cout<<"Case : dynamic array allocation:n";
char **B=new char*[2];
for(int i = 0; i < 2; i++)
B[i]= new char[3];
cout<<"&B["<<0<<"] = "<<(void *)(&B[0])< cout<<"B[0] ="<<(void *)B[0]< return 0;
}

But this is different. Here you're allocating an array of
2 character pointers and two arrays of 3 characters.
(The delete code, which you don't show, will also require
you to do a delete for each NEW).

Quote:
Case : dynamic array allocation:
&B[0] = 00320930
B[0] = 00320968

&B[0] gives you the address of the first pointer.
B[0] gives you the value of the first pointer.

To do the same as the first approach, you would have to write:

/* Dynamic allocation of a 2-by-3 array */
char (*C)[2][3] = new char[][2][3];
cout<<"&C[0] = "<<(void *)(&C[0])< cout<<" C[0] = "<<(void *)C[0]< delete[] C;

&C[0] = 00322C30
C[0] = 00322C30

Better?

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Antoun Kanawati
Guest





PostPosted: Fri Nov 19, 2004 4:49 pm    Post subject: Re: Question on memory allocation process of a 2D array Reply with quote

[email]kewlkarun (AT) yahoo (DOT) com[/email] (KK) wrote in message
news:<c8fd5039.0411171317.2b51f5f8 (AT) posting (DOT) google.com>...
Quote:
Greetings,
I am trying to see how the process of memory allocation is exactly
acheived for a two dimensional array. Apparently, there are two cases
I have to understand - static and dynamic. From the understanding I
have so far, the process should be exactly the same in both cases.
Here is the example that proves me wrong.

Actually, there are three cases: dynamic, automatic, and static.
And this is pretty much for anything involving 'allocation'.

As for 'process', it depends on what you mean by the word; and
how to interpret 'exactly the same'.

In C++, 2D array are stored in row-major order, in a contiguous
chunk of memory. That's about all the sameness that I can
think of.

To allocate a static or automatic 2D array, you say something like:

char a[4][5];

The 'static' process is to reserve the space at compile time, and
merely point the symbol to it. You can expect the 20 chars to
be zeroed out.

The 'automatic' process involves a runtime adjustment of the stack
pointer. The 20 chars will hold random junk in this case.

To do a dynamic allocation:

typedef char char5[5];
char5 *a = new char5[4]; // or
char5 *a = new char[4][5];

In a well-behaved program, a corresponding delete must occur in the
code.

In all cases, we're allocating 20 chars, and interpreting the chunk
as four rows of 5-chars each. In the dynamic case, you can avoid the
typedef by doing some awkward parenthesization in the next line.

However, as you can see, the "process" of creating the 2D array is
not exactly the same, although the layout of the allocated store
is the same.
--
A. Kanawati
[email]NO.antounk.SPAM (AT) comcast (DOT) net[/email]

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Luke Nowak
Guest





PostPosted: Fri Nov 19, 2004 4:53 pm    Post subject: Re: Question on memory allocation process of a 2D array Reply with quote

KK wrote:

Quote:
Greetings,
I am trying to see how the process of memory allocation is exactly
acheived for a two dimensional array. Apparently, there are two cases
I have to understand - static and dynamic. From the understanding I
have so far, the process should be exactly the same in both cases.
Here is the example that proves me wrong.

/* Example to illustrate the processes of memory
allocation of a two dimensional array */
#include using namespace std;
int main(){
/*Static memory allocation */
cout<<"Case : static array allocation:n";
char A[2][3];
cout<<"&A["<<0<<"] = "<<(void *)(&A[0])< cout<<"A[0] ="<<(void *)A[0]<
/*Dynamic memory allocation */
cout<<"Case : dynamic array allocation:n";
char **B=new char*[2];
for(int i = 0; i < 2; i++)
B[i]= new char[3];
cout<<"&B["<<0<<"] = "<<(void *)(&B[0])< cout<<"B[0] ="<<(void *)B[0]< return 0;
}

The output of the program is as follows,
Case : static array allocation:
&A[0] = 0012FF78
A[0] = 0012FF78

Case : dynamic array allocation:
&B[0] = 00320930
B[0] = 00320968

As you might have observed &A[0]==A[0] (static case) where as
&B[0]!=B[0](dynamic case). Could anyone kindly reason out this
inconsistent behaviour?
Thank you in advance,

Well,

in static case, the array elements are in the memory as follows

.... array A

[0][0]
[0][1]
[0][2]
[1][0]
[1][1]
[1][2]


in dynamic case:

.... B array of char *

[0] ... points to aaa
[1] ... points to bbb

.... then aaa

[0]
[1]
[2]

.... and then bbb

[0]
[1]
[2]

as you can see you allocate dynamicly an array of pointers, and then
separate arrays of chars, so in memory they are put separately

Greetings,
Luke

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.