Storing address of pointer (cross platform)

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

    Storing address of pointer (cross platform)

    In 32 bit pc, I can write

    e.g.

    char* p1 = "apple";
    cout<<(int)p1;


    In 64 bit pc, I need to use

    char* p1 = "apple";
    cout<<(__int64) p1;


    What are the cross platform way to so?

    Thanks.


  • =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=

    #2
    Re: Storing address of pointer (cross platform)

    On 2008-01-19 13:47, howa wrote:
    In 32 bit pc, I can write
    >
    e.g.
    >
    char* p1 = "apple";
    cout<<(int)p1;
    >
    >
    In 64 bit pc, I need to use
    >
    char* p1 = "apple";
    cout<<(__int64) p1;
    >
    >
    What are the cross platform way to so?
    If your platforms have C compilers providing <stdint.hyou can check if
    they define the typedef intptr_r or uintptr_t, they should be large
    enough for a void pointer. If you do not have those typedefs you need to
    provide them (or similar) on your own.

    --
    Erik Wikström

    Comment

    • howa

      #3
      Re: Storing address of pointer (cross platform)

      On 1月19日, 下午9時12分 , Erik Wikström <Erik-wikst...@telia. comwrote:
      On 2008-01-19 13:47, howa wrote:
      >
      In 32 bit pc, I can write
      >
      e.g.
      >
      char* p1 = "apple";
      cout<<(int)p1;
      >
      In 64 bit pc, I need to use
      >
      char* p1 = "apple";
      cout<<(__int64) p1;
      >
      What are the cross platform way to so?
      >
      If your platforms have C compilers providing <stdint.hyou can check if
      they define the typedef intptr_r or uintptr_t, they should be large
      enough for a void pointer. If you do not have those typedefs you need to
      provide them (or similar) on your own.
      >
      --
      Erik Wikström
      Thanks.

      Comment

      • =?iso-8859-1?q?Tom=E1s_=D3_h=C9ilidhe?=

        #4
        Re: Storing address of pointer (cross platform)

        howa:
        In 32 bit pc, I can write
        >
        e.g.
        >
        char* p1 = "apple";
        cout<<(int)p1;
        >
        >
        In 64 bit pc, I need to use
        >
        char* p1 = "apple";
        cout<<(__int64) p1;
        >
        >
        What are the cross platform way to so?

        I'm not sure if cout can print a pointer, but printf certainly can:

        int i;

        printf("%p",(vo id*)&i);

        --
        Tomás Ó hÉilidhe

        Comment

        • Rolf Magnus

          #5
          Re: Storing address of pointer (cross platform)

          howa wrote:
          In 32 bit pc, I can write
          >
          e.g.
          >
          char* p1 = "apple";
          cout<<(int)p1;
          >
          >
          In 64 bit pc, I need to use
          >
          char* p1 = "apple";
          cout<<(__int64) p1;
          >
          >
          What are the cross platform way to so?
          cout << static_cast<voi d*>(p1);

          Comment

          • James Kanze

            #6
            Re: Storing address of pointer (cross platform)

            On Jan 19, 1:47 pm, howa <howac...@gmail .comwrote:
            In 32 bit pc, I can write
            e.g.
            char* p1 = "apple";
            cout<<(int)p1;
            In 64 bit pc, I need to use
            char* p1 = "apple";
            cout<<(__int64) p1;
            (A more portable 64 bit type would be long long.)
            What are the cross platform way to so?
            cout << static_cast< void* >( p1 ) ;

            It's the only way guaranteed to work everywhere.

            It doesn't allow you to control the output format in any way,
            however. If you need to control the output format, something
            like:
            cout << static_cast< uintptr_t >( p1 ) ;
            will usually work (but uintptr_t is not guaranteed to be present
            on a machine where pointers are larger than 64 bits).

            If you have to work with an older compiler, which doesn't
            support the new integer types, then casting to size_t is often
            OK (although I've used platforms where it wouldn't work).

            --
            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

            Working...