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

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

    #16
    Re: pointer to string and then back to pointer on a 64 bit machine.

    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

    Comment

    • Barry Schwarz

      #17
      Re: pointer to string and then back to pointer on a 64 bit machine.

      On Thu, 27 Sep 2007 15:32:05 -0000, let_the_best_ma n_win
      <sgiitnewid@gma il.comwrote:
      >On Sep 27, 7:42 pm, CBFalconer <cbfalco...@yah oo.comwrote:
      >let_the_best_m an_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

      Comment

      • Charlie Gordon

        #18
        Re: pointer to string and then back to pointer on a 64 bit machine.

        "Martin Ambuhl" <mambuhl@earthl ink.neta écrit dans le message de news:
        5m1ipfFb4a26U1@ mid.individual. net...
        let_the_best_ma n_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_FAILU RE);
        }
        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.


        Comment

        Working...