Re: good Embedded C resources plz...
- From: Colin Paul Gloster <Colin_Paul_Gloster@xxxxxxx>
- Date: 25 Aug 2005 10:36:46 +0200
Dave said at Wed, 24 Aug 2005 19:36:01 GMT:
"On 24 Aug 2005 20:34:39 +0200, Colin Paul Gloster
<Colin_Paul_Gloster@xxxxxxx> wrote:
[..]
>enum
>{
> BIT6 = 0x40 /*or 1<<6*/,
> GPS_ARM_INTERRUPT = BIT6
>}
You need a semicolon at the end there."
Correct, sorry.
" And it only works for values
of type int. E.g.
enum
{
MAX_VOLTAGE = 16.5
};
will not compile."
Not as good type checking as const, but when enums work they can be
better. If someone accidentally used an enum for a float, this will be
caught at compile time and can be replaced with a variable or a macro
then.
"In C, the compiler _will_ reserve memory for BIT6, no question, where
a macro will not."
Correct, and unless memory is being exhausted, this is not necessarily
a problem, and for those of us who have to modify a deployed system by
radioing a patch with only a few minutes' radio visibility of the
system at a time, it can be beneficial to modify one datum and have it
work throughout the memory image, instead of patching everywhere the
value is needed.
An example I had this week of a system (which will not be patched like
this, chiefly because its only receiver is a GLONASS/GPS receiver and
not a general purpose radio) in which I did replace a const with a
macro did reduce the memory consumption (but even with most of my 4KB
of RAM being used I am not in trouble of running out of RAM, and with
far over half of my 64KB of ROM unused I am content), but the reason I
did this was because one deadline was being missed, and using the
hardcoded literal (which in my terms a macro is, though you would seem
to disagree) sped it up enough to meet that deadline.
"Also,
const int ARRAY_SIZE=10;
int array[ARRAY_SIZE];
works in C++, but not C."
And
enum {ARRAY_SIZE=10};
int array[ARRAY_SIZE];
works in ANSI/ISO C99 and not ex-ANSI/ex-ISO C89. 1989 was a long time
ago. Yikes, so was 1999 even. So why not use enum here in 2005?
.
- References:
- good Embedded C resources plz...
- From: christina
- Re: good Embedded C resources plz...
- From: Colin Paul Gloster
- Re: good Embedded C resources plz...
- From: ian_okey
- Re: good Embedded C resources plz...
- From: Colin Paul Gloster
- Re: good Embedded C resources plz...
- From: Dave Hansen
- good Embedded C resources plz...
- Prev by Date: Re: good Embedded C resources plz...
- Next by Date: Re: Q: lpc921 compatiblity with 8051?
- Previous by thread: Re: good Embedded C resources plz...
- Next by thread: Re: good Embedded C resources plz...
- Index(es):
Relevant Pages
|