strange crash - cast short* to int*

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gernot Frisch

    strange crash - cast short* to int*

    Hi,

    the program below workes w/o problems on a GP2X and on the PC, but my
    PocketPC (using GCC 3.3.3) crashes.
    Very dissapointing, since I expect some speed boost from it.
    Thnak you for your help.


    int dx=..; // number of pixels width to draw
    // one pixel is one unsigned short

    // Get some pointer to the screen to draw a rect
    unsigned short* pScreen = getColorBuffer( x,y);

    int num_q = dx>>2; // number of 4 pixel pairs
    int num_r = (dx & 0x00000003); // remaining pixels -loop unrolling

    // prepair a 2 pixel pair "long"
    unsigned long colcol = col | ((unsigned long)col << 16);
    // get a long pointer to that screen (draw 2 "shorts" at once)
    unsigned long* pScLong = (unsigned long*)pScreen;
    ... loop for each line
    for(register int rx=0; rx<num_q; ++rx)
    {
    #ifdef THIS_CRASHES
    *pScLong++ = colcol;
    *pScLong++ = colcol;
    pScreen+=4;
    #else
    *pScreen++ = col;
    *pScreen++ = col;
    *pScreen++ = col;
    *pScreen++ = col;
    #endif
    }
    ... remaining pixels and next lines...


    --
    ------------------------------------
    Gernot Frisch
    GLBasic is a programming language that supports multiple platforms like e.g. iPhone


  • Sam

    #2
    Re: strange crash - cast short* to int*

    Gernot Frisch writes:
    Hi,
    >
    the program below workes w/o problems on a GP2X and on the PC, but my
    PocketPC (using GCC 3.3.3) crashes.
    Very dissapointing, since I expect some speed boost from it.
    Thnak you for your help.
    >
    >
    int dx=..; // number of pixels width to draw
    // one pixel is one unsigned short
    >
    // Get some pointer to the screen to draw a rect
    unsigned short* pScreen = getColorBuffer( x,y);
    There is no "getColorBuffer ()" function in the standard C++ library. Try
    asking for help on a newsgroup for your hardware platform.
    int num_q = dx>>2; // number of 4 pixel pairs
    int num_r = (dx & 0x00000003); // remaining pixels -loop unrolling
    >
    // prepair a 2 pixel pair "long"
    unsigned long colcol = col | ((unsigned long)col << 16);
    // get a long pointer to that screen (draw 2 "shorts" at once)
    unsigned long* pScLong = (unsigned long*)pScreen;
    ... loop for each line
    for(register int rx=0; rx<num_q; ++rx)
    {
    #ifdef THIS_CRASHES
    *pScLong++ = colcol;
    *pScLong++ = colcol;
    pScreen+=4;
    #else
    *pScreen++ = col;
    *pScreen++ = col;
    *pScreen++ = col;
    *pScreen++ = col;
    #endif
    }
    You did not specify your platform-specific word sizes. Assuming
    sizeof(short)=2 , and sizeof(long)=4, the above math does not add up. Again,
    has nothing to do with C++, the language. You can get a more definitive
    answer on a more appropriate newsgroup.


    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.9 (GNU/Linux)

    iEYEABECAAYFAkj 1z5EACgkQx9p3GY HlUOLt4QCfdvIqG KyvW1XX9bxtR6kO/msh
    jNAAn0a3FDlNUNR UzYohePfpak9LGk fN
    =4Fb0
    -----END PGP SIGNATURE-----

    Comment

    • blargg

      #3
      Re: strange crash - cast short* to int*

      In article <6llpf5Fd50llU1 @mid.individual .net>, "Gernot Frisch"
      <me@privacy.net wrote:
      the program below workes w/o problems on a GP2X and on the PC, but my
      PocketPC (using GCC 3.3.3) crashes.
      Very dissapointing, since I expect some speed boost from it.
      [...]
      // get a long pointer to that screen (draw 2 "shorts" at once)
      unsigned long* pScLong = (unsigned long*)pScreen;
      ... loop for each line
      for(register int rx=0; rx<num_q; ++rx)
      {
      #ifdef THIS_CRASHES
      *pScLong++ = colcol;
      *pScLong++ = colcol;
      pScreen+=4;
      #else
      *pScreen++ = col;
      *pScreen++ = col;
      *pScreen++ = col;
      *pScreen++ = col;
      #endif
      }
      ... remaining pixels and next lines...
      On some platforms, long has a stricter alignment requirement than short,
      and violating that causes a crash. In your case, it's likely that long
      must be aligned on a 4-byte boundary. One fairly portable solution is to
      do an extra short if the address isn't yet 4-byte aligned, then do the
      bulk as longs.

      Comment

      • acehreli@gmail.com

        #4
        Re: strange crash - cast short* to int*

        On Oct 15, 4:10 am, Sam <s...@email-scan.comwrote:
        Gernot Frisch writes:
        Hi,
        >
        the program below workes w/o problems on a GP2X and on the PC, but my
        PocketPC (using GCC 3.3.3) crashes.
        Very dissapointing, since I expect some speed boost from it.
        Thnak you for your help.
        >
         int dx=..; // number of pixels width to draw
         // one pixel is one unsigned short
        >
         // Get some pointer to the screen to draw a rect
         unsigned short* pScreen = getColorBuffer( x,y);
        >
        There is no "getColorBuffer ()" function in the standard C++ library. Try
        asking for help on a newsgroup for your hardware platform.
        How about dx, x, y, etc. Should the poster go elsewhere because they
        are not in the standard as well? Do you have problems with people
        using foo()? Is that standard?
        You did not specify your platform-specific word sizes.
        Why is that needed for the question? If the poster new that the
        platform-specific word sizes had anything with it, he wouldn't be
        surprised.
        Assuming
        sizeof(short)=2 , and sizeof(long)=4, the above math does not add up. Again,
        has nothing to do with C++, the language.
        Do you know much about the C++ language? Are you claiming that sizeof
        applied to types or their comparisons are not on-topic here? Your
        releasing steam is off-topic here.
        You can get a more definitive
        answer on a more appropriate newsgroup.
        This is an appropriate newsgroup. Why seek more?

        Ali

        Comment

        • Kai-Uwe Bux

          #5
          Re: strange crash - cast short* to int*

          acehreli@gmail. com wrote:
          On Oct 15, 4:10 am, Sam <s...@email-scan.comwrote:
          >Gernot Frisch writes:
          Hi,
          >>
          the program below workes w/o problems on a GP2X and on the PC, but my
          PocketPC (using GCC 3.3.3) crashes.
          Very dissapointing, since I expect some speed boost from it.
          Thnak you for your help.
          >>
          int dx=..; // number of pixels width to draw
          // one pixel is one unsigned short
          >>
          // Get some pointer to the screen to draw a rect
          unsigned short* pScreen = getColorBuffer( x,y);
          >>
          >There is no "getColorBuffer ()" function in the standard C++ library. Try
          >asking for help on a newsgroup for your hardware platform.
          >
          How about dx, x, y, etc. Should the poster go elsewhere because they
          are not in the standard as well? Do you have problems with people
          using foo()? Is that standard?
          >
          >You did not specify your platform-specific word sizes.
          >
          Why is that needed for the question? If the poster new that the
          platform-specific word sizes had anything with it, he wouldn't be
          surprised.
          >
          >Assuming
          >sizeof(short)= 2, and sizeof(long)=4, the above math does not add up.
          >Again, has nothing to do with C++, the language.
          >
          Do you know much about the C++ language? Are you claiming that sizeof
          applied to types or their comparisons are not on-topic here?
          I did not read that in that part you quoted.
          Your releasing steam is off-topic here.
          Hm, the posting from Sam does not sound very aggressive, as opposed to
          yours.

          >You can get a more definitive
          >answer on a more appropriate newsgroup.
          >
          This is an appropriate newsgroup. Why seek more?
          Because the topical answer to the OP is that the cast

          unsigned long* pScLong = (unsigned long*)pScreen;

          and the following usage of pScLong is undefined behavior. That answer is
          clearly not helpfull but all that the standard has to say about his
          program. Therefore, looking for answers elsewhere is a good idea.


          Best

          Kai-Uwe Bux

          Comment

          • James Kanze

            #6
            Re: strange crash - cast short* to int*

            On Oct 15, 11:14 pm, Kai-Uwe Bux <jkherci...@gmx .netwrote:
            acehr...@gmail. com wrote:
            On Oct 15, 4:10 am, Sam <s...@email-scan.comwrote:
            Gernot Frisch writes:
            the program below workes w/o problems on a GP2X and on
            the PC, but my PocketPC (using GCC 3.3.3) crashes. Very
            dissapointing, since I expect some speed boost from it.
            Thnak you for your help.
            int dx=..; // number of pixels width to draw
            // one pixel is one unsigned short
            // Get some pointer to the screen to draw a rect
            unsigned short* pScreen = getColorBuffer( x,y);
            There is no "getColorBuffer ()" function in the standard C++
            library. Try asking for help on a newsgroup for your
            hardware platform.
            How about dx, x, y, etc. Should the poster go elsewhere
            because they are not in the standard as well? Do you have
            problems with people using foo()? Is that standard?
            You did not specify your platform-specific word sizes.
            Why is that needed for the question? If the poster new that
            the platform-specific word sizes had anything with it, he
            wouldn't be surprised.
            Assuming sizeof(short)=2 , and sizeof(long)=4, the above
            math does not add up. Again, has nothing to do with C++,
            the language.
            Do you know much about the C++ language? Are you claiming
            that sizeof applied to types or their comparisons are not
            on-topic here?
            I did not read that in that part you quoted.
            Your releasing steam is off-topic here.
            Hm, the posting from Sam does not sound very aggressive, as
            opposed to yours.
            His posting was presumptuous, and just noise, and of no help to
            anyone.
            You can get a more definitive answer on a more appropriate
            newsgroup.
            This is an appropriate newsgroup. Why seek more?
            Because the topical answer to the OP is that the cast
            unsigned long* pScLong = (unsigned long*)pScreen;
            and the following usage of pScLong is undefined behavior. That
            answer is clearly not helpful
            Isn't it? It's certainly a necessary starting point: the code
            was illegal, and if it worked on some platforms, it was just by
            accident.

            One might also point out why it was decided that this should be
            undefined behavior. General considerations about alignment,
            etc., are also on topic here.

            Presumably, of course, the problem is due to the fact that on
            some platforms, long requires more strict alignment than short,
            and that his original short* didn't meet those requirements. On
            an Intel processor, this will slow things up considerably, but
            the code will stil run; on most processors, alignement errors
            will result in a hardware trap, which the system maps into a
            core dump or something similar.
            but all that the standard has to say about his program.
            Therefore, looking for answers elsewhere is a good idea.
            No. His problem is pure C++, and I don't know where else he
            should look. The fact that he gets the original pointer from a
            function which is not in the standard is completely irrelevant
            (but the names do help us understand the context, and why he is
            trying to do this).

            --
            James Kanze (GABI Software) email:james.kan ze@gmail.com
            Conseils en informatique orientée objet/
            Beratung in objektorientier ter Datenverarbeitu ng
            9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

            Comment

            • Kai-Uwe Bux

              #7
              Re: strange crash - cast short* to int*

              James Kanze wrote:
              On Oct 15, 11:14 pm, Kai-Uwe Bux <jkherci...@gmx .netwrote:
              >acehr...@gmail .com wrote:
              On Oct 15, 4:10 am, Sam <s...@email-scan.comwrote:
              >Gernot Frisch writes:
              the program below workes w/o problems on a GP2X and on
              the PC, but my PocketPC (using GCC 3.3.3) crashes. Very
              dissapointing, since I expect some speed boost from it.
              Thnak you for your help.
              >
              int dx=..; // number of pixels width to draw
              // one pixel is one unsigned short
              >
              // Get some pointer to the screen to draw a rect
              unsigned short* pScreen = getColorBuffer( x,y);
              >
              >There is no "getColorBuffer ()" function in the standard C++
              >library. Try asking for help on a newsgroup for your
              >hardware platform.
              >
              How about dx, x, y, etc. Should the poster go elsewhere
              because they are not in the standard as well? Do you have
              problems with people using foo()? Is that standard?
              >
              >You did not specify your platform-specific word sizes.
              >
              Why is that needed for the question? If the poster new that
              the platform-specific word sizes had anything with it, he
              wouldn't be surprised.
              >
              >Assuming sizeof(short)=2 , and sizeof(long)=4, the above
              >math does not add up. Again, has nothing to do with C++,
              >the language.
              >
              Do you know much about the C++ language? Are you claiming
              that sizeof applied to types or their comparisons are not
              on-topic here?
              >
              >I did not read that in that part you quoted.
              >
              Your releasing steam is off-topic here.
              >
              >Hm, the posting from Sam does not sound very aggressive, as
              >opposed to yours.
              >
              His posting was presumptuous, and just noise, and of no help to
              anyone.
              >
              >You can get a more definitive answer on a more appropriate
              >newsgroup.
              >
              This is an appropriate newsgroup. Why seek more?
              >
              >Because the topical answer to the OP is that the cast
              >
              > unsigned long* pScLong = (unsigned long*)pScreen;
              >
              >and the following usage of pScLong is undefined behavior. That
              >answer is clearly not helpful
              >
              Isn't it? It's certainly a necessary starting point: the code
              was illegal, and if it worked on some platforms, it was just by
              accident.
              The "accident" is probably what is important.

              One might also point out why it was decided that this should be
              undefined behavior. General considerations about alignment,
              etc., are also on topic here.
              That was is Sam's posting.

              Presumably, of course, the problem is due to the fact that on
              some platforms, long requires more strict alignment than short,
              and that his original short* didn't meet those requirements. On
              an Intel processor, this will slow things up considerably, but
              the code will stil run; on most processors, alignement errors
              will result in a hardware trap, which the system maps into a
              core dump or something similar.
              >
              >but all that the standard has to say about his program.
              >Therefore, looking for answers elsewhere is a good idea.
              >
              No. His problem is pure C++,
              I beg to differ. His problem, the way I understand it, is to write code that
              works on the target platform. If that involves undefined behavior, so be
              it. Why the particular code he proposes breaks is a matter of how the
              platform defines the behavior left undefined by the standard. Also, what
              possible fixes there are (to improve the speed) is platform specific.
              and I don't know where else he should look.
              Neither do I. But that does not imply that there is no better place.
              The fact that he gets the original pointer from a
              function which is not in the standard is completely irrelevant
              (but the names do help us understand the context, and why he is
              trying to do this).
              True. But what the OP is trying to do with that pointer gives us a clue that
              alignment is probably at the heart of the matter. What alignment
              requirements he actually has, is platform specific.


              Best

              Kai-Uwe Bux

              Comment

              Working...