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

array declaration, is this legal?

P: n/a
Hi,

I wonder if this is a legal array declaration?

const int size = 5;
double array[size];

Or should I use "new"?

Thanks.

Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
b8*******@yahoo.com wrote:
I wonder if this is a legal array declaration?

const int size = 5;
double array[size];
Yes, it is. 'size' is a compile-time constant expression.
Or should I use "new"?


Up to you.

V
Jul 23 '05 #2

P: n/a

<b8*******@yahoo.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
Hi,

I wonder if this is a legal array declaration?

const int size = 5;
double array[size];


Yes, it's legal, because it uses a const int initialized by a constant
expression. This allows the compielr to treat it as a compile-time
constant, just as if you'd simply used the constant expression 5 directly in
the array declaration.

-Howard
Jul 23 '05 #3

P: n/a

b8*******@yahoo.com wrote:
Hi,

I wonder if this is a legal array declaration?

const int size = 5;
double array[size];

Or should I use "new"?

Thanks.


You should use the constant expression for a statically-sized array
since the number of bytes needed is known at compile time. Using new in
this situation incurs unnecessary overhead at runtime; worse, it
imposes on the program a requirement to call delete on the array, which
if neglected, will cause memory to leak.

In general, use new to allocate arrays that are either too large to be
allocated on the stack or whose size is not known until runtime.

Greg

Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.