0
// intialize a char variable, print its address and the next address
char charvar = '\0';
printf("address of charvar = %p\n", (void *)(&charvar));
printf("address of charvar - 1 = %p\n", (void *)(&charvar - 1));
printf("address of charvar + 1 = %p\n", (void *)(&charvar + 1));

// intialize an int variable, print its address and the next address
int intvar = 1;
printf("address of intvar = %p\n", (void *)(&intvar));
printf("address of intvar - 1 = %p\n", (void *)(&intvar - 1));
printf("address of intvar + 1 = %p\n", (void *)(&intvar + 1));

This is a code i found online and here is the concerned output

address of charvar = 0x7fff9575c05f
address of charvar - 1 = 0x7fff9575c05e
address of charvar + 1 = 0x7fff9575c060
address of intvar = 0x7fff9575c058
address of intvar - 1 = 0x7fff9575c054
address of intvar + 1 = 0x7fff9575c05c

My doubt is why the memory address in a computer is stored in a hexadecimal format? We know the size of one char is 8bits or 1 byte, what does 1 byte mean in memory that is the address of the start bit of charvar is 0x7fff9575c05f shouldn't the address of the char+1 be the 0x7fff9575c05f + 8bits be 0x7fff9575c067, but it seems that one memory location in the computer is organised in terms of 8bits or 1 byte. Am i correct?If so why?

4
  • 1
    "why the memory address in a computer is stored in a hexadecimal format?" You're printing it in hexadecimal notation by using the %p format specifier - that's all. How the value is stored is a completely different matter.
    – Michael
    Commented Jan 4, 2015 at 11:51
  • So what does 0x7fff9575c05f refer to ?
    – Varun
    Commented Jan 4, 2015 at 11:57
  • An address, printed in hexadecimal notation.
    – Michael
    Commented Jan 4, 2015 at 11:58
  • Why is the question rated -1 ?
    – Varun
    Commented Jan 4, 2015 at 13:53

3 Answers 3

2

The memory is organized in terms of bytes, and pointers point to a specific byte, not to a single bit. The reason is probably that early computers had 8-bit registers/... and usually whole bytes were processed at once. Since the computer was operating on whole bytes, addressing bytes instead of single bits made more sense. It also saves address space, allowing for more memory to be addressed with the same pointer size.

Also the memory addresses are not really stored in hexadecimal format, they are just formatted that way when printed out. Internally in memory they are binary numbers just like all the other numbers a computer works with.

2
  • So internally the location with value ' 101111111...' refers to a segment of 8 bits/1 byte . So basically you can at max control memory of one complete byte.The hexadecimal conversion is just base conversion of binary value
    – Varun
    Commented Jan 4, 2015 at 12:06
  • Modern computers might even really use bigger segments internally. So the computer might for example really always work on 4 byte blocks internally. But the addresses stay byte based for compatibility, and the processor or compiler will appropriately handle access to single bytes.
    – sth
    Commented Jan 4, 2015 at 12:23
1

The smallest part of memory you can easily access is a byte, so there would be no use making an address for every bit.

0

Memory is addressed in bytes rather than bits, you have to accept this as a matter of fact. Once you get a value of a certain byte in a memory you can work with its bits using logical (bitwise) functions or operators, such as &, |, ^, ~ etc. See http://www.cprogramming.com/tutorial/bitwise_operators.html

Moreover, the address is not stored in hexadecimal format, the hexadecimal format is only the format of the number printed to the output. If you used other formatter %d rather than %p in your printf call you will get decadic format. See http://www.cplusplus.com/reference/cstdio/printf/

3
  • 1
    If you use %d instead of %p, you don't get decadic format; you get undefined behaviour. Please don't encourage undefined behaviour.
    – fuz
    Commented Jan 4, 2015 at 12:24
  • 1
    FUZxxl - you are right, thanks for correcting my mistake. I have not been using printf quite a while. :) What would be needed to get the decadic format is to convert the pointer to unsigned integral of proper length and using %lu format specifier. Commented Jan 4, 2015 at 13:03
  • If you use at least C99, you could use printf("%ju", (uintmax_t)ptr). uintmax_t is the largest integer type and is thus large enough to contain the numeric representation of every possible pointer.
    – fuz
    Commented Jan 4, 2015 at 13:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.