C preprocessor

F

francesco

I'm programming an embedded system, on a Fujitsu microcontroller.
Stated that I'm not sure about constants optimization, I'm trying to
declare constants in two ways, one for debug version and one for
release version.

In debug, I would like to use const declaration, as I found it more
safer:
for example: const uint8_t SPEED_FAST = 10;

In release, I would like to use #define, as the constant value is
substituted inside the code, thus optimizing RAM usage:
for example: #define SPEED_FAST 10

I've written this macro:

#ifdef NDEBUG
#define constant(type, name, value) #define name value
#else
#define constant(type, name, value) const type name = value;
#endif /* NDEBUG */

Actually, this code doesn't work as in the NDEBUG version I read this
error:
Misplaced '#' or '##' in a macro definition.

How should I do?

Thanks.
 
J

jacob navia

You can't make a #define directive within a macro.
The standard says:

The resulting completely macro-replaced preprocessing token sequence is not
processed
as a preprocessing directive even if it resembles one,

(6.10.3.4.3, page 155)
 
E

Emmanuel Delahaye

In said:
I'm programming an embedded system, on a Fujitsu microcontroller.
Stated that I'm not sure about constants optimization, I'm trying to
declare constants in two ways, one for debug version and one for
release version.

In debug, I would like to use const declaration, as I found it more
safer:
for example: const uint8_t SPEED_FAST = 10;

In release, I would like to use #define, as the constant value is
substituted inside the code, thus optimizing RAM usage:
for example: #define SPEED_FAST 10

I've written this macro:

#ifdef NDEBUG
#define constant(type, name, value) #define name value
#else
#define constant(type, name, value) const type name = value;
#endif /* NDEBUG */

Actually, this code doesn't work as in the NDEBUG version I read this
error:
Misplaced '#' or '##' in a macro definition.

Yes. You can't use a preprocessor directive in a macro.
How should I do?

You could use enum() instead of #define

#ifdef NDEBUG
#define constant(type, name, value) enum {name = value}
#else
#define constant(type, name, value) const type name = value
#endif /* NDEBUG */

(Note : trailing ';' removed. They belong to the coding.)
 
F

francesco

You could use enum() instead of #define
#ifdef NDEBUG
#define constant(type, name, value) enum {name = value}
#else
#define constant(type, name, value) const type name = value
#endif /* NDEBUG */

(Note : trailing ';' removed. They belong to the coding.)

OK, but the problem is the same: enums are variables, so occupy memory
space, while using define I could substitute them inside the code and
occupy no space. In this sense, it is better to use constants, as they
occupy just an unsigned char space (generally one byte), while enums
are int and generally occupy 4 byte on a 32 bit machine.

I begin to think that my problem is without solution.

Thanks anyway.
 
C

Chris Dollin

francesco said:
OK, but the problem is the same: enums are variables,

They're not.
so occupy memory space,

They need not (and typically won't).
while using define I could substitute them inside the code and
occupy no space. In this sense, it is better to use constants, as they
occupy just an unsigned char space (generally one byte), while enums
are int and generally occupy 4 byte on a 32 bit machine.

Objects of an enum type might indeed be four bytes wide. But that
doesn't mean that every enum constant occupies four bytes of
variable (or constant) space, or that it needs four bytes of
code to load.

OT though it may be, what does your compiler do with

enum Spoo { zero, one, seventeen = 17, fortyTwo = 42 };

int example( int a, int b, int c, int d )
{
int A = a + zero, B = b + one;
int C = c + seventeen, d = d + fortyTwo;
return A + B + C + D;
}

??
 
E

Emmanuel Delahaye

In said:
OK, but the problem is the same: enums are variables, so occupy memory

No. enums are constants. Don't mix a constant literal defined by an enum and
a variable of type enum.
space, while using define I could substitute them inside the code and
occupy no space. In this sense, it is better to use constants, as they
occupy just an unsigned char space (generally one byte), while enums
are int and generally occupy 4 byte on a 32 bit machine.

I begin to think that my problem is without solution.

Don't surrender so easily. I have given a solution to you. They are others.
 
F

francesco

Don't surrender so easily. I have given a solution to you. They are others.

Sorry to have given the feeling that I was surrending. I'm searching
again.

Anyway, I would like to thank you for your valuable help. Surely this
is a solution and I'm sorry if I made you believe not to be gratefull.

Thanks guy!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top