will the pointer survive?

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

    will the pointer survive?

    Hi all,

    Will my pointer survive this conversion?

    MyType_tag MyType;
    MyType_tag* pMyPntr = &MyType;
    int iUglyTempStorag e = (int)pMyPntr;
    MyType_tag* pMyOtherPntr = (MyType_tag*)iU glyTempStorage;
    --

    Best wishes,
    Allen




  • Ivan Vecerina

    #2
    Re: will the pointer survive?

    "Allen" <allen-terri-ng!@#att.net> wrote in message
    news:ayZsb.5916 2$Ec1.3577677@b gtnsc05-news.ops.worldn et.att.net...[color=blue]
    > Will my pointer survive this conversion?
    >
    > MyType_tag MyType;
    > MyType_tag* pMyPntr = &MyType;
    > int iUglyTempStorag e = (int)pMyPntr;
    > MyType_tag* pMyOtherPntr = (MyType_tag*)iU glyTempStorage;[/color]

    No. (except on some platforms)


    hth,
    Ivan
    --
    Ivan Vecerina - expert in medical devices, software - info, links, contact information, code snippets



    Comment

    • Chris Theis

      #3
      Re: will the pointer survive?


      "Allen" <allen-terri-ng!@#att.net> wrote in message
      news:ayZsb.5916 2$Ec1.3577677@b gtnsc05-news.ops.worldn et.att.net...[color=blue]
      > Hi all,
      >
      > Will my pointer survive this conversion?
      >
      > MyType_tag MyType;
      > MyType_tag* pMyPntr = &MyType;
      > int iUglyTempStorag e = (int)pMyPntr;
      > MyType_tag* pMyOtherPntr = (MyType_tag*)iU glyTempStorage;
      > --
      >
      > Best wishes,
      > Allen
      >[/color]

      Not necessarily, though your conversion will (more or less) succeed as
      you're forcing the compiler to obey your wish. Generally you might get away
      with this on some platforms but it's not guaranteed to work and it's
      certainly not portable.

      Chris


      Comment

      • Unforgiven

        #4
        Re: will the pointer survive?

        Allen wrote:[color=blue]
        > Hi all,
        >
        > Will my pointer survive this conversion?
        >
        > MyType_tag MyType;
        > MyType_tag* pMyPntr = &MyType;
        > int iUglyTempStorag e = (int)pMyPntr;
        > MyType_tag* pMyOtherPntr = (MyType_tag*)iU glyTempStorage;[/color]

        Only if you're using a platform where an int is the same size as a pointer.

        --
        Unforgiven

        "You can't rightfully be a scientist if you mind people thinking
        you're a fool."


        Comment

        • EventHelix.com

          #5
          Re: will the pointer survive?

          > int iUglyTempStorag e = (int)pMyPntr;

          The above line is the source of trouble. Here it assumes that the
          pointer and the integer are of the same size. This may not be
          true on several platforms.

          Sandeep
          --
          Sequence diagram based systems engineering and architecture design tool. Built in support for alternative scenarios and multi-tier architectures.

          EventStudio 2.0 - Generate Sequence Diagrams and Use Case Diagrams in PDF

          Comment

          • Nils Petter Vaskinn

            #6
            Re: will the pointer survive?

            On Fri, 14 Nov 2003 05:15:50 +0000, Allen wrote:
            [color=blue]
            > Will my pointer survive this conversion?
            >
            > MyType_tag MyType;
            > MyType_tag* pMyPntr = &MyType;
            > int iUglyTempStorag e = (int)pMyPntr;
            > MyType_tag* pMyOtherPntr = (MyType_tag*)iU glyTempStorage;[/color]

            Will this work?


            MyType_tag MyType;
            MyType_tag* pMyPntr = &MyType;
            char *ugly[sizof(MyType_ta g*)];
            memcpy(ugly,&pM yPtr,sizeof(MyT ype_tag*));
            MyType_tag* pMyOtherPntr;
            memcpy(&pMyOthe rPntr,ugly,size of(MyType_tag*) );


            That made me feel really bad :( I think I have to go take a shower and
            wash my mouth out with soap ;)

            --
            NPV

            "the large print giveth, and the small print taketh away"
            Tom Waits - Step right up

            Comment

            • Allen

              #7
              Re: will the pointer survive?

              "EventHelix.com " <eventhelix@hot mail.com> wrote in message
              news:566e2bfb.0 311140428.7c5fe 20e@posting.goo gle.com...[color=blue][color=green]
              > > int iUglyTempStorag e = (int)pMyPntr;[/color]
              >
              > The above line is the source of trouble. Here it assumes that the
              > pointer and the integer are of the same size. This may not be
              > true on several platforms.[/color]

              But this should only be a compile-time issue, right? Once it's
              compiled, the binary image should no longer be dependant on the sizes of
              types on various platforms should it?


              Comment

              • Jack Klein

                #8
                Re: will the pointer survive?

                On Fri, 14 Nov 2003 21:22:06 GMT, "Allen" <allen-terri-ng!@#att.net>
                wrote in comp.lang.c++:
                [color=blue]
                > "EventHelix.com " <eventhelix@hot mail.com> wrote in message
                > news:566e2bfb.0 311140428.7c5fe 20e@posting.goo gle.com...[color=green][color=darkred]
                > > > int iUglyTempStorag e = (int)pMyPntr;[/color]
                > >
                > > The above line is the source of trouble. Here it assumes that the
                > > pointer and the integer are of the same size. This may not be
                > > true on several platforms.[/color]
                >
                > But this should only be a compile-time issue, right? Once it's
                > compiled, the binary image should no longer be dependant on the sizes of
                > types on various platforms should it?[/color]

                Once it's compiled, the binary image will not be compatible with
                various platforms that have different sizes for types.

                --
                Jack Klein
                Home: http://JK-Technology.Com
                FAQs for
                comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
                comp.lang.c++ http://www.parashift.com/c++-faq-lite/
                alt.comp.lang.l earn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq

                Comment

                • Rolf Magnus

                  #9
                  Re: will the pointer survive?

                  Allen wrote:
                  [color=blue]
                  > "EventHelix.com " <eventhelix@hot mail.com> wrote in message
                  > news:566e2bfb.0 311140428.7c5fe 20e@posting.goo gle.com...[color=green][color=darkred]
                  >> > int iUglyTempStorag e = (int)pMyPntr;[/color]
                  >>
                  >> The above line is the source of trouble. Here it assumes that the
                  >> pointer and the integer are of the same size. This may not be
                  >> true on several platforms.[/color]
                  >
                  > But this should only be a compile-time issue, right? Once it's
                  > compiled, the binary image should no longer be dependant on the sizes
                  > of types on various platforms should it?[/color]

                  Well, I don't know what you mean, but if you compile it for a platform
                  on which a pointer is bigger than an int (like e.g. virtually all 64bit
                  platforms), it will never work, even if there are other platforms on
                  which int and pointers are the same size.

                  Comment

                  Working...