Memory address and Pointer

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

    Memory address and Pointer

    vc++6.0,winxp.
    I use this code to see the memory address and the real value`s
    address below:
    #include<stdio. h>
    int main()
    {
    int a=22;
    int *p;
    int i;
    p=&a;
    printf("%x\n",& a);
    printf("%p\n",* p);
    }
    the result is:
    12ff7c
    00000016
    Press any key to continue

    the pointer p is pointing to a value which is 22,and the address of
    the value is 0012ff7c.
    i want to compare the memory address with the value address to see
    wheather they are the same.
    So ,next,i use win Debug to help me.
    -d 0012:ff7c
    but the output is 00 00 00 00 00 00 00 00 00......
    what`s wrong? as far as i know,the value of a,must have it`s address
    in the memory,and when i search the address,why there is nothing left
    in it. there must be some mistake in my understanding,i really expect
    your words~~


  • Martin Ambuhl

    #2
    Re: Memory address and Pointer

    erfan wrote:
    vc++6.0,winxp.
    I use this code to see the memory address and the real value`s
    address below:
    Those terms have no meaning and your code is severely broken. Notice
    the changes below:

    #include<stdio. h>

    int main(void)
    {
    int a = 22;
    int *p;
    p = &a;
    #if 0
    /* mha: the line below is an error. &a has type 'int *', but "%x"
    expects the very different type 'unsigned int' */
    printf("%x\n", &a);
    #endif
    printf("%p\n", (void *) &a); /* mha: replacement for the above
    erroneous statement */
    #if 0
    /* mha: the line below is an error. *p has type 'int', but "%p"
    expects the very different type 'void *' */
    printf("%p\n", *p);
    #endif
    printf("%p\n", (void *) p); /* mha: replacement for the above
    erroneous statement */
    return 0;
    }

    [OP's code snipped, being essentially included above]


    >
    the pointer p is pointing to a value which is 22,and the address of
    the value is 0012ff7c.
    i want to compare the memory address with the value address to see
    wheather they are the same.
    Those terms still mean nothing.

    Comment

    • santosh

      #3
      Re: Memory address and Pointer

      erfan wrote:
      vc++6.0,winxp.
      To this group it shouldn't matter.
      I use this code to see the memory address and the real value`s
      address below:
      #include<stdio. h>
      int main()
      {
      int a=22;
      int *p;
      int i;
      p=&a;
      printf("%x\n",& a);
      The 'x' format specifier expects an unsigned int argument. The
      expression '&a' yields a value of type int *. Therefore there is a
      mismatch which leads to undefined behaviour. To print a pointer value
      use the 'p' format specifier which just for this purpose. It expects a
      corresponding argument of type void *, so you need to cast the
      appropriate argument.

      printf("&a = %p\n", (void *)&a);
      printf("%p\n",* p);
      The expression '*p' yields a value of type int. The format specifier 'p'
      expects a corresponding argument of type void *, so once again there is
      a mismatch. Do:

      printf("*p = %d\n", *p);
      }
      the result is:
      12ff7c
      00000016
      Press any key to continue
      >
      the pointer p is pointing to a value which is 22,and the address of
      the value is 0012ff7c.
      'p' holds the address of an int object, in this case 'a', which, in
      turn, contains a value that is 22 in base 10.
      i want to compare the memory address with the value address to see
      wheather they are the same.
      Which memory address? And the phrase "value address" is totally
      meaningless. Only objects have addresses, not values. For example:

      int foo = 10;
      int bar;

      bar = foo + 10;

      Now in the last statement both 'foo' and '10' resolve to a value, but
      only 'foo' is an object and hence has an address (unless it was
      declared as a register object). The expression '10' is simply a source
      code literal which has (from C's point of view) no address. Note though
      that a string literal _does_ yield an address.
      So ,next,i use win Debug to help me.
      -d 0012:ff7c
      but the output is 00 00 00 00 00 00 00 00 00......
      what`s wrong? as far as i know,the value of a,must have it`s address
      in the memory,and when i search the address,why there is nothing left
      in it. there must be some mistake in my understanding,i really expect
      your words~~
      Try the suggested changes and see. I think you are confused between
      memory addresses and memory content and the involvement of pointers in
      both. If you can be more clear with your questions, we can give you
      more helpful answers. Also be sure to see the group's de facto FAQ at:

      <http://www.c-faq.com/>

      Comment

      • Keith Thompson

        #4
        Re: Memory address and Pointer

        erfan <zhaoerfan@gmai l.comwrites:
        vc++6.0,winxp.
        I use this code to see the memory address and the real value`s
        address below:
        #include<stdio. h>
        int main()
        {
        int a=22;
        int *p;
        int i;
        p=&a;
        printf("%x\n",& a);
        printf("%p\n",* p);
        }
        the result is:
        12ff7c
        00000016
        Press any key to continue
        You're using incorrect formats in both your printf calls. "%x" prints
        an unsigned int in hexadecimal; you're passing it a pointer to int.
        "%p" expects a pointer, specifically a void*; you're passing it an
        int.
        the pointer p is pointing to a value which is 22,and the address of
        the value is 0012ff7c.
        Pointers point to objects, not to values; objects have values.

        p points to an *object* whose value is 22. The address of that object
        happens to be 0012ff7c (when displayed in hexadecimal).
        i want to compare the memory address with the value address to see
        wheather they are the same.
        I don't know what you mean by "memory address" vs. "value address".
        So ,next,i use win Debug to help me.
        -d 0012:ff7c
        but the output is 00 00 00 00 00 00 00 00 00......
        what`s wrong? as far as i know,the value of a,must have it`s address
        in the memory,and when i search the address,why there is nothing left
        in it. there must be some mistake in my understanding,i really expect
        your words~~
        I've never used win Debug, so I can't help you with that.

        Here's a corrected and expanded version of your program. I'm not sure
        what you're asking, but perhaps this will help you answer it anyway.

        #include <stdio.h>
        int main(void)
        {
        int a = 22;
        int *p = &a;
        printf("a = %d\n", a);
        printf("&a = %p\n", (void*)&a);
        printf("p = %p\n", (void*)p);
        printf("*p = %d\n", *p);
        return 0;
        }

        --
        Keith Thompson (The_Other_Keit h) <kst-u@mib.org>
        Looking for software development work in the San Diego area.
        "We must do something. This is something. Therefore, we must do this."
        -- Antony Jay and Jonathan Lynn, "Yes Minister"

        Comment

        • Mark McIntyre

          #5
          Re: Memory address and Pointer

          erfan wrote:
          vc++6.0,winxp.
          I use this code to see the memory address and the real value`s
          address below:
          #include<stdio. h>
          int main()
          {
          int a=22;
          int *p;
          int i;
          p=&a;
          printf("%x\n",& a);
          printf("%p\n",* p);
          }
          the result is:
          12ff7c
          So ,next,i use win Debug to help me.
          -d 0012:ff7c
          but the output is 00 00 00 00 00 00 00 00 00......
          ignoring the errors in printf which others have commented on, the above
          isn't likely to work anyway. In most modern OSes, programmes have their
          own memory space and another application normally can't invade that.
          Also the 'debug' app you're using seems to be a 16-bit app, and the
          address format you're supplying looks very much like a 16-bit format.
          Neither of these will be valid ways of accessing data in a 32-bit
          programme in different memory space!

          Comment

          • Barry Schwarz

            #6
            Re: Memory address and Pointer

            On Thu, 29 Nov 2007 21:55:33 -0800 (PST), erfan <zhaoerfan@gmai l.com>
            wrote:
            >vc++6.0,winx p.
            I use this code to see the memory address and the real value`s
            >address below:
            >#include<stdio .h>
            >int main()
            >{
            > int a=22;
            > int *p;
            > int i;
            > p=&a;
            > printf("%x\n",& a);
            This invokes undefined behavior. %x expects an unsigned int. If you
            want to print an address, use %p and cast the address to void*.
            > printf("%p\n",* p);
            And again. If you want to print the hex representation of an int, use
            %x and cast the value to unsigned.
            >}
            >the result is:
            >12ff7c
            >00000016
            >Press any key to continue
            Where did this line come from? It is not in your code.
            >
            >the pointer p is pointing to a value which is 22,and the address of
            >the value is 0012ff7c.
            >i want to compare the memory address with the value address to see
            >wheather they are the same.
            Then use
            printf("&a=%p, p=%p\n", (void*)&a, (void*)p);
            >So ,next,i use win Debug to help me.
            Questions about specific tools need to be asked in newsgroups that
            deal with those tools.
            >-d 0012:ff7c
            This does not look like the same address. Where did the colon come
            from?
            >but the output is 00 00 00 00 00 00 00 00 00......
            >what`s wrong? as far as i know,the value of a,must have it`s address
            >in the memory,and when i search the address,why there is nothing left
            >in it. there must be some mistake in my understanding,i really expect
            >your words~~
            >

            Remove del for email

            Comment

            Working...