-1

I thought addresses are 4 bytes big, why is this address 6 bytes?

Im compiling with gcc on a virtual machine with Ubuntu 20.04 and I got an x64 processor if this matters.

#include <stdio.h>

int main() {

        char char_array[3] = {'a', 'b', 'c'};
        printf("Address of char_array: %p\n", char_array);
}
$ gcc -g test.c
$ ./a.out
Address of char_array: 0x7ffc0ff83645
tonik
  • 465
  • 1
  • 4
  • 15
  • 2
    Are addresses 4 bytes long on a 64-bit computer? – Fiddling Bits Jun 10 '20 at 20:00
  • 3
    4 bytes is only 32 bits. You're in the 64 bit world now! – Fred Larson Jun 10 '20 at 20:00
  • 1
    The address is eight bytes (because it's a 64 bit computer/program), but the high two bytes are zeroes, that's all. – ShadowRanger Jun 10 '20 at 20:03
  • 2
    6 bytes (apparently) due to leading 0 suppression. – Weather Vane Jun 10 '20 at 20:03
  • 1
    The actual address bus [is 48 bits](https://stackoverflow.com/q/6716946/10077), so I don't think you'll ever see anything in those two high bytes. – Fred Larson Jun 10 '20 at 20:07
  • Thank you guys! Sorry for this trivial question – tonik Jun 10 '20 at 20:11
  • 2
    @FredLarson: Hah - echos of the Motorola 68000, which had a 24-bit address bus. Enterprising Macintosh programmers would often stuff data into those 8 unused bits to make the most of that precious 128KB of RAM. Worked great until the 68020 came out, which had a 32-bit address bus and all that code had to be rewritten. – John Bode Jun 10 '20 at 20:29
  • @FredLarson: What does the physical address on the bus have to do with the virtual address in a process? – Eric Postpischil Jun 10 '20 at 20:35
  • If you want to know how big a pointer is on your computer, just look at `sizeof(void *)`. – Tom Karzes Jun 10 '20 at 20:35
  • @EricPostpischil: Well, that's valid. But it does appear that the virtual address space isn't necessarily the full 64 bits either, at least according to this comment on what I linked: "A number of modern x86_64 CPUs support a 48-bit physical address space and a 52-bit virtual address space." – Fred Larson Jun 10 '20 at 20:40
  • ... or maybe that comment [got it backwards](https://superuser.com/q/655121/37572). – Fred Larson Jun 10 '20 at 20:50
  • @FredLarson: indeed, current x86-64 hardware has 48-bit virtual addresses. [Why in 64bit the virtual address are 4 bits short (48bit long) compared with the physical address (52 bit long)?](https://stackoverflow.com/q/46509152) on SO is the same answer. Linux puts the user-space stack at the top of the lower canonical range of virtual addresses (47 bit), reserving the upper half for kernel virtual addresses. [Address canonical form and pointer arithmetic](https://stackoverflow.com/q/38977755) – Peter Cordes Jun 17 '20 at 14:15
  • @JohnBode: AMD learned from past problems and has the hardware check that addresses are correctly sign-extended 48-bit. If you want to use the upper 16 bits (or upper 8 with PML5) for other data, you have to redo sign extension before using it as a pointer; the HW won't ignore it. (e.g. for user-space, but ANDing to zero-extend). So backwards compat for such programs will just involve having future OSes limit which virtual addresses they allocate for processes with a certain "personality flag" set. [Address canonical form and pointer arithmetic](https://stackoverflow.com/q/38977755) – Peter Cordes Jun 17 '20 at 14:18

3 Answers3

3

First of all C pointers are oftentimes either 4-byte (32-bit) or 8-byte (64-bit) but this is not guaranteed and should not be assumed.

Secondly you can’t count the digits in a hexadecimal memory address and assume that the number of digits divided by two is the number of bytes the system uses for a pointer, because leading zeroes are not being printed. For instance if your memory address was 0x00000020 and you passed it to printf() with the %p specifier, you would get an output of 0x20.

If you want to know the byte width of a char* in your system you should try printf(“%zd\n”,sizeof(char*));

Willis Hershey
  • 1,520
  • 4
  • 22
0

The virtual address space on x64 is 48 bits (6 bytes) long.

https://en.wikipedia.org/wiki/64-bit_computing

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 27 '22 at 05:31
0

%8p to show all 8 bytes of address. also

fprintf(stdout, "%d  %d", sizeof(char_array), sizeof(&(char_array[0])));

The second use of sizeof() depends on your compiler...

Sys101
  • 11
  • 2
  • Note that you should be using the `%zu` format specifier for `size_t` types (such as returned by the `sizeof` operator) and `%08p` to display leading zeros in a pointer (`%8p` will pad the length of the output to 8 characters by using spaces). – Adrian Mole Aug 26 '22 at 17:05