Arrays in C++

S

Sunny

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

Ian Collins

Sunny said:
#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

Ian Collins

Ian said:
Sunny said:
#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.
 
S

Sunny

Ian said:
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.

Thanks for the reply, Ian.
 
S

Sunny

Ian said:
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.
- 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
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

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.
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;
 
A

Adrian Hawryluk

#include said:
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/]______\/
 
F

Fei Liu

Sunny said:
#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
 
S

Sunny

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;

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.
 
A

Adrian Hawryluk

Sunny said:
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/]______\/
 
I

Ivan Vecerina

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

Sunny

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,781
Messages
2,569,616
Members
45,306
Latest member
TeddyWeath

Latest Threads

Top