By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,778 Members | 2,015 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,778 IT Pros & Developers. It's quick & easy.

#define vs const declaration

P: n/a
Suppose I have #included <stdint.h>

#define MAX_LINE_SIZE SIZE_MAX

or

const size_t maxLineLength = SIZE_MAX ;

Between the above #define and const declaration, which should be
preferred and why ?

Mar 10 '07 #1
Share this Question
Share on Google+
20 Replies


P: n/a

su**************@yahoo.com, India wrote:
Suppose I have #included <stdint.h>

#define MAX_LINE_SIZE SIZE_MAX

or

const size_t maxLineLength = SIZE_MAX ;

Between the above #define and const declaration, which should be
preferred and why ?
There's no universal answer. Both have some advantages and
disadvantages. Depending on your exact usage of the constant, one of
the methods can be chosen.

The preprocessor based method offers more flexibility at the cost of
loss of type safety and scope. Preprocessor symbols are file scope and
thus can be inadvertently misused. However their big advantage is that
they're available at compile time and hence can be used for things
like macros and conditional compilation. const qualified objects only
exist at runtime. However their use can be better checked by the
compiler.

Mar 10 '07 #2

P: n/a
On Mar 10, 8:54 am, "santosh" <santosh....@gmail.comwrote:
subramanian10...@yahoo.com, India wrote:
Suppose I have #included <stdint.h>
#define MAX_LINE_SIZE SIZE_MAX
or
const size_t maxLineLength = SIZE_MAX ;
Between the above #define and const declaration, which should be
preferred and why ?

There's no universal answer. Both have some advantages and
disadvantages. Depending on your exact usage of the constant, one of
the methods can be chosen.

The preprocessor based method offers more flexibility at the cost of
loss of type safety and scope. Preprocessor symbols are file scope and
thus can be inadvertently misused. However their big advantage is that
they're available at compile time and hence can be used for things
like macros and conditional compilation. const qualified objects only
exist at runtime. However their use can be better checked by the
compiler.
I understand your point.

If a const variable is used, it will be visible to the debugger. Can
this
be treated as an advantage of a const decalration when both are
allowed
for a particular situation.

Mar 10 '07 #3

P: n/a
su**************@yahoo.com, India wrote:
On Mar 10, 8:54 am, "santosh" <santosh....@gmail.comwrote:
subramanian10...@yahoo.com, India wrote:
Suppose I have #included <stdint.h>
#define MAX_LINE_SIZE SIZE_MAX
or
const size_t maxLineLength = SIZE_MAX ;
Between the above #define and const declaration, which should be
preferred and why ?
There's no universal answer. Both have some advantages and
disadvantages. Depending on your exact usage of the constant, one of
the methods can be chosen.

The preprocessor based method offers more flexibility at the cost of
loss of type safety and scope. Preprocessor symbols are file scope and
thus can be inadvertently misused. However their big advantage is that
they're available at compile time and hence can be used for things
like macros and conditional compilation. const qualified objects only
exist at runtime. However their use can be better checked by the
compiler.

I understand your point.

If a const variable is used, it will be visible to the debugger. Can this
be treated as an advantage of a const decalration when both are
allowed for a particular situation.
Yes.

One more thing. A const object is prone to inadvertent modification,
which leads to undefined behaviour. A preprocessor constant cannot be
modified, since it's not an lvalue.

Mar 10 '07 #4

P: n/a

santosh wrote:
su**************@yahoo.com, India wrote:
On Mar 10, 8:54 am, "santosh" <santosh....@gmail.comwrote:
subramanian10...@yahoo.com, India wrote:
Suppose I have #included <stdint.h>
>
#define MAX_LINE_SIZE SIZE_MAX
>
or
>
const size_t maxLineLength = SIZE_MAX ;
>
Between the above #define and const declaration, which should be
preferred and why ?
>
There's no universal answer. Both have some advantages and
disadvantages. Depending on your exact usage of the constant, one of
the methods can be chosen.
>
The preprocessor based method offers more flexibility at the cost of
loss of type safety and scope. Preprocessor symbols are file scope and
thus can be inadvertently misused. However their big advantage is that
they're available at compile time and hence can be used for things
like macros and conditional compilation. const qualified objects only
exist at runtime. However their use can be better checked by the
compiler.
I understand your point.

If a const variable is used, it will be visible to the debugger. Can this
be treated as an advantage of a const decalration when both are
allowed for a particular situation.

Yes.

One more thing. A const object is prone to inadvertent modification,
[ ... ]

The word 'prone' probably conveys it's meaning in a stronger form than
I intended.

Mar 10 '07 #5

P: n/a
On Mar 10, 9:12 am, "santosh" <santosh....@gmail.comwrote:
santosh wrote:
One more thing. A const object is prone to inadvertent modification,
I am unable to understand this. If we try to modify a const object
inadvertantly, won't the compiler give diagnostic ?

Mar 10 '07 #6

P: n/a
su**************@yahoo.com, India wrote:
On Mar 10, 9:12 am, "santosh" <santosh....@gmail.comwrote:
santosh wrote:
One more thing. A const object is prone to inadvertent modification,

I am unable to understand this. If we try to modify a const object
inadvertantly, won't the compiler give diagnostic ?
Obviously the compiler will emit a diagnostic, but it's still
possible. The other case is not possible.

Mar 10 '07 #7

P: n/a
"su**************@yahoo.com, India" wrote:
"santosh" <santosh....@gmail.comwrote:
>One more thing. A const object is prone to inadvertent modification,

I am unable to understand this. If we try to modify a const object
inadvertantly, won't the compiler give diagnostic ?
It can be accidentally modified, for example by a buffer overrun or
a wild pointer.

You can get the advantage of debugger visibility by name and a
#define by using:

enum {MAXWHATEVER = 12345};

after which you can use MAXWHATEVER just as if it had been
#defined. The value needs to be an integer.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
--
Posted via a free Usenet account from http://www.teranews.com

Mar 10 '07 #8

P: n/a
On Mar 10, 12:51 pm, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.comwrote:
Suppose I have #included <stdint.h>

#define MAX_LINE_SIZE SIZE_MAX

or

const size_t maxLineLength = SIZE_MAX ;

Between the above #define and const declaration, which should be
preferred and why ?
I usually use

enum { kMaxLineLength = SIZE_MAX };

Mar 10 '07 #9

P: n/a

<su**************@yahoo.comwrote in message
news:11*********************@q40g2000cwq.googlegro ups.com...
On Mar 10, 9:12 am, "santosh" <santosh....@gmail.comwrote:
>santosh wrote:
One more thing. A const object is prone to inadvertent modification,

I am unable to understand this. If we try to modify a const object
inadvertantly, won't the compiler give diagnostic ?
Not always

For instance, this compiles
char *s = "a";
s[0] = 'b';

while s is actually const.

But also the "constness" of an object can get lost somewhere in the jungle
of function calls because somebody had to cast it away to be able to use a
function that expects a non const. This cant happen when a macro is used
because the following is not possible.

#define VAL 2

VAL = 3;

I think thats what santosh meant
Mar 10 '07 #10

P: n/a
"Servé Laurijssen" <se*@n.tkwrites:
For instance, this compiles
char *s = "a";
s[0] = 'b';

while s is actually const.
I'm sure that experienced C programmers will all know what you
mean here, but I think it's worth noting a couple of things for
the novices. First, s is modifiable here; it is the string
literal that s points into that may not be modified. Second, the
characters in the string literal are not actually
const-qualified; rather, they are just non-modifiable.
--
Ben Pfaff
bl*@cs.stanford.edu
http://benpfaff.org
Mar 10 '07 #11

P: n/a
Servé Laurijssen wrote:
<su**************@yahoo.comwrote in message
news:11*********************@q40g2000cwq.googlegro ups.com...ation,
>>>
I am unable to understand this. If we try to modify a const object
inadvertantly, won't the compiler give diagnostic ?

Not always

For instance, this compiles
char *s = "a";
s[0] = 'b';

while s is actually const.

But also the "constness" of an object can get lost somewhere in the jungle
of function calls because somebody had to cast it away to be able to use a
function that expects a non const. This cant happen when a macro is used
because the following is not possible.

#define VAL 2

VAL = 3;
But in the case you cite, the fact that a parameter started life as a
macro will be lost by the time it becomes a function parameter. The
only case that comes to mind is where a parameter is passed by address
and you can't take the address of a macro constant.

--
Ian Collins.
Mar 10 '07 #12

P: n/a
On Mar 10, 12:13 pm, "Servé Laurijssen" <s...@n.tkwrote:
<subramanian10...@yahoo.comwrote in message
On Mar 10, 9:12 am, "santosh" <santosh....@gmail.comwrote:
santosh wrote:
One more thing. A const object is prone to inadvertent modification,
I am unable to understand this. If we try to modify a const object
inadvertantly, won't the compiler give diagnostic ?

Not always

For instance, this compiles
char *s = "a";
s[0] = 'b';

while s is actually const.
s is not const. s is currently pointing at something which is not
legally modifiable. It is perfectly legal to point s at something
else and modify stuff through the s pointer.

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

Mar 10 '07 #13

P: n/a
On Mar 10, 11:29 pm, "christian.bau"
<christian....@cbau.wanadoo.co.ukwrote:
On Mar 10, 12:51 pm, "subramanian10...@yahoo.com, India"

<subramanian10...@yahoo.comwrote:
Suppose I have #included <stdint.h>
#define MAX_LINE_SIZE SIZE_MAX
or
const size_t maxLineLength = SIZE_MAX ;
Between the above #define and const declaration, which should be
preferred and why ?

I usually use

enum { kMaxLineLength = SIZE_MAX };
SIZE_MAX is the maximum value of type size_t

Mar 11 '07 #14

P: n/a

christian.bau wrote:
On Mar 10, 12:51 pm, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.comwrote:
Suppose I have #included <stdint.h>

#define MAX_LINE_SIZE SIZE_MAX

or

const size_t maxLineLength = SIZE_MAX ;

Between the above #define and const declaration, which should be
preferred and why ?

I usually use

enum { kMaxLineLength = SIZE_MAX };
Enumeration constants are of type int. SIZE_MAX is of type size_t.

Mar 11 '07 #15

P: n/a
CBFalconer <cb********@yahoo.comwrites:
"su**************@yahoo.com, India" wrote:
>"santosh" <santosh....@gmail.comwrote:
>>One more thing. A const object is prone to inadvertent modification,

I am unable to understand this. If we try to modify a const object
inadvertantly, won't the compiler give diagnostic ?

It can be accidentally modified, for example by a buffer overrun or
a wild pointer.

You can get the advantage of debugger visibility by name and a
#define by using:

enum {MAXWHATEVER = 12345};

after which you can use MAXWHATEVER just as if it had been
#defined. The value needs to be an integer.
And, in fact, it needs to be within the range of type int.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 14 '07 #16

P: n/a
"su**************@yahoo.com, India" <su**************@yahoo.comwrites:
Suppose I have #included <stdint.h>

#define MAX_LINE_SIZE SIZE_MAX

or

const size_t maxLineLength = SIZE_MAX ;

Between the above #define and const declaration, which should be
preferred and why ?
SIZE_MAX is an awfully big line length.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 14 '07 #17

P: n/a
CBFalconer <cb********@yahoo.comwrites:
You can get the advantage of debugger visibility by name and a
#define by using:

enum {MAXWHATEVER = 12345};

after which you can use MAXWHATEVER just as if it had been
#defined. The value needs to be an integer.
You can often get better debugger visibility by giving the
enumeration type a name and then declaring objects that take the
enumeration's value to have that type. Then the debugger is more
likely able to tell you the object's value by name, rather than
by value.
--
"Welcome to the wonderful world of undefined behavior, where the demons
are nasal and the DeathStation users are nervous." --Daniel Fox
Mar 14 '07 #18

P: n/a
"su**************@yahoo.com, India" <su**************@yahoo.com>
writes:
Suppose I have #included <stdint.h>

#define MAX_LINE_SIZE SIZE_MAX

or

const size_t maxLineLength = SIZE_MAX ;

Between the above #define and const declaration, which should be
preferred and why ?
I'd probably prefer the former, because it can be used as a
compile-time constant. (However, that's most useful when you can
declare an array with that many elements; declaring an array to
have SIZE_MAX elements is unwise.)
--
"To get the best out of this book, I strongly recommend that you read it."
--Richard Heathfield
Mar 14 '07 #19

P: n/a
On Mar 10, 2:54 pm, "santosh" <santosh....@gmail.comwrote:
subramanian10...@yahoo.com, India wrote:
On Mar 10, 9:12 am, "santosh" <santosh....@gmail.comwrote:
santosh wrote:
One more thing. A const object is prone to inadvertent modification,
I am unable to understand this. If we try to modify a const object
inadvertantly, won't the compiler give diagnostic ?

Obviously the compiler will emit a diagnostic, but it's still
possible. The other case is not possible.

#include <stdio.h>
#define MAX 243

int
main(void)
{
printf("MAX = %d\n", MAX);
#undef MAX
#define MAX 18
printf("MAX = %d\n", MAX);

return 0;
}

Mar 14 '07 #20

P: n/a
Bill Pursell wrote:
On Mar 10, 2:54 pm, "santosh" <santosh....@gmail.comwrote:
subramanian10...@yahoo.com, India wrote:
On Mar 10, 9:12 am, "santosh" <santosh....@gmail.comwrote:
santosh wrote:
One more thing. A const object is prone to inadvertent modification,
I am unable to understand this. If we try to modify a const object
inadvertantly, won't the compiler give diagnostic ?
Obviously the compiler will emit a diagnostic, but it's still
possible. The other case is not possible.

#include <stdio.h>
#define MAX 243

int
main(void)
{
printf("MAX = %d\n", MAX);
#undef MAX
#define MAX 18
printf("MAX = %d\n", MAX);

return 0;
}
MAX is not an object. It resolves to a value. It cannot be modified
within a single expression.

Mar 14 '07 #21

This discussion thread is closed

Replies have been disabled for this discussion.