[r2038] Weird randart thing

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Tiburon Silverflame
    Swordsman
    • Feb 2010
    • 405

    #16
    Here's a good article on how HP's C compilers do it.



    There are gotchas in here, and definitely compiler dependencies. Unless there's a damn good reason, I'd also say, there should never be any unsigned int, short, or byte variables in the code, and there shouldn't be any signed/unsigned conversions.

    Comment

    • Pete Mack
      Prophet
      • Apr 2007
      • 6883

      #17
      All well and good, but we haven't solved the original problem.

      Magnate--what file and line number should I look at to find this problem?

      -Pete

      Comment

      • zaimoni
        Knight
        • Apr 2007
        • 590

        #18
        Originally posted by Tiburon Silverflame
        Here's a good article on how HP's C compilers do it.



        There are gotchas in here, and definitely compiler dependencies. Unless there's a damn good reason, I'd also say, there should never be any unsigned int, short, or byte variables in the code, and there shouldn't be any signed/unsigned conversions.
        Sigh.
        Welcome to h-basic.h:
        Code:
        #define N_ELEMENTS(a) (sizeof(a) / sizeof((a)[0]))
        This macro is needed for accurate upper loop bounds on static arrays; result is an unsigned integer type per C90/C99 standards. Any for-loop counter variable compared to this has to be unsigned (preferably std::size_t, although unsigned int is often tenable) to avoid signed/unsigned conversion when comparing.
        Zaiband: end the "I shouldn't have survived that" experience. V3.0.6 fork on Hg.
        Zaiband 3.0.10 ETA Mar. 7 2011 (Yes, schedule slipped. Latest testing indicates not enough assert() calls to allow release.)
        Z.C++: pre-alpha C/C++ compiler system (usable preprocessor). Also on Hg. Z.C++ 0.0.10 ETA December 31 2011

        Comment

        • PowerDiver
          Prophet
          • Mar 2008
          • 2820

          #19
          Originally posted by zaimoni
          Sigh.
          Welcome to h-basic.h:
          Code:
          #define N_ELEMENTS(a) (sizeof(a) / sizeof((a)[0]))
          This macro is needed for accurate upper loop bounds on static arrays; result is an unsigned integer type per C90/C99 standards. Any for-loop counter variable compared to this has to be unsigned (preferably std::size_t, although unsigned int is often tenable) to avoid signed/unsigned conversion when comparing.
          Why would you care about conversion when comparing? Was this historically a problem? My imagination is lacking in figuring out what the issues could be.

          If it is a problem, a gross solution might be to define the macro to cast to signed int. Besides being ridiculous, is there anything intrinsically broken about this gross idea?

          The code I offer is unlikely to have size_t in it, and I bet there are other contributors equally unaware of when to use size_t.

          Comment

          • Magnate
            Angband Devteam member
            • May 2007
            • 5110

            #20
            Originally posted by Pete Mack
            All well and good, but we haven't solved the original problem.

            Magnate--what file and line number should I look at to find this problem?

            -Pete
            It's wiz-spoil.c line 513 ... but thanks for making me look it up, because I see now that I *did* use %u, copying ignorantly from the Min lev / Max lev / Gen chance which also used %u, instead of the weight which used %d.

            Ah well, mystery solved. Thanks folks!
            "Been away so long I hardly knew the place, gee it's good to be back home" - The Beatles

            Comment

            • Tiburon Silverflame
              Swordsman
              • Feb 2010
              • 405

              #21
              Code:
              #define N_ELEMENTS(a) (sizeof(a) / sizeof((a)[0]))
              This macro is needed for accurate upper loop bounds...

              When I see stuff like this, boy howdy does that say, it's time to get the *_@#$ OUT of a language that is so damn primitive that this is necessary.

              Comment

              • zaimoni
                Knight
                • Apr 2007
                • 590

                #22
                Originally posted by PowerDiver
                Why would you care about conversion when comparing?
                When I care that -1<0U is counterintuitively false. Which doesn't happen in those pervasive for-loops, counting up from 0 inclusive, in V.
                Originally posted by PowerDiver
                Was this historically a problem? My imagination is lacking in figuring out what the issues could be.
                Context: I am showing a presumed 4th-generation programming language addict that this superannuated, knee-jerk "safety fix" for C programs is a logic paradox.
                Zaiband: end the "I shouldn't have survived that" experience. V3.0.6 fork on Hg.
                Zaiband 3.0.10 ETA Mar. 7 2011 (Yes, schedule slipped. Latest testing indicates not enough assert() calls to allow release.)
                Z.C++: pre-alpha C/C++ compiler system (usable preprocessor). Also on Hg. Z.C++ 0.0.10 ETA December 31 2011

                Comment

                • PowerDiver
                  Prophet
                  • Mar 2008
                  • 2820

                  #23
                  Originally posted by zaimoni
                  When I care that -1<0U is counterintuitively false. Which doesn't happen in those pervasive for-loops, counting up from 0 inclusive, in V.
                  Context: I am showing a presumed 4th-generation programming language addict that this superannuated, knee-jerk "safety fix" for C programs is a logic paradox.
                  Didn't there use to be some strange contexts where negative indices meant floor items in V? Maybe it is still in there.

                  It is quite possible to imagine using negative loop counters even when indexing into arrays. So much so that, a long time ago, I decided, for my own coding purposes, that loop counters should be signed rather than unsigned for extensibility even when it appears unsigned makes more sense. My only exception is looping over all possible packed bitflag combinations.

                  The only reason I'm beating this horse is that I always found the size_t stuff very strange. At least now I have an inkling why someone might want to do it. Thanks for that.

                  Comment

                  • zaimoni
                    Knight
                    • Apr 2007
                    • 590

                    #24
                    Originally posted by PowerDiver
                    It is quite possible to imagine using negative loop counters even when indexing into arrays.
                    Yes. (My C memory manager reimplementation for Windows/GCC uses this to allow O(1) access to the exact size of a malloc-allocated memory block.) Note that ptrdiff_t, the result of subtracting two pointers, is a signed integer type -- and that strictly speaking operator [] is expecting one of its arguments to be a ptrdiff_t.

                    (Of course, std::size_t is a C++-ism; its C equivalent is size_t).
                    Zaiband: end the "I shouldn't have survived that" experience. V3.0.6 fork on Hg.
                    Zaiband 3.0.10 ETA Mar. 7 2011 (Yes, schedule slipped. Latest testing indicates not enough assert() calls to allow release.)
                    Z.C++: pre-alpha C/C++ compiler system (usable preprocessor). Also on Hg. Z.C++ 0.0.10 ETA December 31 2011

                    Comment

                    • zaimoni
                      Knight
                      • Apr 2007
                      • 590

                      #25
                      Originally posted by PowerDiver
                      Didn't there use to be some strange contexts where negative indices meant floor items in V? Maybe it is still in there.
                      It's still there. But those indices are never actually used to dereference arrays as negative values.
                      Zaiband: end the "I shouldn't have survived that" experience. V3.0.6 fork on Hg.
                      Zaiband 3.0.10 ETA Mar. 7 2011 (Yes, schedule slipped. Latest testing indicates not enough assert() calls to allow release.)
                      Z.C++: pre-alpha C/C++ compiler system (usable preprocessor). Also on Hg. Z.C++ 0.0.10 ETA December 31 2011

                      Comment

                      Working...
                      😀
                      😂
                      🥰
                      😘
                      🤢
                      😎
                      😞
                      😡
                      👍
                      👎