DSPRelated.com
Forums

#define vs. const

Started by Kesh November 22, 2004
Bhaskar Thiagarajan wrote:

> "Kesh" <tikuma@hotmail.com> wrote in message > news:cntf2e$2o1$1@solaris.cc.vt.edu... > >>Hi, >> >>Can anyone tell me if there is any advantage of using #define vs. const >>(or vice versa) in TI CCS (in C)? > > > I have never used CCS, but there are some good articles on the web > (Embedded.com?) on this topic. > The advantage as Tim already pointed out is the memory allocation associated > with const vs no allocation in #define(although I'm not sure I understood > his static construct and why it is equivalent to the define) > > The const declaration provides much better type checking in your code. It > also makes the variable available in your debugger (by name). If it is only > a few definitions, I think the const is a better practice (since the memory > penalty is not going to be significant). If it is, then your system has no > margin and requires a review. > > Cheers > Bhaskar > > > >>E.g. >> >> #define myConst 1/256 >> >>vs. >> >> const float myConst = 1/256; >> >>then use it like: >> >> b = a*myConst; >> >>Thanks! >>Kesh > > >
The static const ... declaration tells the compiler (if it's listening) that the variable scope is limited to that file only. The CCS compiler will then treat the declaration as a define with type checking. This feature should not be counted on in just any compiler. -- Tim Wescott Wescott Design Services http://www.wescottdesign.com
> [Re: static const] The CCS compiler will then treat the declaration as a > define with type checking.
Tim, this clarified what you were saying earlier, thx. This made me think of another thing. How about if you have an const array? For instance, constant tap delay weights to be used in a FIR operation. Does the CCS compiler treat each element as a stand-alone constant as opposed to a parameter array in a memory? The latter is what you want to do in DSP, I think. Kesh
Kesh wrote:
>> [Re: static const] The CCS compiler will then treat the declaration as a >> define with type checking. > > > Tim, this clarified what you were saying earlier, thx. > > This made me think of another thing. How about if you have an const > array? For instance, constant tap delay weights to be used in a FIR > operation. Does the CCS compiler treat each element as a stand-alone > constant as opposed to a parameter array in a memory? The latter is what > you want to do in DSP, I think. > > Kesh
Thats a good question! Normally when you make an array you take it's address, at least implicitly. Certainly if you have something like static const int bob[] = {1, 2, 3, 4}; ... for (int ralph = 0; ralph < 4; ralph++) sum += bob[ralph]; the compiler would put bob[] into memory because when you address an array element you're implicitly taking the array address. On a good embedded tool set like Code Composter it would put it in ROM and not RAM, as well. On a really, really perverted toolset it would unwind the loop... -- Tim Wescott Wescott Design Services http://www.wescottdesign.com
On Mon, 22 Nov 2004 14:37:53 -0500, Kesh <tikuma@hotmail.com> wrote:

>Hi, > >Can anyone tell me if there is any advantage of using #define vs. const >(or vice versa) in TI CCS (in C)? > >E.g. > > #define myConst 1/256 > >vs. > > const float myConst = 1/256; > >then use it like: > > b = a*myConst; > >Thanks! >Kesh
I use "const" instead of "define" only because all "stupid" debuggers will evaluate a const but all "stupid" debuggers will not evaluate a define... CCS is a "stupid" compiler and "stupid" debugger. BTW, "smart" compilers will not necessarily allocate memory for a const unless it improves execution speed.
On 2004-11-22 23:24:16 +0100, glen herrmannsfeldt <gah@ugcs.caltech.edu> said:

> I have heard stories of ex-Pascal programmers doing: > > #define BEGIN { > #define END } > > and maybe worse. > > -- glen
:-) I've been known to do something like this for a while: #define EVER (;;) .. for EVER { // some code } Contrary to the Pascal style BEGIN and END macros which are indeed silly in C, I find the "for EVER" loop more readable than the cryptic "for (;;)" -- Stephan M. Bernsee http://www.dspdimension.com
Hi Stephan,

Stephan M. Bernsee <spam@dspdimension.com> wrote in message news:> 
> for EVER > { > > // some code > > } > > Contrary to the Pascal style BEGIN and END macros which are indeed > silly in C, I find the "for EVER" loop more readable than the cryptic > "for (;;)"
Then why not: while (true) { // some other code } Less cryptic than for (;;) and more appropriate than #defines... ;-) Regards, /Rob
On 2004-11-23 14:35:06 +0100, robert.bielik@gyros.com (Robert Bielik) said:

> Then why not: > while (true)
Yeah, sure, but I particularly like the look of the "for EVER" statement. -- Stephan M. Bernsee http://www.dspdimension.com
"Stephan M. Bernsee" <spam@dspdimension.com> wrote in message
news:30gum0F2ue78vU1@uni-berlin.de...
> On 2004-11-23 14:35:06 +0100, robert.bielik@gyros.com (Robert Bielik) said: > > > Then why not: > > while (true) > > Yeah, sure, but I particularly like the look of the "for EVER" statement.
I'm partial to: while (1) // Do forever The simple comment removes all "crypticness". But Stephan's is nice too in that it is self-documenting.
"glen herrmannsfeldt" <gah@ugcs.caltech.edu> wrote in message
news:cntp0m$8hb$1@gnus01.u.washington.edu...
> > > I once worked at a company who's software department was recovering from > > a programmer who seemed to think of C as an assembler to go along with > > the C preprocessor -- she went as far as having multiple open braces in > > some macros that were matched by the same number in other macros. It > > was certainly very intriguing, but it didn't lend itself to maintenance. > > > I have heard stories of ex-Pascal programmers doing: > > #define BEGIN { > #define END }
Interesting. In school, I learned Pascal first, then C. When I found out that C let you use {} instead of BEGIN END, I thought that was the greatest thing! It saves so many keystrokes and makes the code look neater too. So I find it odd that anyone would prefer the Pascal version, but I suppose it is all in what you are used to. I had only been programming Pascal for one term, so I didn't have any deeply ingrained habits to break.
> Then why not: > > while (true) > { > // some other code > } > > Less cryptic than for (;;) and more appropriate than #defines... ;-)
Because your lint checker will complain about the constant expression in the conditional. I prefer the while(true) syntax, however. It may be slightly more efficient to use for(;;) becuase the compiler is required to check if evaluating the expression in the while returns true. It may be smart enough to optimise it out though. A