pointer to string and then back to pointer on a 64 bit machine.

Discussion in 'C Programming' started by let_the_best_man_win, Sep 27, 2007.

  1. How do I print a pointer address into a string buffer and then read it
    back on a 64 bit machine ?
    Compulsion is to use string (char * buffer) only.

    printing with %d does not capture the full 64-bits of the pointer.
    does %l exist in both printf and scanf for this purpose ?
     
    let_the_best_man_win, Sep 27, 2007
    #1
    1. Advertising

  2. atol does not work !
    It does not bring back the correct value from the string buffer.
    sscanf with %lu does it very well.


    #include <stdio.h>

    int main () {
    char *p = (char *)182947876880;
    char *p2;
    char buf[100];

    printf ("\n\n");
    printf ("lu = %lu\n", p);

    sprintf (buf, "%lu", p);
    printf ("buf = %s\n", buf);

    p2 = (char *)atol (buf); // THIS LINE DOES NOT WORK
    sscanf (buf, "%lu", &p2); // sscanf DOES THE EXPECTED JOB
    printf ("lu = %lu\n", p2);

    if (p==p2) {
    printf ("equal\n");
    } else {
    printf ("NOT equal\n");
    }

    printf ("\n\n");
    }
     
    let_the_best_man_win, Sep 27, 2007
    #2
    1. Advertising

  3. let_the_best_man_win

    Ian Collins Guest

    let_the_best_man_win wrote:
    > How do I print a pointer address into a string buffer and then read it
    > back on a 64 bit machine ?
    > Compulsion is to use string (char * buffer) only.
    >
    > printing with %d does not capture the full 64-bits of the pointer.
    > does %l exist in both printf and scanf for this purpose ?
    >

    That's because %d isn't for pointers, %p is.

    --
    Ian Collins.
     
    Ian Collins, Sep 27, 2007
    #3
  4. Is there a corresponding %p in sscanf ?
     
    let_the_best_man_win, Sep 27, 2007
    #4
  5. yes there is...
    I replaced %lu with %p in the above program and it worked !

    thanks for the help !
     
    let_the_best_man_win, Sep 27, 2007
    #5
  6. let_the_best_man_win

    Ian Collins Guest

    let_the_best_man_win wrote:
    > Is there a corresponding %p in sscanf ?
    >

    Corresponding to what? Please provide enough context for your message
    to make sense on its own.

    What does you book/documentation tell you about %p in sscanf?

    --
    Ian Collins.
     
    Ian Collins, Sep 27, 2007
    #6
  7. let_the_best_man_win wrote:
    > How do I print a pointer address into a string buffer and then read it
    > back on a 64 bit machine ?
    > Compulsion is to use string (char * buffer) only.
    >
    > printing with %d does not capture the full 64-bits of the pointer.
    > does %l exist in both printf and scanf for this purpose ?


    Your references to a "64 bit machine" suggest that you don't really
    understand pointers. They are to an address space for which the values
    may not correspond to any physical address on your machine.

    And "%d" is a specifier for signed ints, not for pointers. Check the
    code below for hints about how to do what you _may_ (or may not) be
    meaning to do. Your question is underspecified, as I'm sure you will
    realize on reflection.

    #include <stdio.h>
    #include <stdlib.h>

    int main(void)
    {
    char *buffer;
    size_t n;

    /* find out what the length of the string needs to be */
    n = snprintf(0, 0, "%p", (void *) &buffer);

    /* allocate the space */
    if (!(buffer = malloc(n + 1))) {
    fprintf(stderr, "could not allocate space for buffer.\n"
    "giving up ...\n");
    exit(EXIT_FAILURE);
    }

    /* put the address of buffer into buffer and show it */
    sprintf(buffer, "%p", (void *) buffer);
    printf("The buffer is at %p, and contains the string \"%s\"\n",
    (void *) buffer, buffer);

    free(buffer);
    return 0;
    }

    The buffer is at 20d98, and contains the string "20d98"
     
    Martin Ambuhl, Sep 27, 2007
    #7
  8. let_the_best_man_win

    Richard Guest

    Martin Ambuhl <> writes:

    > let_the_best_man_win wrote:
    >> How do I print a pointer address into a string buffer and then read it
    >> back on a 64 bit machine ?
    >> Compulsion is to use string (char * buffer) only.
    >>
    >> printing with %d does not capture the full 64-bits of the pointer.
    >> does %l exist in both printf and scanf for this purpose ?

    >
    > Your references to a "64 bit machine" suggest that you don't really
    > understand pointers. They are to an address space for which the


    How do you figure that out? He might have got the wrong specifier for a
    pointer but that was about it. A 64 bit machine running a program
    compiled for 64 bit will almost certainly have 64 bit pointers. Or?
     
    Richard, Sep 27, 2007
    #8
  9. let_the_best_man_win wrote:
    > atol does not work !
    > It does not bring back the correct value from the string buffer.
    > sscanf with %lu does it very well.


    atol is to convert strings to longs, which are integers.
    Pointers are not long ints, or any kind of integer.
    Using "%lu" is an error as well.
    >
    >
    > #include <stdio.h>
    >
    > int main () {
    > char *p = (char *)182947876880;
    > char *p2;
    > char buf[100];
    >
    > printf ("\n\n");
    > printf ("lu = %lu\n", p);
    >
    > sprintf (buf, "%lu", p);
    > printf ("buf = %s\n", buf);
    >
    > p2 = (char *)atol (buf); // THIS LINE DOES NOT WORK
    > sscanf (buf, "%lu", &p2); // sscanf DOES THE EXPECTED JOB
    > printf ("lu = %lu\n", p2);
    >
    > if (p==p2) {
    > printf ("equal\n");
    > } else {
    > printf ("NOT equal\n");
    > }
    >
    > printf ("\n\n");
    > }
     
    Martin Ambuhl, Sep 27, 2007
    #9
  10. let_the_best_man_win

    Lew Pitcher Guest

    On Sep 27, 7:43 am, Richard <> wrote:
    > Martin Ambuhl <> writes:
    > > let_the_best_man_win wrote:
    > >> How do I print a pointer address into a string buffer and then read it
    > >> back on a 64 bit machine ?
    > >> Compulsion is to use string (char * buffer) only.

    >
    > >> printing with %d does not capture the full 64-bits of the pointer.
    > >> does %l exist in both printf and scanf for this purpose ?

    >
    > > Your references to a "64 bit machine" suggest that you don't really
    > > understand pointers. They are to an address space for which the

    >
    > How do you figure that out? He might have got the wrong specifier for a
    > pointer but that was about it. A 64 bit machine running a program
    > compiled for 64 bit will almost certainly have 64 bit pointers. Or?


    I suppose so, but...

    1) the OP didn't make it clear that the machine from which he will
    print the pointer is a 64bit machine.

    2) Unless the pointer is being printed and the printed value read in
    the same invocation of the same program (that is, using a printed
    value as a temporary storage for a pointer), then the thing pointed to
    by the pointer is probably /not/ available to the program reading the
    pointer. Hopefully, the program reading the printed pointer value
    doesn't want the data pointed to by the pointer.
     
    Lew Pitcher, Sep 27, 2007
    #10
  11. On Thu, 27 Sep 2007 05:06:06 -0700,
    Lew Pitcher <> wrote:

    >> > let_the_best_man_win wrote:
    >> >> How do I print a pointer address into a string buffer and then read it
    >> >> back on a 64 bit machine ?


    [snip intermediate discussion]

    > 1) the OP didn't make it clear that the machine from which he will
    > print the pointer is a 64bit machine.


    He probably did intend it though. His original question is maybe a bit
    ambiguous; 'on a 64 bit machine' can, if you want, just refer to 'read
    it back', but it can also refer to the the whole previous sentence,
    which, I think, is more likely.

    Especially, since later in the OP we find:

    >> >> printing with %d does not capture the full 64-bits of the pointer


    which, to me at least, resolves the ambiguity. I fail to see why the
    poster would say this if they were printing the pointer on a machine
    with a different configuration.

    Also note that the OP did not say that he wanted to print to a file,
    socket, or other external medium which then could be read somewhere
    else, or even by another invocation of the same program. The poster
    states that they want to print to a string buffer. Generally a string
    buffer on a 32 bit machine is not available to read on a, different, 64
    bit machine without some intermediate representation.

    Martien
    --
    |
    Martien Verbruggen | Make it idiot proof and someone will make a
    | better idiot.
    |
     
    Martien verbruggen, Sep 27, 2007
    #11
  12. let_the_best_man_win

    CBFalconer Guest

    let_the_best_man_win wrote:
    >
    > How do I print a pointer address into a string buffer and then
    > read it back on a 64 bit machine ?


    Use %p. Make sure the pointer is cast to void* for printing.

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>



    --
    Posted via a free Usenet account from http://www.teranews.com
     
    CBFalconer, Sep 27, 2007
    #12
  13. On Sep 27, 7:42 pm, CBFalconer <> wrote:
    > let_the_best_man_win wrote:
    >
    > > How do I print a pointer address into a string buffer and then
    > > read it back on a 64 bit machine ?

    >
    > Use %p. Make sure the pointer is cast to void* for printing.


    Why this restriction of casting to a void* ?
    Shouldn't the size of all pointers be same ?
    (Now please do not go on to HUGE and LONG pointers, whatever they
    are !)
    (I just mean simple pointers)
     
    let_the_best_man_win, Sep 27, 2007
    #13
  14. let_the_best_man_win

    Mark Bluemel Guest

    let_the_best_man_win wrote:
    > On Sep 27, 7:42 pm, CBFalconer <> wrote:
    >> let_the_best_man_win wrote:
    >>
    >>> How do I print a pointer address into a string buffer and then
    >>> read it back on a 64 bit machine ?

    >> Use %p. Make sure the pointer is cast to void* for printing.

    >
    > Why this restriction of casting to a void* ?


    We've just discussed this very thoroughly in a thread entitled "The
    usage of %p in C". The executive summary is this

    1) Because that's what the language specifies.

    2) Why not work with the language rather than against it?

    > Shouldn't the size of all pointers be same ?


    Not necessarily. And it's not just the size that is relevant. Read the
    thread cited above.
     
    Mark Bluemel, Sep 27, 2007
    #14
  15. let_the_best_man_win

    CBFalconer Guest

    let_the_best_man_win wrote:
    >
    > On Sep 27, 7:42 pm, CBFalconer <> wrote:
    > > let_the_best_man_win wrote:
    > >
    > > > How do I print a pointer address into a string buffer and then
    > > > read it back on a 64 bit machine ?

    > >
    > > Use %p. Make sure the pointer is cast to void* for printing.

    >
    > Why this restriction of casting to a void* ?
    > Shouldn't the size of all pointers be same ?


    No. All pointers can be cast to void* and back to the same type.

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>


    --
    Posted via a free Usenet account from http://www.teranews.com
     
    CBFalconer, Sep 27, 2007
    #15
  16. let_the_best_man_win

    cr88192 Guest

    "let_the_best_man_win" <> wrote in message
    news:...
    > Is there a corresponding %p in sscanf ?
    >


    hopefully you are not storing pointers in files...

    just be aware, a pointer is (in general) only valid within a particular
    instance of an app.
    as a result, saving pointers into a file, or transferring them between
    instances, is a bad idea (in the next run, or on the other other box, or
    just in another process, the pointer can point to something completely
    different).

    within the same instance of the same app it is ok though, or (for most
    things) between different threads in the same instance.

    or such...
     
    cr88192, Sep 27, 2007
    #16
  17. let_the_best_man_win

    Tor Rustad Guest

    CBFalconer wrote:

    [...]

    > No. All pointers can be cast to void* and back to the same type.


    Nope. All pointers to objects can, but there is no such guarantee for
    function pointers.

    --
    Tor <torust [at] online [dot] no>

    "Premature tuning is the root of all evil"
     
    Tor Rustad, Sep 27, 2007
    #17
  18. let_the_best_man_win

    CBFalconer Guest

    Tor Rustad wrote:
    > CBFalconer wrote:
    >
    > [...]
    >
    >> No. All pointers can be cast to void* and back to the same type.

    >
    > Nope. All pointers to objects can, but there is no such guarantee
    > for function pointers.


    True. I know that, but never seem to remember to mention it.

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>



    --
    Posted via a free Usenet account from http://www.teranews.com
     
    CBFalconer, Sep 28, 2007
    #18
  19. On Thu, 27 Sep 2007 15:32:05 -0000, let_the_best_man_win
    <> wrote:

    >On Sep 27, 7:42 pm, CBFalconer <> wrote:
    >> let_the_best_man_win wrote:
    >>
    >> > How do I print a pointer address into a string buffer and then
    >> > read it back on a 64 bit machine ?

    >>
    >> Use %p. Make sure the pointer is cast to void* for printing.

    >
    >Why this restriction of casting to a void* ?


    Because the answer to the next question is no.

    >Shouldn't the size of all pointers be same ?


    The only requirement is that void* and char* have the same size,
    alignment, and representation. Other types of pointers are not
    constrained this way.

    >(Now please do not go on to HUGE and LONG pointers, whatever they
    >are !)


    There are no HUGE or LONG pointers in C. There are pointers to long
    but C is case sensitive. And a long* need not be the same size as any
    other pointer.


    Remove del for email
     
    Barry Schwarz, Sep 28, 2007
    #19
  20. "Martin Ambuhl" <> a écrit dans le message de news:
    ...
    > let_the_best_man_win wrote:
    >> How do I print a pointer address into a string buffer and then read it
    >> back on a 64 bit machine ?
    >> Compulsion is to use string (char * buffer) only.
    >>
    >> printing with %d does not capture the full 64-bits of the pointer.
    >> does %l exist in both printf and scanf for this purpose ?

    >
    > Your references to a "64 bit machine" suggest that you don't really
    > understand pointers. They are to an address space for which the values
    > may not correspond to any physical address on your machine.
    >
    > And "%d" is a specifier for signed ints, not for pointers. Check the code
    > below for hints about how to do what you _may_ (or may not) be meaning to
    > do. Your question is underspecified, as I'm sure you will realize on
    > reflection.
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > int main(void)
    > {
    > char *buffer;
    > size_t n;
    >
    > /* find out what the length of the string needs to be */
    > n = snprintf(0, 0, "%p", (void *) &buffer);


    You compute the length of the representation of &buffer, which is the
    address of the pointer, not the pointer itself.

    > /* allocate the space */
    > if (!(buffer = malloc(n + 1))) {
    > fprintf(stderr, "could not allocate space for buffer.\n"
    > "giving up ...\n");
    > exit(EXIT_FAILURE);
    > }


    good, now buffer has a value, but there is no guarantee it is large enough
    for the representation of its own address by %p.

    > /* put the address of buffer into buffer and show it */
    > sprintf(buffer, "%p", (void *) buffer);


    bingo! potential buffer overflow

    > printf("The buffer is at %p, and contains the string \"%s\"\n",
    > (void *) buffer, buffer);
    >
    > free(buffer);
    > return 0;
    > }
    >
    > The buffer is at 20d98, and contains the string "20d98"


    The above code is broken.

    --
    Chqrlie.
     
    Charlie Gordon, Sep 29, 2007
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Saurabh Aggrawal

    32 bit pointer to 64 bit pointer

    Saurabh Aggrawal, Dec 9, 2004, in forum: C++
    Replies:
    1
    Views:
    612
    msalters
    Dec 9, 2004
  2. Replies:
    3
    Views:
    1,847
    Timothy Bendfelt
    Jan 19, 2007
  3. Mohan
    Replies:
    3
    Views:
    375
    Robbie Hatley
    Jul 14, 2006
  4. harijay
    Replies:
    2
    Views:
    503
    Gabriel Genellina
    Feb 19, 2009
  5. Replies:
    0
    Views:
    336
Loading...

Share This Page