#define vs const declaration

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • subramanian100in@yahoo.com, India

    #1

    #define vs const declaration

    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 ?

  • santosh

    #2
    Re: #define vs const declaration


    subramanian100i n@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.

    Comment

    • subramanian100in@yahoo.com, India

      #3
      Re: #define vs const declaration

      On Mar 10, 8:54 am, "santosh" <santosh....@gm ail.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.

      Comment

      • santosh

        #4
        Re: #define vs const declaration

        subramanian100i n@yahoo.com, India wrote:
        On Mar 10, 8:54 am, "santosh" <santosh....@gm ail.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.

        Comment

        • santosh

          #5
          Re: #define vs const declaration


          santosh wrote:
          subramanian100i n@yahoo.com, India wrote:
          On Mar 10, 8:54 am, "santosh" <santosh....@gm ail.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.

          Comment

          • subramanian100in@yahoo.com, India

            #6
            Re: #define vs const declaration

            On Mar 10, 9:12 am, "santosh" <santosh....@gm ail.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 ?

            Comment

            • santosh

              #7
              Re: #define vs const declaration

              subramanian100i n@yahoo.com, India wrote:
              On Mar 10, 9:12 am, "santosh" <santosh....@gm ail.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.

              Comment

              • CBFalconer

                #8
                Re: #define vs const declaration

                "subramanian100 in@yahoo.com, India" wrote:
                "santosh" <santosh....@gm ail.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.securityfoc us.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

                Comment

                • christian.bau

                  #9
                  Re: #define vs const declaration

                  On Mar 10, 12:51 pm, "subramanian10. ..@yahoo.com, India"
                  <subramanian10. ..@yahoo.comwro te:
                  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 };

                  Comment

                  • Servé Laurijssen

                    #10
                    Re: #define vs const declaration


                    <subramanian100 in@yahoo.comwro te in message
                    news:1173536640 .234481.49390@q 40g2000cwq.goog legroups.com...
                    On Mar 10, 9:12 am, "santosh" <santosh....@gm ail.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


                    Comment

                    • Ben Pfaff

                      #11
                      Re: #define vs const declaration

                      "Servé Laurijssen" <ser@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
                      blp@cs.stanford .edu

                      Comment

                      • Ian Collins

                        #12
                        Re: #define vs const declaration

                        Servé Laurijssen wrote:
                        <subramanian100 in@yahoo.comwro te in message
                        news:1173536640 .234481.49390@q 40g2000cwq.goog legroups.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.

                        Comment

                        • websnarf@gmail.com

                          #13
                          Re: #define vs const declaration

                          On Mar 10, 12:13 pm, "Servé Laurijssen" <s...@n.tkwrote :
                          <subramanian10. ..@yahoo.comwro te in message
                          On Mar 10, 9:12 am, "santosh" <santosh....@gm ail.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
                          Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.



                          Comment

                          • subramanian100in@yahoo.com, India

                            #14
                            Re: #define vs const declaration

                            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.comwro te:
                            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

                            Comment

                            • santosh

                              #15
                              Re: #define vs const declaration


                              christian.bau wrote:
                              On Mar 10, 12:51 pm, "subramanian10. ..@yahoo.com, India"
                              <subramanian10. ..@yahoo.comwro te:
                              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.

                              Comment

                              Working...