Jump to content
  • Advertisement

Archived

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

Mathematix

extern const confusion...

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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
[Lone Wolves Game Development][RoboBlast][Articles][GD Emporium][Webdesign][E-mail]


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
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 this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!