By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,750 Members | 1,221 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,750 IT Pros & Developers. It's quick & easy.

Arrays in C++

P: n/a
#include <iostream>

int main()
{
int len;
std::cin >len;
int Arr[len];
int *p = new int[len];
Arr[len-1]=5;
std::cout << &len << " " << &Arr << " " << p << std::endl;
return 0;
}

This code declares an array Arr of size len which is not fixed at
compile time. This program compiles using g++ (gcc version 3.2.3
20030502) and works properly. I am surprised to see this because I
thought arrays in C++ should have size fixed at compile time.
Is g++ deviating from standards here?

Mar 22 '07 #1
Share this Question
Share on Google+
11 Replies


P: n/a
Sunny wrote:
#include <iostream>

int main()
{
int len;
std::cin >len;
int Arr[len];
int *p = new int[len];
Arr[len-1]=5;
std::cout << &len << " " << &Arr << " " << p << std::endl;
return 0;
}

This code declares an array Arr of size len which is not fixed at
compile time. This program compiles using g++ (gcc version 3.2.3
20030502) and works properly. I am surprised to see this because I
thought arrays in C++ should have size fixed at compile time.
Is g++ deviating from standards here?
Yes, gcc supports C99 style variable length arrays.

--
Ian Collins.
Mar 22 '07 #2

P: n/a
Ian Collins wrote:
Sunny wrote:
>>#include <iostream>

int main()
{
int len;
std::cin >len;
int Arr[len];
int *p = new int[len];
Arr[len-1]=5;
std::cout << &len << " " << &Arr << " " << p << std::endl;
return 0;
}

This code declares an array Arr of size len which is not fixed at
compile time. This program compiles using g++ (gcc version 3.2.3
20030502) and works properly. I am surprised to see this because I
thought arrays in C++ should have size fixed at compile time.
Is g++ deviating from standards here?

Yes, gcc supports C99 style variable length arrays.
I should have added "as an extension to ", not deviating from the standard.

--
Ian Collins.
Mar 22 '07 #3

P: n/a
On Mar 22, 3:05 pm, Ian Collins <ian-n...@hotmail.comwrote:
Ian Collins wrote:
Sunny wrote:
>#include <iostream>
>int main()
{
int len;
std::cin >len;
int Arr[len];
int *p = new int[len];
Arr[len-1]=5;
std::cout << &len << " " << &Arr << " " << p << std::endl;
return 0;
}
>This code declares an array Arr of size len which is not fixed at
compile time. This program compiles using g++ (gcc version 3.2.3
20030502) and works properly. I am surprised to see this because I
thought arrays in C++ should have size fixed at compile time.
Is g++ deviating from standards here?
Yes, gcc supports C99 style variable length arrays.

I should have added "as an extension to ", not deviating from the standard.

--
Ian Collins.- Hide quoted text -

- Show quoted text -
Thanks for the reply, Ian.

Mar 22 '07 #4

P: n/a
On Mar 22, 3:09 pm, "Sunny" <shiladitya.bis...@gmail.comwrote:
On Mar 22, 3:05 pm, Ian Collins <ian-n...@hotmail.comwrote:


Ian Collins wrote:
Sunny wrote:
>>#include <iostream>
>>int main()
>>{
> int len;
> std::cin >len;
> int Arr[len];
> int *p = new int[len];
> Arr[len-1]=5;
> std::cout << &len << " " << &Arr << " " << p << std::endl;
> return 0;
>>}
>>This code declares an array Arr of size len which is not fixed at
>>compile time. This program compiles using g++ (gcc version 3.2.3
>>20030502) and works properly. I am surprised to see this because I
>>thought arrays in C++ should have size fixed at compile time.
>Is g++ deviating from standards here?
Yes, gcc supports C99 style variable length arrays.
I should have added "as an extension to ", not deviating from the standard.
--
Ian Collins.- Hide quoted text -
- Show quoted text -

Thanks for the reply, Ian.- Hide quoted text -

- Show quoted text -
#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 |

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.

Thanks

Mar 22 '07 #5

P: n/a
On 22 Mar, 13:04, "Sunny" <shiladitya.bis...@gmail.comwrote:
On Mar 22, 3:09 pm, "Sunny" <shiladitya.bis...@gmail.comwrote:
On Mar 22, 3:05 pm, Ian Collins <ian-n...@hotmail.comwrote:
Ian Collins wrote:
Sunny wrote:
>#include <iostream>
>int main()
>{
int len;
std::cin >len;
int Arr[len];
int *p = new int[len];
Arr[len-1]=5;
std::cout << &len << " " << &Arr << " " << p << std::endl;
return 0;
>}
>This code declares an array Arr of size len which is not fixed at
>compile time. This program compiles using g++ (gcc version 3.2.3
>20030502) and works properly. I am surprised to see this because I
>thought arrays in C++ should have size fixed at compile time.
Is g++ deviating from standards here?
Yes, gcc supports C99 style variable length arrays.
I should have added "as an extension to ", not deviating from the standard.
--
Ian Collins.- Hide quoted text -
- Show quoted text -
Thanks for the reply, Ian.- Hide quoted text -
- Show quoted text -

#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 |

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.
It's a pointer, it does not matter how large the array is since it's
not allocated on the stack, which the other variables (len, width).
Arr must be a pointer since the compiler doesn't know at compile-time
how much space to reserve on the stack for the array, so it makes room
for an array and handles the allocation and deallocation for you. So
basically it's just a compiler generated wrapper around

int* Arr;
arr = new int[len];

// At end of scope
delete[] Arr;

--
Erik Wikström

Mar 22 '07 #6

P: n/a
#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 |

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.
How the compiler works internally is implementation dependent. Most
likely, the extra are housekeeping bytes. If exception handling is
turned on/off, you will probably notice other differences too.

If you would like more compiler specific answers, you will need to go to
a more compiler specific newsgroup. This is an ANSI C++ group AFAIK ;),
and is not worried so much about the internals.

Good luck.
Adrian
--
__________________________________________________ ___________________
\/Adrian_Hawryluk BSc. - Specialties: UML, OOPD, Real-Time Systems\/
\ My newsgroup writings are licensed under a Creative Commons /
\ Attribution-Share Alike 3.0 License /
\_______[http://creativecommons.org/licenses/by-sa/3.0/]______/
\/_______[blog:_http://adrians-musings.blogspot.com/]______\/
Mar 22 '07 #7

P: n/a
Sunny wrote:
#include <iostream>

int main()
{
int len;
std::cin >len;
int Arr[len];
int *p = new int[len];
Arr[len-1]=5;
std::cout << &len << " " << &Arr << " " << p << std::endl;
return 0;
}

This code declares an array Arr of size len which is not fixed at
compile time. This program compiles using g++ (gcc version 3.2.3
20030502) and works properly. I am surprised to see this because I
thought arrays in C++ should have size fixed at compile time.
Is g++ deviating from standards here?
If you turn on -Wall -ansi -pendatic, then you will see a warning that
you are using variable length array.

Fei
Mar 22 '07 #8

P: n/a
On Mar 22, 6:19 pm, "Erik Wikström" <eri...@student.chalmers.se>
wrote:
On 22 Mar, 13:04, "Sunny" <shiladitya.bis...@gmail.comwrote:


On Mar 22, 3:09 pm, "Sunny" <shiladitya.bis...@gmail.comwrote:
On Mar 22, 3:05 pm, Ian Collins <ian-n...@hotmail.comwrote:
Ian Collins wrote:
Sunny wrote:
>>#include <iostream>
>>int main()
>>{
> int len;
> std::cin >len;
> int Arr[len];
> int *p = new int[len];
> Arr[len-1]=5;
> std::cout << &len << " " << &Arr << " " << p << std::endl;
> return 0;
>>}
>>This code declares an array Arr of size len which is not fixed at
>>compile time. This program compiles using g++ (gcc version 3.2.3
>>20030502) and works properly. I am surprised to see this because I
>>thought arrays in C++ should have size fixed at compile time.
>Is g++ deviating from standards here?
Yes, gcc supports C99 style variable length arrays.
I should have added "as an extension to ", not deviating from the standard.
--
Ian Collins.- Hide quoted text -
- Show quoted text -
Thanks for the reply, Ian.- Hide quoted text -
- Show quoted text -
#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 |
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.

It's a pointer, it does not matter how large the array is since it's
not allocated on the stack, which the other variables (len, width).
Arr must be a pointer since the compiler doesn't know at compile-time
how much space to reserve on the stack for the array, so it makes room
for an array and handles the allocation and deallocation for you. So
basically it's just a compiler generated wrapper around

int* Arr;
arr = new int[len];

// At end of scope
delete[] Arr;

--
Erik Wikström- Hide quoted text -

- Show quoted text -
Hi Eric,
I investigated further and printed out &Arr[0], &Arr[1],.... These
addresses looked like stack addresses ad not heap addresses. So it is
unlikely that compiler is using new.

Mar 22 '07 #9

P: n/a
Sunny wrote:
>
Hi Eric,
I investigated further and printed out &Arr[0], &Arr[1],.... These
addresses looked like stack addresses ad not heap addresses. So it is
unlikely that compiler is using new.
You are right. It is allocating it on the stack. The sizes and address
locations indicate such. However, this is not (currently) in the C++
spec, this is (as Ian stated) an extension. Do this at your own peril.
If you move to another compiler, it may not work.
Adrian
--
__________________________________________________ ___________________
\/Adrian_Hawryluk BSc. - Specialties: UML, OOPD, Real-Time Systems\/
\ _---_ Q. What are you doing here? _---_ /
\ / | A. Just surf'n the net, teaching and | \ /
\__/___\___ learning, learning and teaching. You?_____/___\__/
\/______[blog:__http://adrians-musings.blogspot.com/]______\/
Mar 22 '07 #10

P: n/a
"Sunny" <sh***************@gmail.comwrote in message
news:11**********************@l77g2000hsb.googlegr oups.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

Mar 23 '07 #11

P: n/a
On Mar 23, 2:25 pm, "Ivan Vecerina"
<_INVALID_use_webfo...@ivan.vecerina.comwrote:
"Sunny" <shiladitya.bis...@gmail.comwrote in message

news:11**********************@l77g2000hsb.googlegr oups.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
Thanks Ian,
You are right. I had interpreted sizes incorrectly. I will look at the
alloca() call.

Mar 23 '07 #12

This discussion thread is closed

Replies have been disabled for this discussion.