Re: good Embedded C resources plz...



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?
.



Relevant Pages

  • Re: Enum memory allocation
    ... > an instance of the enum class FavoriteSport is getting created and sent ... An Enum is a Structure, ... there is no explicit creation on the heap for this memory. ... value of this argument has its value copied to it from the calling Sub. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Enum memory allocation
    ... > if it is only stored as an integer in memory? ... You know it is an enum object by virtual you defined it as an Enum type. ... >> value of this argument has its value copied to it from the calling Sub. ... >> directly onto the stack of the called Sub. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Boolean data type?
    ... I'm writing code for an embedded systems project so I'll *really* have ... to watch how much memory I'm using... ... In that case, if I didn't have access to a C99 compiler, or a compiler ... smallest type than holds the range of the enum values. ...
    (comp.lang.c)
  • ioremap_xxx() with EXEC enabled
    ... mapped with ioremap() now. ... I think we may need a method to map memory ... Interface scheme 1: ... enum ioremap_xmode { ...
    (Linux-Kernel)
  • Re: Enum memory allocation
    ... I also did a test using enum objects and checking memory usage to confirm ... > FavoriteSport and acts accordingly. ... The Common Language Infrastructure Standard itself ...
    (microsoft.public.dotnet.languages.vb)