Re: Promoting unsigned long int to long int

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Keith Thompson

    Re: Promoting unsigned long int to long int

    pereges <Broli00@gmail. comwrites:
    [...]
    #define ulong unsigned long int
    #define uchar unsigned char
    [...]

    These types already have perfectly good names already. Why give them
    new ones?

    If you must rename them for some reason, use typedefs, not macros.

    --
    Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
  • pereges

    #2
    Re: Promoting unsigned long int to long int

    On Jun 30, 8:49 pm, Keith Thompson <ks...@mib.orgw rote:
    These types already have perfectly good names already. Why give them
    new ones?
    No purpose really. Just that in some of my functions, there are too
    many parameters already and the whole thing doesn't fit in single
    line.
    If you must rename them for some reason, use typedefs, not macros.
    >
    ok

    Comment

    • Keith Thompson

      #3
      Re: Promoting unsigned long int to long int

      pereges <Broli00@gmail. comwrites:
      On Jun 30, 8:49 pm, Keith Thompson <ks...@mib.orgw rote:
      These types already have perfectly good names already. Why give them
      new ones?
      >
      No purpose really. Just that in some of my functions, there are too
      many parameters already and the whole thing doesn't fit in single
      line.
      So write it on multiple lines.

      Given:
      #define ulong unsigned long int
      #define uchar unsigned char
      or, preferably:
      typedef unsigned long int ulong;
      typedef unsigned char uchar;

      If I'm reading your code and see a reference to "ulong", I can't
      understand what it means until I've confirmed that "ulong" means
      "unsigned long int" (which I'd probably write as "unsigned long").
      And I have to wonder whether you might some day change the definition
      so "ulong" means something else.

      If you drop the definition of "ulong" and just write "unsigned long"
      directly, I don't have to wonder; your code will be clearer.

      Now if you want a typedef whose name says something about how you're
      using the type, rather than how it's define, that's a different
      matter.

      You use "ulong" for array indices; it would make more sense to use
      size_t.

      You use "uchar" for a parameter that can only have the value 0, 1, or
      2. I'd use int. Using unsigned char might save some space, but it's
      just as likely to cost you in code size, since the compiler has to
      generate code to expand the 1-byte value into a word before it can
      operate on it, and to shrink it back down to 1 byte before storing it.
      You also make the reader wonder whether there's some fundamental
      reason for this value to fit into a byte (there isn't). If you had an
      array of these things, it would make sense to use a smaller type.
      Since it's just a single parameter, using int is fine.
      If you must rename them for some reason, use typedefs, not macros.
      >
      ok
      --
      Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
      Nokia
      "We must do something. This is something. Therefore, we must do this."
      -- Antony Jay and Jonathan Lynn, "Yes Minister"

      Comment

      • Jens Thoms Toerring

        #4
        Re: Promoting unsigned long int to long int

        pereges <Broli00@gmail. comwrote:
        On Jun 30, 8:49 pm, Keith Thompson <ks...@mib.orgw rote:
        These types already have perfectly good names already. Why give them
        new ones?
        No purpose really. Just that in some of my functions, there are too
        many parameters already and the whole thing doesn't fit in single
        line.
        You don't have to put all arguments etc. on a single line, e.g.

        void
        quicksort( vector ** parent_vpa,
        unsigned long left,
        unsigned long right,
        unsigned char axis )

        will do nicely and may even increases readabilty (and there's
        still space for adding a short comment;-)

        Regards, Jens
        --
        \ Jens Thoms Toerring ___ jt@toerring.de
        \______________ ____________ http://toerring.de

        Comment

        • pereges

          #5
          Re: Promoting unsigned long int to long int

          On Jun 30, 9:43 pm, Keith Thompson <ks...@mib.orgw rote:
          So write it on multiple lines.
          >
          Given:
          #define ulong unsigned long int
          #define uchar unsigned char
          or, preferably:
          typedef unsigned long int ulong;
          typedef unsigned char uchar;
          >
          If I'm reading your code and see a reference to "ulong", I can't
          understand what it means until I've confirmed that "ulong" means
          "unsigned long int" (which I'd probably write as "unsigned long").
          And I have to wonder whether you might some day change the definition
          so "ulong" means something else.
          >
          If you drop the definition of "ulong" and just write "unsigned long"
          directly, I don't have to wonder; your code will be clearer.
          >
          Now if you want a typedef whose name says something about how you're
          using the type, rather than how it's define, that's a different
          matter.
          >
          You use "ulong" for array indices; it would make more sense to use
          size_t.
          >
          You use "uchar" for a parameter that can only have the value 0, 1, or
          2. I'd use int. Using unsigned char might save some space, but it's
          just as likely to cost you in code size, since the compiler has to
          generate code to expand the 1-byte value into a word before it can
          operate on it, and to shrink it back down to 1 byte before storing it.
          You also make the reader wonder whether there's some fundamental
          reason for this value to fit into a byte (there isn't). If you had an
          array of these things, it would make sense to use a smaller type.
          Since it's just a single parameter, using int is fine.
          >
          If I had to store 80-90 of structures and each had a member say
          'axis'. How much of a difference would using a unsigned char over int
          would make (for that member) ? For my program, space as well as
          performance are important.

          Comment

          • pereges

            #6
            Re: Promoting unsigned long int to long int

            On Jun 30, 9:44 pm, j...@toerring.d e (Jens Thoms Toerring) wrote:
            You don't have to put all arguments etc. on a single line, e.g.
            >
            void
            quicksort( vector ** parent_vpa,
            unsigned long left,
            unsigned long right,
            unsigned char axis )
            >
            will do nicely and may even increases readabilty (and there's
            still space for adding a short comment;-)
            >
            Just a style question (I'm cleaning up my code)

            Which of the two in your opinion is better ?

            void quicksort( vector ** parent_vpa,
            unsigned long left,
            unsigned long right,
            unsigned char axis ) {

            ....
            ....
            }

            OR

            void quicksort( vector ** parent_vpa,
            unsigned long left,
            unsigned long right,
            unsigned char axis )
            {

            ....
            ....
            }

            Comment

            • Keith Thompson

              #7
              Re: Promoting unsigned long int to long int

              pereges <Broli00@gmail. comwrites:
              [...]
              If I had to store 80-90 of structures and each had a member say
              'axis'. How much of a difference would using a unsigned char over int
              would make (for that member) ? For my program, space as well as
              performance are important.
              It depends.

              If "axis" is the last member of your structure, it's likely that the
              compiler will insert padding after it if it's an unsigned char anyway,
              so declaring it as int would make no difference.

              In the worst case (well, the worst plausible case), the cost of using
              int would be 90 * (sizeof(int) - 1), which is trivial on anything
              other than a small embedded system. But using int could easily save
              code space, depending on the underlying architecture. And if you're
              concerned about speed, it's likely (though by no means guaranteed)
              that operations on int will be faster than operations on unsigned
              char.

              I think you're engaging in premature micro-optimization.

              --
              Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
              Nokia
              "We must do something. This is something. Therefore, we must do this."
              -- Antony Jay and Jonathan Lynn, "Yes Minister"

              Comment

              • pete

                #8
                Re: Promoting unsigned long int to long int

                pereges wrote:
                If I had to store 80-90 of structures and each had a member say
                'axis'. How much of a difference would using a unsigned char over int
                would make (for that member) ? For my program, space as well as
                performance are important.
                On my machine it would make
                no difference in terms of usable memory saved
                and it would slow down the arithmetic operations.

                An array of char will save usable space over an array of int;
                but if I only define several distinct char objects in a function,
                my compiler will place them on int boundaries.
                Meaning that in between the char objects,
                there are three bytes of unusable memory
                (sizof(int) equals 4 on my machine).

                char arithmetic on my machine
                is accomplished in int size objects or registers,
                requiring additional masking operations.

                --
                pete

                Comment

                • pereges

                  #9
                  Re: Promoting unsigned long int to long int

                  btw, i see a lot of people saying that size_t must be used over
                  unsigned long. The question is are there any fix guidelines as to when
                  i should and when i shouldn't use size_t. It seems to me that size_t
                  is most commonly used in three situations :

                  1. Size of array or any object.
                  2. Array indices.
                  3. Count of something.

                  Although I don't see why using unsigned long for any of the above
                  could be wrong. It may not be a necessity to use size_t

                  Comment

                  • Jens Thoms Toerring

                    #10
                    Re: Promoting unsigned long int to long int

                    pereges <Broli00@gmail. comwrote:
                    On Jun 30, 9:44 pm, j...@toerring.d e (Jens Thoms Toerring) wrote:
                    You don't have to put all arguments etc. on a single line, e.g.

                    void
                    quicksort( vector ** parent_vpa,
                    unsigned long left,
                    unsigned long right,
                    unsigned char axis )

                    will do nicely and may even increases readabilty (and there's
                    still space for adding a short comment;-)
                    Just a style question (I'm cleaning up my code)
                    Which of the two in your opinion is better ?
                    void quicksort( vector ** parent_vpa,
                    unsigned long left,
                    unsigned long right,
                    unsigned char axis ) {
                    ....
                    ....
                    }
                    OR
                    void quicksort( vector ** parent_vpa,
                    unsigned long left,
                    unsigned long right,
                    unsigned char axis )
                    {
                    ....
                    ....
                    }
                    I usually go for the way that has more white-space, probably
                    because my eyes aren't as good anymore as they used to be,
                    so I usually put the opening brace on a new line (at least
                    when I am writing C, in Perl I do it the other way round;-).
                    So I personally don't mind much. Just pick something that
                    looks good to you and is logically consistent (or, if you
                    are working somewhere, use the in-house style) and use that
                    consistently.
                    Regards, Jens
                    --
                    \ Jens Thoms Toerring ___ jt@toerring.de
                    \______________ ____________ http://toerring.de

                    Comment

                    • Keith Thompson

                      #11
                      Re: Promoting unsigned long int to long int

                      pereges <Broli00@gmail. comwrites:
                      btw, i see a lot of people saying that size_t must be used over
                      unsigned long. The question is are there any fix guidelines as to when
                      i should and when i shouldn't use size_t. It seems to me that size_t
                      is most commonly used in three situations :
                      >
                      1. Size of array or any object.
                      2. Array indices.
                      3. Count of something.
                      >
                      Although I don't see why using unsigned long for any of the above
                      could be wrong. It may not be a necessity to use size_t
                      size_t can represent the size in bytes of any object (since it's the
                      type of the result of the sizeof operator and of the argument to
                      malloc(). It can therefore represent the size in elements of any
                      array object, making it suitable for array indices.

                      The third is a bit more iffy; it depends on what you're counting.

                      But there are no such guarantees for unsigned long. Consider a
                      hypothetical system where unsigned long is 32 bits and size_t is 64
                      bits. You might have objects whose size cannnot be represented as an
                      unsigned long value.

                      The question is, why would you want to use unsigned long rather than
                      size_t?

                      --
                      Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                      Nokia
                      "We must do something. This is something. Therefore, we must do this."
                      -- Antony Jay and Jonathan Lynn, "Yes Minister"

                      Comment

                      • pete

                        #12
                        Re: Promoting unsigned long int to long int

                        pereges wrote:
                        btw, i see a lot of people saying that size_t must be used over
                        unsigned long. The question is are there any fix guidelines as to when
                        i should and when i shouldn't use size_t. It seems to me that size_t
                        is most commonly used in three situations :
                        >
                        1. Size of array or any object.
                        2. Array indices.
                        3. Count of something.
                        >
                        Although I don't see why using unsigned long for any of the above
                        could be wrong. It may not be a necessity to use size_t
                        size_t is for counting parts of an object.
                        That's cases 1 and 2 of your list.
                        In terms of efficiency, you can expect that size_t
                        will probably be the fastest type that is big enough
                        to do the job.

                        When I count nodes in linked list, I use long unsigned.
                        The number of nodes that malloc can generate,
                        isn't related to size_t.

                        --
                        pete

                        Comment

                        • santosh

                          #13
                          Re: Promoting unsigned long int to long int

                          pereges wrote:
                          btw, i see a lot of people saying that size_t must be used over
                          unsigned long. The question is are there any fix guidelines as to when
                          i should and when i shouldn't use size_t. It seems to me that size_t
                          is most commonly used in three situations :
                          >
                          1. Size of array or any object.
                          This is the purpose of size_t.
                          2. Array indices.
                          IMHO, any unsigned type of suitable range will do. Sometimes signed
                          types are convenient too, for certain forms of loops, and rarely, to
                          index with a negative offset.
                          3. Count of something.
                          Again a suitable unsigned type, not necessarily size_t should be okay.
                          Although I don't see why using unsigned long for any of the above
                          could be wrong. It may not be a necessity to use size_t
                          You are correct. In C, size_t has the only purpose of being large enough
                          to hold the size in bytes of the largest possible single object. Also
                          the sizeof operator yields a size_t value for related reasons. It is
                          not necessarily a suitable type for counting things are holding
                          offsets, indexes and such.

                          Generally, a careful examination of the origin, purpose and types of use
                          a value may be subject to will give you a clue as to the most suitable
                          type of object to represent it.

                          Comment

                          • pereges

                            #14
                            Re: Promoting unsigned long int to long int

                            On Jun 30, 11:19 pm, santosh <santosh....@gm ail.comwrote:
                            1. Size of array or any object.
                            >
                            This is the purpose of size_t.
                            >
                            2. Array indices.
                            >
                            IMHO, any unsigned type of suitable range will do. Sometimes signed
                            types are convenient too, for certain forms of loops, and rarely, to
                            index with a negative offset.
                            >
                            3. Count of something.
                            >
                            Again a suitable unsigned type, not necessarily size_t should be okay.
                            >
                            Although I don't see why using unsigned long for any of the above
                            could be wrong. It may not be a necessity to use size_t
                            >
                            You are correct. In C, size_t has the only purpose of being large enough
                            to hold the size in bytes of the largest possible single object. Also
                            the sizeof operator yields a size_t value for related reasons. It is
                            not necessarily a suitable type for counting things are holding
                            offsets, indexes and such.
                            >
                            Generally, a careful examination of the origin, purpose and types of use
                            a value may be subject to will give you a clue as to the most suitable
                            type of object to represent it.
                            Thanks for the clarification. After reading this, I guess I will
                            continue using unsigned long for all the 3 situations.

                            Comment

                            • pereges

                              #15
                              Re: Promoting unsigned long int to long int

                              On Jun 30, 11:13 pm, Keith Thompson <ks...@mib.orgw rote:
                              size_t can represent the size in bytes of any object (since it's the
                              type of the result of the sizeof operator and of the argument to
                              malloc(). It can therefore represent the size in elements of any
                              array object, making it suitable for array indices.
                              >
                              The third is a bit more iffy; it depends on what you're counting.
                              >
                              But there are no such guarantees for unsigned long. Consider a
                              hypothetical system where unsigned long is 32 bits and size_t is 64
                              bits. You might have objects whose size cannnot be represented as an
                              unsigned long value.
                              >
                              The question is, why would you want to use unsigned long rather than
                              size_t?
                              Suppose I have a structure:

                              struct mesh
                              {
                              unsigned long nvert; /* number of vertices */
                              unsigned long ntri; /* number of triangles */
                              vector *vert; /* pointer to array of vertices */
                              triangle *tri; /* pointer to array of triangles */
                              };

                              Now, according to what you are saying nvert and ntri should have data
                              type size_t instead. In my program its possible for nvert and ntri to
                              be in millions. I have been adviced that size_t in many cases causes
                              loss of data, hence I'm skecptical about its use. Also, I would want
                              to keep things consistent.

                              Comment

                              Working...