what is wrong with this code: declaring a char array using variable

F

farseer

i am getting "error C2057: expected constant expression" with the
following code:

ifstream f( argv[ 1 ] );
f.seekg( 0, ios::end );
const long fSize = f.tellg();
f.close();

char content[ fSize ];
 
M

Mark P

i am getting "error C2057: expected constant expression" with the
following code:

ifstream f( argv[ 1 ] );
f.seekg( 0, ios::end );
const long fSize = f.tellg();
f.close();

char content[ fSize ];

The array size (fSize) must be a compile time constant. If not you need
an alternate approach such as dynamic allocation (via new[]) or a vector.

Mark
 
L

Luke Meyers

Mark said:
i am getting "error C2057: expected constant expression" with the
following code:

ifstream f( argv[ 1 ] );
f.seekg( 0, ios::end );
const long fSize = f.tellg();
f.close();

char content[ fSize ];

The array size (fSize) must be a compile time constant. If not you need
an alternate approach such as dynamic allocation (via new[]) or a vector.

To give a little more exposition (in case you want it)...

Mark is completely correct -- the compiler distinguishes between values
which are compile-time constants, and those which are not. That means
that the value is known at compile time, so it's information available
to the compiler. Constrast this with other types of values, where the
only thing the compiler knows is the data type, not the value.

Ways to get a compile-time constant include:
* Use a numeric literal
* Use the sizeof operator
* Combine other compile-time constants, using operators (e.g.
sizeof(int) << 8). This includes arithmetic, boolean, shifting,
ternary, etc.
* Use an enum value
* Use a boolean literal

These are some of the things which you can do with compile-time
constants, but not with other kinds of values:
* Specify the size of a stack-allocated array (this is what you're
trying to do above)
* Instantiate a template with a non-type parameter (e.g. template <int
i> class pow_)
* Combine them to form other compile-time constants
* Perform static assertions (conditions checked at compile time)

Now, one more thing you may be wondering -- why can't you do what you
tried to do? There are various levels of complexity with which one
could answer this question -- I'll do my best to strike a balance.
Arrays which are not created using dynamic memory allocation (new,
malloc, etc.) are allocated on the stack. When the compiler creates
the assembly code associated with a function or other scope, it needs
to know at compile time how much memory will be used by all the local
variables. The amount of memory depends on the size of the array,
obviously, so you can only use a size that is known at the time when
the compiler needs to make this decision -- that is, a compile-time
constant. Make more sense now?

Luke
 
F

farseer

thank you both. Luke, thanks much for the detailed explanation...you
made it easy to understand.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top