#define vs const declaration

Discussion in 'C Programming' started by subramanian100in@yahoo.com, India, Mar 10, 2007.

  1. , India

    , India Guest

    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 ?
     
    , India, Mar 10, 2007
    #1
    1. Advertising

  2. , India

    santosh Guest

    , 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.
     
    santosh, Mar 10, 2007
    #2
    1. Advertising

  3. , India

    , India Guest

    On Mar 10, 8:54 am, "santosh" <> wrote:
    > , 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.
     
    , India, Mar 10, 2007
    #3
  4. , India

    santosh Guest

    , India wrote:
    > On Mar 10, 8:54 am, "santosh" <> wrote:
    > > , 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.
     
    santosh, Mar 10, 2007
    #4
  5. , India

    santosh Guest

    santosh wrote:
    > , India wrote:
    > > On Mar 10, 8:54 am, "santosh" <> wrote:
    > > > , 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.
     
    santosh, Mar 10, 2007
    #5
  6. , India

    , India Guest

    On Mar 10, 9:12 am, "santosh" <> wrote:
    > 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 ?
     
    , India, Mar 10, 2007
    #6
  7. , India

    santosh Guest

    , India wrote:
    > On Mar 10, 9:12 am, "santosh" <> wrote:
    > > 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.
     
    santosh, Mar 10, 2007
    #7
  8. , India

    CBFalconer Guest

    ", India" wrote:
    > "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 ?


    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
     
    CBFalconer, Mar 10, 2007
    #8
  9. On Mar 10, 12:51 pm, ", 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 ?


    I usually use

    enum { kMaxLineLength = SIZE_MAX };
     
    christian.bau, Mar 10, 2007
    #9
  10. <> wrote in message
    news:...
    > On Mar 10, 9:12 am, "santosh" <> wrote:
    >> 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
     
    Servé Laurijssen, Mar 10, 2007
    #10
  11. , India

    Ben Pfaff Guest

    "Servé Laurijssen" <> writes:

    > 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

    http://benpfaff.org
     
    Ben Pfaff, Mar 10, 2007
    #11
  12. , India

    Ian Collins Guest

    Servé Laurijssen wrote:
    > <> wrote in message
    > news:...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.
     
    Ian Collins, Mar 10, 2007
    #12
  13. , India

    Guest

    On Mar 10, 12:13 pm, "Servé Laurijssen" <> wrote:
    > <> wrote in message
    > > On Mar 10, 9:12 am, "santosh" <> wrote:
    > >> 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, 2007
    #13
  14. , India

    , India Guest

    On Mar 10, 11:29 pm, "christian.bau"
    <> wrote:
    > On Mar 10, 12:51 pm, ", 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 ?

    >
    > I usually use
    >
    > enum { kMaxLineLength = SIZE_MAX };


    SIZE_MAX is the maximum value of type size_t
     
    , India, Mar 11, 2007
    #14
  15. , India

    santosh Guest

    christian.bau wrote:
    > On Mar 10, 12:51 pm, ", 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 ?

    >
    > I usually use
    >
    > enum { kMaxLineLength = SIZE_MAX };


    Enumeration constants are of type int. SIZE_MAX is of type size_t.
     
    santosh, Mar 11, 2007
    #15
  16. CBFalconer <> writes:
    > ", India" wrote:
    >> "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 ?

    >
    > 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) <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"
     
    Keith Thompson, Mar 14, 2007
    #16
  17. ", India" <> 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 ?


    SIZE_MAX is an awfully big line length.

    --
    Keith Thompson (The_Other_Keith) <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"
     
    Keith Thompson, Mar 14, 2007
    #17
  18. , India

    Ben Pfaff Guest

    CBFalconer <> writes:

    > 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
     
    Ben Pfaff, Mar 14, 2007
    #18
  19. , India

    Ben Pfaff Guest

    ", India" <>
    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
     
    Ben Pfaff, Mar 14, 2007
    #19
  20. , India

    Bill Pursell Guest

    On Mar 10, 2:54 pm, "santosh" <> wrote:
    > , India wrote:
    > > On Mar 10, 9:12 am, "santosh" <> wrote:
    > > > 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;
    }
     
    Bill Pursell, Mar 14, 2007
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alexander Farber
    Replies:
    0
    Views:
    464
    Alexander Farber
    Jun 21, 2005
  2. Replies:
    4
    Views:
    430
    Alf P. Steinbach
    May 5, 2006
  3. ThazKool
    Replies:
    1
    Views:
    461
  4. Replies:
    11
    Views:
    1,151
  5. Javier
    Replies:
    2
    Views:
    621
    James Kanze
    Sep 4, 2007
Loading...

Share This Page