#### Archived

This topic is now archived and is closed to further replies.

# extern const confusion...

This topic is 5267 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey all! I''m using Visual C++ .net 2002. When I declare a constant variable in one file, say ''const int aConst = 1;'', then redeclare it in another source file as ''extern const aConst;'', expecting it to be initialised to 1 already, the compiler moans if I try to set the size of the array using this extern const saying that it cannot create a zero sized array. I am forced to explicitly reinitialise aConst to 1 in the new source file. Have I misunderstood something? Thanks all. Beginners and experts will find answers here.

##### Share on other sites
arrays declared thus:

int a[x];

Have their size resolved at compile time. Therefore x can only be a literal or a preprocessor defined value (which is a literal when the compiler sees it).

Your const won''t have a value until run time, so it is of no use to the compiler.

Also, in C++ const_cast exists, so the compiler cannot necessarily trust the value of a const to be constant throughout execution.

##### Share on other sites
Just put that const in a header and include it in both .cpp.

##### Share on other sites
Hello Mathematix,

I think you problem is this.
You declared const int aConst = 1; in one file which in when this file gets compiled then aConst will be assign 1 for this file and at runtime.
But in the file you using aConst it is not initialized, since you not reading in the file which has aConst initialized, whence your error of tying to declare an array of zero length, this will happen for all global arrays that will use aConst which are not in the file that has aConst = 1.

There are a few solutions for you.
One is to put const aConst = 1; in an header file, and hope compile is smart enough only to allocate one occurrence of aConst. There might be compiler switch to make sure theres only one.

Or just redeclare extern const int aConst = 1; in the file you want to use aCont in. This is perfectly legal C++ and has to be done in you your case and if only have one occurrence of aConst is a must.

An thrid way is to put in a header file and use defines for a C++ file that actual allocate space for aConst.
example:

in a aConst.h
#ifdef _ACONST_CC_
EXTERN
#else
EXTERN extern
#endif

EXTERN const int aConst = 1;

then in the file that aConst would be defined in put
#define _ACONST_CC_
#include "aConst.h"
#undefine _ACONST_CC_

This allows for initailization and only one occurrence of aConst.
This should work in M$NET if it is ANSI compliant. Could also be done as an emun. Lord Bart #### Share this post ##### Link to post ##### Share on other sites Two easie ways: 1) #define aConst 1 since it''s a const int you are not going to change it so you might as well make it a define. 2) create your array dynamically: extern const int aConst; int *array; //or any other type. just what you nees array = new int[aConst]; //or any other type ... delete[] array; Make SURE you don''t forget to add the delete[] or you will cause memory leaks. Sander Maréchal GSACP: GameDev Society Against Crap Posting To join: Put these lines in your signature and don''t post crap! #### Share this post ##### Link to post ##### Share on other sites #### Share this post ##### Link to post ##### Share on other sites quote: Have their size resolved at compile time. Therefore x can only be a literal or a preprocessor defined value (which is a literal when the compiler sees it). According to MSDN it is possible (and it doesn''t mention M$-specific either).

quote:
MSDN
In C++, you can use the const keyword instead of the #define preprocessor directive to define constant values. Values defined with const are subject to type checking, and can be used in place of constant expressions. In C++, you can specify the size of an array with a const variable as follows:

const int maxarray = 255;
char store_char[maxarray]; // Legal in C++; illegal in C

In C, constant values default to external linkage, so they can appear only in source files. In C++, constant values default to internal linkage, which allows them to appear in header files.

Did work for me...

##### Share on other sites
quote:
Original post by Fruny
An even easier way : use a std::vector.

std::vector array( aConst );

I second that.

[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]

##### Share on other sites
Ah, nobody listens to me, oh well

##### Share on other sites
quote:
Original post by mputters
Ah, nobody listens to me, oh well

I second that.

[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]