Subj : Re: Win98 error To : borland.public.cpp.borlandcpp From : maeder@glue.ch (Thomas Maeder [TeamB]) Date : Sat Mar 13 2004 08:16 am Bob Gonder writes: >>const int nmax=100; >> >>is not a variable. It's a const. If used like a macro can be used, it has an >>"immediate" value, like a macro. Additionally, it's address can be taken. > > Hmmm.. how can you take the address of an immediate (non-existant) > variable? If the address of a const is taken, memory will usually have to be reserved for it; unless the compiler finds some magic to avoid it in a way that a correct program can't detect. > I would suppose it'd get pushed on the stack, then the > stack address used. At which point, it _is_ a variable. I'd say: At which point, memory will probably be allocated for it. It's still not a variable. >>Short, it has *all* the uses the macro version has; then, it has more uses; >>but it hasn't any of the problems macros have. > > Can it also be used in a header file by several modules? Yes. By default (i.e. unless declared extern), consts have internal linkage. This means that each translation unit gets its own "copy" of the const. I write "copy" in quotes because in most cases, integral consts that are used like "your" macro would be will be inlined by the compiler. > (I supposed I _could_ try it and find out.....) And... I haven't run > into any "problems" with macros. The main problem is that you can very easily break code at one end of a program by #defining a macro at the other end because macros don't respect scopes. > You'd probably tisk at a lot more of my code too... I wouldn't know of a programmer that does not dislike some of the habits of each other. :-) > You were likely schooled in C. My U didn't offer C, just Fortran, > Algocol, Cobol, Basic, and several ASMs (there might have been a > Snobol, but didn't take it) The first languages I taught myself were Pascal (I'm Swiss, after all :-) ) and Basic. In school, we mainly used Pascal as well. > So, I'm not indoctrinated with the "politically correct" way to write > C code. But my objection is not political at all. My point is that if there are two ways to reach the same goal, and one of them has only advantages over the other, I'd consider using the other a bug. There aren't many silver bullets out there, but this is one. > I've learned by pattern recognition.. ie: > void dosomething( const int i ) > { char array[i]; /* wrong (isn't it?)*/ I'm not sure about C99, but it is certainly wrong in C++ and C prior to 99. > looks very much like > const int i = 100; > char array[i]; /* ok */ > It's the same words, but different meanings. I don't like to use > "magic" in compilers. Too often it bites ya. I agree that *this* is a problem. One of the leading principles in the design of the C language was to keep the number of keywords low; C++ follows this principle as well. The result is that all but one of the meanings of keywords such as static and virtual are confusing at least. .