 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
KK Guest
|
Posted: Fri Nov 19, 2004 3:37 pm Post subject: Question on static and dynamic memory allocation |
|
|
Greetings,
I am trying to undersand the process of static and dynamic memory
allocation for a two dimensional array.
/* 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 |
|
 |
jjr2004a Guest
|
Posted: Tue Nov 23, 2004 10:45 am Post subject: Re: Question on static and dynamic memory allocation |
|
|
[email]kewlkarun (AT) yahoo (DOT) com[/email] (KK) wrote in message news:<c8fd5039.0411182132.1aab5b9f (AT) posting (DOT) google.com>...
| Quote: | Greetings,
I am trying to undersand the process of static and dynamic memory
allocation for a two dimensional array.
/* 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,
|
The behavior is not inconsistent at all. By taking a look at
how the memory is laid out you can see why.
A is an array of six characters laid out in contiguous memory
like this:
+-+-+-+-+-+-+
| Quote: | 0|1|2|3|4|5|
+-+-+-+-+-+-+ |
B is slightly more complicated layout. It consists of space
for three pointers and space for six characters which are
not contiguous.
B B[0]
+-+ +-+ +-+-+-+
| Quote: | *--->|*--->|0|1|2|
+-+ +-+ +-+-+-+ +-+-+-+
*------------>|3|4|5|
+-+ +-+-+-+ |
B[1]
From this layout you can deduce that B == &B[0], B[0] == &B[0][0]
and B[1] == &B[1][0] by definition.
This is really a C question. I've found the book "Advanced C Tips
and Techniques", by Kochan & Wood explains this stuff in depth.
It may be out of print but they seem to have a website and it may
be available from there for download.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
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
|
|