10

I have started learning C (so, you know.. pointers).

I have this code:

#include <stdio.h>
#include <string.h>

int main (int argc, char* argv[])
{
    char c = 'c';
    char* cptr = &c;

    printf("c = %c\n", c);
    printf("*cptr = %c\n", *cptr);  
    printf("c address = %p\n", &c);  
}

My output is:

c = c
*cptr = c
c address = 0x7fff0217096f

When I convert the hexadecimal above to decimal, I get: 140720994002157

My questions:

1) Does this decimal value represent the memory address? Isn't it too big?

2) How can I print the value of the pointer (which means the address of the c variable) as a decimal?

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
padawanTony
  • 1,348
  • 2
  • 22
  • 41

2 Answers2

11

Isn't [the address] too big?

This is a virtual address, meaning that its numerical value does not necessarily represent the sequential number of the byte in physical memory. Moreover, different processes may keep different data at the same virtual address, because each one has its individual address space.

How can I print the value of the pointer in an integer format?

Use uintptr_t to represent the pointer as an integer value, then print using PRIuPTR macro:

#include <stdio.h>
#include <inttypes.h>

int main(void) {
    char c = 'x';
    char *p = &c;
    uintptr_t x = (uintptr_t)p;
    printf("Pointer as decimal: %"PRIuPTR"\n", x);
    return 0;
}

Demo.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Thank you. +1 for using appropriate links and a demo. About this however `This is a virtual address, meaning that its numerical value does not necessarily represent the sequential number of the byte in physical memory.`, I am tempted to ask how I can print the sequential number of byte in physical memory? – padawanTony Aug 25 '16 at 10:30
  • 3
    @padawanTony You can't, mapping from virtual to physical memory in not accessible to user. – Jean-Baptiste Yunès Aug 25 '16 at 10:36
  • 1
    @padawanTony This is something hidden from your program by a combination of the operating system and the hardware on which it is running, so there is no OS-independent way to obtain a mapping of a particular address to a physical address. Physical addresses are also meaningless outside the context of the OS virtual memory manager, because pages (allocation blocks) come in no particular order. – Sergey Kalinichenko Aug 25 '16 at 10:40
  • 1
    @padawanTony You can't. You have no practical use knowing that information so the operating system doesn't provide any easy methods to access it. Also, physical memory isn't contiguous either on most modern machines. – Art Aug 25 '16 at 10:40
3

1). You should print the address as printf("c address = %p\n", &c);. Now you attempt to print the address where the pointer variable itself is stored, which probably doesn't make much sense.

That being said, it might still be a valid address, assuming 64 bit addresses.

2). You will have to safely convert it to an integer which is guaranteed to be large enough to contain a pointer address:

#include <inttypes.h>

printf("c address = %" PRIuPTR "\n", (uintptr_t)&c);
Lundin
  • 195,001
  • 40
  • 254
  • 396
  • 1) That's exactly what I'm doing 2) Yes, that produces the correct result (which is 140723639717279) but 140723639717279 are equal to around 1750 GB, right? I certainly don't have so much memory! What's going on? – padawanTony Aug 25 '16 at 10:20
  • @padawanTony as said above, it's [virtual memory](https://en.wikipedia.org/wiki/Virtual_memory), not real physical address. x86_64 implementations might use [canonical address](https://en.wikipedia.org/wiki/X86-64#Canonical_form_addresses) which comes from the 2 ends – phuclv Aug 25 '16 at 10:24
  • @Luu but why would it use virtual address if I have enough RAM? – padawanTony Aug 25 '16 at 10:26
  • 1
    @padawanTony to protect processes from being messed up by others. This is in a multithreading environment, not DOS or embedded systems where you only run one program at a time – phuclv Aug 25 '16 at 10:29
  • I see. Thank you. Do you by any chance have a nice reference to read about what you just mentioned? – padawanTony Aug 25 '16 at 10:32