casting between pointers

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

    #1

    casting between pointers

    hi,

    why does the following produce an output of 64769 instead of 1

    int main()
    {
    bool* pBool = new bool(false);

    unsigned short* pUS = (unsigned short*)pBool;

    cout << *pUS;

    delete pBool;

    return 0;
    }

    I expected the output to be 1 since the size of a bool and the size of
    an unsigned int is 1
  • Jakob Bieling

    #2
    Re: casting between pointers

    scroopy <scroopy@nospam .com> wrote:
    [color=blue]
    > why does the following produce an output of 64769 instead of 1[/color]

    Because you get undefined behaviour when casting one pointer type to
    another and using it as that. You know .. the
    demons-fly-out-of-your-nose issues ..
    [color=blue]
    > int main()
    > {
    > bool* pBool = new bool(false);
    >
    > unsigned short* pUS = (unsigned short*)pBool;
    >
    > cout << *pUS;
    >
    > delete pBool;
    >
    > return 0;
    > }
    >
    > I expected the output to be 1 since the size of a bool and the size of
    > an unsigned int is 1[/color]

    hth
    --
    jb

    (reply address in rot13, unscramble first)


    Comment

    • Jim Langston

      #3
      Re: casting between pointers

      "scroopy" <scroopy@nospam .com> wrote in message
      news:vlkk229ghk r7kvshd2b70p7t9 6o7mbqd85@4ax.c om...[color=blue]
      > hi,
      >
      > why does the following produce an output of 64769 instead of 1
      >
      > int main()
      > {
      > bool* pBool = new bool(false);
      >
      > unsigned short* pUS = (unsigned short*)pBool;
      >
      > cout << *pUS;
      >
      > delete pBool;
      >
      > return 0;
      > }
      >
      > I expected the output to be 1 since the size of a bool and the size of
      > an unsigned int is 1[/color]

      On what system? The size of an unsigned char would be 1, unsigned short
      would probably be 2?


      Comment

      • Carlos Martinez Garcia

        #4
        Re: casting between pointers

        Hi scroopy:

        scroopy wrote:[color=blue]
        > hi,
        >
        > why does the following produce an output of 64769 instead of 1
        >
        > int main()
        > {
        > bool* pBool = new bool(false);
        >
        > unsigned short* pUS = (unsigned short*)pBool;
        >
        > cout << *pUS;
        >
        > delete pBool;
        >
        > return 0;
        > }
        >
        > I expected the output to be 1 since the size of a bool and the size of
        > an unsigned int is 1[/color]

        C++ Standard says "sizeof(boo l) is not required to be 1."
        I think size of bool type depends on your compiler.

        Are you sure that in your compiler sizeof(bool)==s izeof(unsigned short)

        Why do you think the result must be 1?
        Supossing that for your compiler, sizeof(bool)==s izeof(unsigned short),
        the result will be 0.

        Comment

        • Bob Hairgrove

          #5
          Re: casting between pointers

          On Wed, 29 Mar 2006 10:30:48 +0100, scroopy <scroopy@nospam .com>
          wrote:
          [color=blue]
          >I expected the output to be 1 since the size of a bool and the size of
          >an unsigned int is 1[/color]

          This is implementation-defined. On 16-bit Intel x86 platforms, for
          example, sizeof(int) is 2; on 32-bit platforms it is usually 4, and on
          64-bit platforms 8. Other platforms exist where this is different.

          bool can also be implemented using a single bit, even if sizeof(bool)
          implies something else. So even if a bool variable might occupy the
          same storage as an int, the compiler might just ignore all but one of
          the bits (which one, again, would be implementation-defined).

          --
          Bob Hairgrove
          NoSpamPlease@Ho me.com

          Comment

          • Raider

            #6
            Re: casting between pointers

            It seems you are writing 1 byte and reading 2. Second byte is
            undefined. Add the following line:

            assert( sizeof(bool) == sizeof(unsigned short) );

            before casting your pointers. And you will get an error message if
            casting is not possible.

            And IMHO "false" is not equals to 1. I suppose it fills all
            sizeof(bool) bytes by "1" bits. It is 0xFF for sizeof(bool)==1 and so
            on.

            Raider

            Comment

            • Jakob Bieling

              #7
              Re: casting between pointers

              Jakob Bieling <argfhesNGtzkQB Garg@rot13.com> wrote:[color=blue]
              > scroopy <scroopy@nospam .com> wrote:
              >[color=green]
              >> why does the following produce an output of 64769 instead of 1[/color]
              >
              > Because you get undefined behaviour when casting one pointer type[/color]

              Wee correction: it is not undefined, but implementation defined. So
              no demons now ;) But you would have to check your compiler manual or a
              newsgroup dedicated for your compiler to find out why you get that
              result.

              On my system, I would get a similar result to yours. I have
              sizeof(bool) == 1 and sizeof (unsigned short) == 2 .. by just observing
              what is happening, I can see that the short pointer lets me read memory,
              that does not belong to the bool anymore, ie. it contains junk. The
              bool-part is 1 tho. Here at least.

              regards
              --
              jb

              (reply address in rot13, unscramble first)


              Comment

              • Daniel T.

                #8
                Re: casting between pointers

                In article <vlkk229ghkr7kv shd2b70p7t96o7m bqd85@4ax.com>,
                scroopy <scroopy@nospam .com> wrote:
                [color=blue]
                > hi,
                >
                > why does the following produce an output of 64769 instead of 1
                >
                > int main()
                > {
                > bool* pBool = new bool(false);
                >
                > unsigned short* pUS = (unsigned short*)pBool;
                >
                > cout << *pUS;
                >
                > delete pBool;
                >
                > return 0;
                > }
                >
                > I expected the output to be 1 since the size of a bool and the size of
                > an unsigned int is 1[/color]

                On my system, sizeof short is 2, while sizeof bool is 4. What the sizeof
                unsigned int has to do with the above program I have no idea...


                --
                Magic depends on tradition and belief. It does not welcome observation,
                nor does it profit by experiment. On the other hand, science is based
                on experience; it is open to correction by observation and experiment.

                Comment

                • Old Wolf

                  #9
                  Re: casting between pointers

                  Daniel T. wrote:
                  [color=blue]
                  > scroopy <scroopy@nospam .com> wrote:[color=green]
                  >> why does the following produce an output of 64769 instead of 1
                  >>
                  >> bool* pBool = new bool(false);
                  >> unsigned short* pUS = (unsigned short*)pBool;
                  >> cout << *pUS;
                  >>
                  >> I expected the output to be 1 since the size of a bool and the size of
                  >> an unsigned int is 1[/color]
                  >
                  > On my system, sizeof short is 2, while sizeof bool is 4. What the sizeof
                  > unsigned int has to do with the above program I have no idea...[/color]

                  If sizeof(unsigned int) == 1, it would imply that sizeof(unsigned
                  short) is also 1, because short cannot be bigger than int.

                  To answer the original question, and assuming the quoted sizes are
                  correct: probably the bool only uses one bit of storage, so the other
                  bits could contain garbage values. The result of trying to read *pUS
                  will be:
                  - undefined, if sizeof(unsigned short) > sizeof(bool)
                  - undefined, if the content of the memory region in question does
                  not represent a valid unsigned short
                  - well-defined otherwise.

                  ISTR that unsigned types are not allowed to have trap representations ,
                  so the second case might never happen.

                  Comment

                  Working...