Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > array declaration, is this legal?

Reply
Thread Tools

array declaration, is this legal?

 
 
b83503104@yahoo.com
Guest
Posts: n/a
 
      07-12-2005
Hi,

I wonder if this is a legal array declaration?

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

Or should I use "new"?

Thanks.

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      07-12-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
 
 
 
Howard
Guest
Posts: n/a
 
      07-12-2005

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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


 
Reply With Quote
 
Greg
Guest
Posts: n/a
 
      07-14-2005

(E-Mail Removed) 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const and array of array (of array ...) Mara Guida C Programming 3 09-03-2009 07:54 AM
length of an array in a struct in an array of structs in a struct in an array of structs Tuan Bui Perl Misc 14 07-29-2005 02:39 PM
Length of Array of Array of Array Tom Perl Misc 3 12-20-2004 05:23 PM
How to combine 2 int Array into ONE int Array ? S300 Java 4 08-19-2003 07:04 PM
hashed array in array need the keys... and length Daniel Perl 1 08-14-2003 06:49 PM



Advertisments