"Sunny" <sh************ ***@gmail.comwr ote in message
news:11******** **************@ l77g2000hsb.goo glegroups.com.. .
: #include <iostream>
:
: int main()
: {
: int len;
: std::cin >len;
: int Arr[len];
: int Arr1[2*len];
: int width;
: Arr[len-1]=5;
: std::cout << &len << " " << &Arr << " " << &Arr1<<"" <<&width <<
: std::endl;
: return 0;
:
: }
:
: This code declares two variable size arrays. I am entering len as 20.
: The program produces the following output:
:
: len : 0xbfff99a0
: Arr : 0xbfff9940
: Arr1 : 0xbfff98a0
: width :0xbfff999c
:
: ^
: 8a0 |
: |
: 940 |
: |
: 9a0 |
: |
: 99c |
I would rather represent this as:
99a0 len
999c width
9940 Arr
98a0 Arr1
: So width is allocated 4 bytes, len 0x60 bytes, Arr 0xA0 bytes. I am
: unable to understand how compiler generates size of Arr as 0xA0 bytes
: when it is of length 0x14*4 = 0x50 bytes. And why len gets 0x60
: bytes.
This is not the way that I would interpret the above results:
width gets 4 bytes (sizeof(int) appears to be 4 bytes)
Arr gets 99c-940 = 0x5c = 92 bytes
Arr1 gets 940-8a0 = 0xa0 = 160 bytes
With len=20, the required sizes for the arrays are:
- 20*4 = 80 bytes
- 40*4 = 160 bytes.
There seems to be some kind of padding or other compiler-generated
data between Arr and width, which is perfectly fine (it's probably
for housekeeping data required by the dynamic allocation).
BTW, runtime-sized C arrays are not allocated on the heap
with malloc, but possibly a function/macro called "alloca",
which you may be able to look-up in your compiler's doc.
This of course is all off-topic here, but I wanted to
clear some of the confusion...
I hope this helps,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <
http://www.brainbench.com