473,402 Members | 2,064 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Array size as constant expression

The number of elemets of the array, the array bound must be constant
expression?Why is this restriction?

Vinodh

Jul 22 '05 #1
9 3913
pv**********@gmail.com wrote:
The number of elemets of the array, the array bound must be constant
expression?Why is this restriction?


Because it's better (easier) that way.
Jul 22 '05 #2
Easier in case of the compiler implementation?
Is it that difficult?I really don't know.

Jul 22 '05 #3
pv**********@gmail.com wrote:
The number of elemets of the array, the array bound must be constant
expression?Why is this restriction?


Firstly, because the current object model of C++ language requires that
sizes of objects with static or automatic storage duration is known at
compile time. Memory layout for static and automatic memory in C++ is
determined at compile time. In order to do that the compiler needs to
know the sizes of all objects, including arrays. (This condition was
weakened in new C - C99, but it is still there in C++).

Secondly, there's no such requirement for dynamic objects. For this
reason, array size in 'new[]' expression is not required to be a
constant expression.

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #4
pv**********@gmail.com wrote:
Easier in case of the compiler implementation?
Is it that difficult?I really don't know.


What would be the size of an object which as a member has an array whose
size is unknown? How do you work with an object of an unknown size? How
do you access other members of such object? All of those offsets have to
be decided at the run-time. Can you imagine the overhead it will cause?
Jul 22 '05 #5
<pv**********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
The number of elemets of the array, the array bound must be constant
expression?Why is this restriction?


Because the size of an array is part of its type and C++ types are fixed
during compilation.
Jul 22 '05 #6
But why there is no such restriction while allocating in heap?

Andrew Koenig wrote:
<pv**********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
The number of elemets of the array, the array bound must be constant expression?Why is this restriction?
Because the size of an array is part of its type and C++ types are

fixed during compilation.


Jul 22 '05 #7
But why there is no such restriction while allocating in heap?

Andrew Koenig wrote:
<pv**********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
The number of elemets of the array, the array bound must be constant expression?Why is this restriction?
Because the size of an array is part of its type and C++ types are

fixed during compilation.


Jul 22 '05 #8
* pv**********@gmail.com:
[top-posting]
Don't top-post. See the FAQ. Corrected (also corrected the quoting).

* pv**********@gmail.com: * Andrew Koenig:
* pv**********@gmail.com:
The number of elemets of the array, the array bound must be
constant expression?Why is this restriction?


Because the size of an array is part of its type and C++ types are
fixed during compilation.


But why there is no such restriction while allocating in heap?


A C++ is a low level structure that directly is a contigous area
of memory -- in contrast to an array in e.g. Java or C#, which
is a fixed size pointer that points to some data structure which
might or might not be a contigous area of memory (doesn't matter).

In short, in C++ there's minimal abstraction between your array and
the raw memory, nothing that intercepts your stated operations.

Now consider the two basic ways an array can be allocated:

* As part of another object.

* Not as part of another object, i.e. free-standing.

When created as part of another object that object's memory layout
is fixed at compilation. More precisely, the offsets from the start
of the object to the start of each member are fixed at compilation.
So unless the array is the very last member it must be of fixed
length -- a variable length would change the offsets of the following
members at run-time, and so play havoc with the code that assumes fixed
offsets (again this is due to no intervening abstraction layer).

Summing up: part of another object -> fixed length, unless last member
of the object (this last possibility for variable length is offered as
a language extension by some compilers, but isn't supported by the std.).

Then consider the three ways an array can be allocated when it's not
defined by you as being part of another object:

* Statically (either outside any function or using the word
'static' inside a function).

* On the stack (local variable).

* On the heap.

If it's statically allocated then, although you haven't specified that
it's part of another object, at the level of memory layout it is: it's
squeezed in between the other static objects, which can't have their
offset from the start of static storage changed at run-time -- the
very same situation as for object members.

Static allocation -> fixed length, unless it's globally the last static
object (which there's no way to ensure), so the standard simply
requires fixed length.

With allocation on the stack there is technically the possibility of
dynamic length, because the stack is a dynamic data structure, and that
is supported in C99, and offered as a C++ language extension by some
compilers. Also, some compilers offer a run-time library function that
lets you allocate dynamically on the stack (of course in last-in-first-out
fashion). But standard C++ doesn't support either feature, at least not yet.

I'm not sure why that was decided, but original C was a comparably simple
language, and did not offer features that did not have very direct benefit.

Finally, with allocation on the heap there's no allocation-related problem,
because heap allocation at bottom already is a function that must be ready to
deliver any number of bytes of contigous storage, with n specified at runtime.
The only problem is that the number of array elements must be dynamically
recorded somewhere, in order to support destructor calls for the elements.
In C++ that's supported because dynamic heap allocation of arrays was
supported in C (that didn't have the destructor call issue), it would be
silly if C++ was more limited than C in that regard, and most important it
would have made the language nearly unusable -- it's that critical.

Hth.,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #9
<pv**********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
But why there is no such restriction while allocating in heap?


Because what gets returned is a pointer to the initial element of the array,
not the array itself, so the size does not participate in the type.
Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: HappyHippy | last post by:
Hi, I'm wondering what you think about this piece of code: #include<iostream> int main() { int size; std::cin >> size;
3
by: Simon | last post by:
Hello everybody, I've got a problem. I'm trying to allocate an array inside a function with Parameter int n: main(){ int i; .... i = strlen(ch); array(i); ....
16
by: herbertF | last post by:
Hi guys, In a program (not my own) I encountered the declaration of a constant pointer to an array consisting of two other const pointers to arrays. Not quite sure why they do it so complicated,...
22
by: Vijay | last post by:
With the option strict On set..... Dim fs As FileStream = File.OpenRead(strFile) With fs Dim buffer(fs.Length) As Byte ' <--- Option Strict On disallows implicit conversions from 'Long' to...
4
by: jayharris | last post by:
I'm having a ton of trouble initializing a multi-dimensional array inside a constructor, largely because I don't know the size of the array until runtime. I have a class that looks like this: ...
8
by: redefined.horizons | last post by:
I would like to have an array declaration where the size of the array is dependent on a variable. Something like this: /* Store the desired size of the array in a variable named "array_size". */...
13
by: hn.ft.pris | last post by:
Hi: I have the following simple program: #include<iostream> using namespace std; int main(int argc, char* argv){ const double L = 1.234; const int T = static_cast<const int>(L); int arr;
9
by: barcaroller | last post by:
Can variables be used for array size in C++? I know that in the past, I could not do the following: foo (int x) { type arr; } I have recently seen code that does exactly that. Is it right?
24
by: arnuld | last post by:
I want to create a new array of the same size of an array already available: #include <stdio.h> #include <string.h> int main(void) { char arrc = "URI";
8
by: s4tanu | last post by:
Hi, char arr_one = {'a','b','c','d','\0'}; i want to make another array with same size as 'arr_one' if i do like this - size_t len= strlen(arr); char arr_two;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.