The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

declare static variables in C/C++ file module

I have several .cpp files containing large integer arrays. These arrays consists of constants, and are used by some method implements. They are only meaningful for a couple of methods and it does not make much sense to declare them in global scope or class scope. I used to delcare them as static variable, and place them before the implement. So a typical CPP file look like this:

[Header files]

<static variable definitions>
static const int a_table[]={1, 22, 33...}
...

[method implement]
classA:methodA(...)

Problem is, these constant arrays span too many lines in a CPP file. I would like to place a declaration at the beginning and place the bulk definition at the end of the file. If I wrote
static const int a_table[];
the compliler gave an error that the size of a_table is unknown.

If I change teh static to extern, the compiler just issued a warning, however.
extern const int a_table[];
<implement>
static const int a_table[]= {...}

On VS2003, the warning message is:
warning C4211: nonstandard extension used : redefined extern to static

Well, is there better to get around this issue?
BurnedInCA
Saturday, October 29, 2005
 
 
I've broken then up into multiple arrays before.

A better option is to use a vector or something and load the vector up with your data through a static contructor, lazy initialization, explicit initialization.
son of parnas
Sunday, October 30, 2005
 
 
You might be able to do away with the static / extern keywords entirely, as const's have internal linkage by default -- at least in C++. I don't know if doing so will solve your compiler warning though.
dotdot
Sunday, October 30, 2005
 
 
Whatever you do, I would not ignore the compiler warning - defining as static and declaring as extern is a really ugly hack.
hoser Send private email
Sunday, October 30, 2005
 
 
Banish the array into an extra include file.
ping?
Sunday, October 30, 2005
 
 
I don't think it's an ugly hack. It doesn't even give a warning on the compilers I tried it on here.

You could make it not be a static if the warning bothers you.
Art Wilkins
Sunday, October 30, 2005
 
 
static int *get_array();

/* code */

static int *get_array()
{
  static int array[] = { ... };
  return array;
}
UNIX Guy
Sunday, October 30, 2005
 
 
UNIX Guy gives the right answer, with the possible addition of "inline" on the function definition.

But why are you declaring your static array in the header file and not the cpp file? If you put it in the header file, you will be including the static array definition in every source file that includes that header. Is that really what you want? It seems from what you've said you only want the static array available to ClassA::MethodA()?  If you want something to be available only privately to a class implementation, you should try to put it in the cpp file for the class. That is true for UNIX Guy's suggestion too.
Ian Boys Send private email
Sunday, October 30, 2005
 
 
On re-reading, maybe you are putting things in the cpp file. Your code sample is a little ambiguous at first reading.
Ian Boys Send private email
Sunday, October 30, 2005
 
 
+1 for "Banish the array into an extra include file."
Almost H. Anonymous Send private email
Monday, October 31, 2005
 
 
Write a function to access the array, then implement the function at the end.
Mr Jack
Monday, October 31, 2005
 
 
>  +1 for "Banish the array into an extra include file."

I don't think the compiler cares where it is. If it's too long then it's too long. That's at least what I have experienced.
son of parnas
Monday, October 31, 2005
 
 
I don't think the array is too long for the compiler -- I got the impression that it was visually too long for the programmer.
Almost H. Anonymous Send private email
Monday, October 31, 2005
 
 
>  I don't think the array is too long for the compile

I've run into the too long error from compilers, so that's what I thought he was talking about.
son of parnas
Monday, October 31, 2005
 
 
Yeah, the complier does not complain the array is too big. I do. And they are a bunch of such arrays (around) 30 occupying too much space at the begining of CPP file.
BurnedInCA
Monday, October 31, 2005
 
 
I'd just do one file per array -- assuming they're all pretty long -- and include them at the top.
Almost H. Anonymous Send private email
Monday, October 31, 2005
 
 
How long ARE these declarations. They must be huge if any modern compiler can't handle them.

If that's true, figure out a way to break them up. Nothing burns time worse than fighting your tools.
sometimes unprepared
Monday, October 31, 2005
 
 
We don't often get to use modern compilers for embedded systems.
son of parnas
Monday, October 31, 2005
 
 
hmm isn't "static const" meaning you get a copy of it per object file?

wouldn't "extern const" be much neater and take less RAM etc?

I'd say banish the array to a .cpp file and just have the prototype in the headers
new nick, new rep
Tuesday, November 01, 2005
 
 
lol ignore my prev post

I managed to actually read the OP and yet somehow get everything upside-down by the time I reached the bottom of the thread..
new nick, new rep
Tuesday, November 01, 2005
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz