PEPRBV%CFAAMP.BITNET@husc6.harvard.EDU (Bob Babcock) (01/15/88)
If I want to initialize a global variable in an include file, and have the initialization statement appear only in the main program, I can use something like #ifdef MAIN_PROGRAM #define INITIAL_VALUE(x) =x #else #define INITIAL_VALUE(x) #endif int i INITIAL_VALUE(1); But this doesn't work for an array, and I can't figure out anything which does. Is there some trick I'm missing?
gwyn@brl-smoke.ARPA (Doug Gwyn ) (01/15/88)
In article <11276@brl-adm.ARPA> PEPRBV%CFAAMP.BITNET@husc6.harvard.EDU (Bob Babcock) writes: >If I want to initialize a global variable in an include file, ... >But this doesn't work for an array ... >Is there some trick I'm missing? Yes, the trick of not trying to be so bloody clever. Supply static initializers in some place that makes sense, for example in the source code for the module that owns the data.
ljz@fxgrp.UUCP (Lloyd Zusman, Master Byte Software) (01/16/88)
In article <11276@brl-adm.ARPA> PEPRBV%CFAAMP.BITNET@husc6.harvard.EDU (Bob Babcock) writes: >If I want to initialize a global variable in an include file, and have >the initialization statement appear only in the main program, I can use >something like > > #ifdef MAIN_PROGRAM > #define INITIAL_VALUE(x) =x > #else > #define INITIAL_VALUE(x) > #endif > int i INITIAL_VALUE(1); > >But this doesn't work for an array, and I can't figure out anything >which does. Is there some trick I'm missing? This appeared on the net a couple years ago, I believe. Do the following: #define _ , /* i.e., define underscore to be comma */ #ifdef MAIN_PROGRAM #define INITIAL_VALUE(x) = {x} /* note the braces */ #else #define INITIAL_VALUE(x) #endif Then, int i INITIAL_VALUE(1); int j[8] INITIAL_VALUE(1 _ 2 _ 3 _ 4 _ 5 _ 6 _ 7 _ 8); Many purists will not like this, but if you want to do it, it should work. --- Lloyd Zusman Master Byte Software Los Gatos, California Internet: fxgrp!ljz@ames.arpa "We take things well in hand." UUCP: ...!ames!fxgrp!ljz
be@dde.uucp (Bjorn Engsig) (01/18/88)
In article <11276@brl-adm.ARPA>, PEPRBV%CFAAMP.BITNET@husc6.harvard.EDU (Bob Babcock) writes: > If I want to initialize a global variable in an include file, and have ... > But this doesn't work for an array, and I can't figure out anything > which does. Is there some trick I'm missing? I'v always used something like the following in a header file: #ifdef MAIN_PROGRAM # define EXTERN # define INITVAL(x) {x} #else # define EXTERN extern # define INITVAL(x) #endif EXTERN int variable INITVAL(27); EXTERN int array[] #ifdef MAIN_PROGRAM {1, 2, 3, 4, 5, } #endif ; BTW, gwyn@brl-smoke.ARPA (Doug Gwyn @ Ballistic Research Lab (BRL), APG, MD.) writes: > Supply static initializers in some place that makes sense, > for example in the source code for the module that owns the data. This is good for statics but NOT for externals. You really want to write the external variables ONE and only ONE place, and the header file above is very appropriate. The scheme has at least proven to be useful in the 3000 (three thousand, maybe more) source files large software product I'm working on. -- Bjorn Engsig, E-mail: ..!uunet!mcvax!diku!dde!be or be@dde.uucp -- Hofstadters Law: It always take longer than you expect, even if you take into account Hofstadters Law.
daveb@laidbak.UUCP (Dave Burton) (01/19/88)
In article <11276@brl-adm.ARPA> PEPRBV%CFAAMP.BITNET@husc6.harvard.EDU (Bob Babcock) writes: >If I want to initialize a global variable in an include file, and have >the initialization statement appear only in the main program, I can use >something like > > #ifdef MAIN_PROGRAM > #define INITIAL_VALUE(x) =x > #else > #define INITIAL_VALUE(x) > #endif > int i INITIAL_VALUE(1); > >But this doesn't work for an array, and I can't figure out anything >which does. Is there some trick I'm missing? I would consider this an abuse of conditional compilation. Conditionals are best used to control implementation of some implementation dependent code, not which module you're compiling. If the data is truly global (used throughout most of the modules in your code), consider using a file dedicated to initializing the globals. I have used a file named "global.c" for purposes such as this for years, with no ill effects. A separate data initialization file makes it easy to hunt down bad initializations. Do not place all globals in such a file, however. In the majority of cases, such variables are global to only one file. In this case they should be initialized at the top of that file, and declared static. -- --------------------"Well, it looked good when I wrote it"--------------------- Verbal: Dave Burton Net: ...!ihnp4!laidbak!daveb V-MAIL: (312) 505-9100 x325 USSnail: 1901 N. Naper Blvd. #include <disclaimer.h> Naperville, IL 60540
gwyn@brl-smoke.ARPA (Doug Gwyn ) (01/19/88)
In article <303@Aragorn.dde.uucp> be@dde.uucp (Bjorn Engsig) writes: >BTW, gwyn@brl-smoke.ARPA (Doug Gwyn @ Ballistic Research Lab (BRL), APG, MD.) writes: >> Supply static initializers in some place that makes sense, >> for example in the source code for the module that owns the data. >This is good for statics but NOT for externals. It's not even an issue for statics! I disagree completely with the approach that external data should be thought of as just one big amorphous pool of generally-accessible information. It is not what one would obtain by following structured design methodologies.
pablo@polygen.uucp (Pablo Halpern) (01/20/88)
In article <11276@brl-adm.ARPA> PEPRBV%CFAAMP.BITNET@husc6.harvard.EDU (Bob Babcock) writes:
- If I want to initialize a global variable in an include file, and have
- the initialization statement appear only in the main program, I can use
- something like
-
- #ifdef MAIN_PROGRAM
- #define INITIAL_VALUE(x) =x
- #else
- #define INITIAL_VALUE(x)
- #endif
- int i INITIAL_VALUE(1);
-
- But this doesn't work for an array, and I can't figure out anything
- which does. Is there some trick I'm missing?
Using your existing macros for INITIAL_VALUE(), you should be able
to use the following declaration:
int i[] INITIAL_VALUE( { 1, 2, 3, 4 } );
Other than a bit of uglyness, do you see anything wrong with that?
lew@gsg.UUCP (Paul Lew) (01/21/88)
> From: be@dde.uucp (Bjorn Engsig) > Organization: Dansk Data Elektronik A/S, Herlev, Denmark > > This is good for statics but NOT for externals. You really want to > write the external variables ONE and only ONE place, and the header > file above is very appropriate. The scheme has at least proven to > be useful in the 3000 (three thousand, maybe more) source files large > software product I'm working on. There are side effects: if you add one variable to this file which is included by 3000 files, 'make' might rebuild everything when there are only 2 files that needs recompilation. This happened to me before and I dont like it. (However, I might use your approach if I have a CRAY..) Keep variables where it belongs and use tools like 'gid' will be a good compromise before you have a good CASE environment. You might also need some help when creating programs, e.g., bind some key in emacs to automatically insert external definition for the word before the cursor to the top of the file, etc. -- ---------------------------------------------------------------------- Paul Lew {olivea,harvard,decvax}!gsg!lew (UUCP) General Systems Group, 5 Manor Parkway, Salem, NH 03079 (603) 893-1000