Say,the address of a variable 'a' is 64488 and aptr is a pointer variable such that int*aptr; aptr=&a; So,64488 is stored in aptr pointer variable.What I don't understand is how can aptr store 64488 in just a single byte (8 bits) because the maximum decimal value a byte can store is 255
2 Answers
On 32 bit systems such as IDE50 memory addresses (pointers) are 32 bits as the name would suggest; likewise on 64 bit systems the memory addresses are 64 bits.
- 32 / 8 = 4
- 64 / 8 = 8
So a pointer is not 4 or 8 bits, but instead 32 or 64 bits, which is equal to 4 or 8 bytes. This leaves plenty of room for addresses, in fact a 32 bit system can store 4,294,967,295 different memory locations, meanwhile a 64 bit system can store a whole 9,223,372,036,854,775,807 different memory locations!
-
No,What I want to know is how can the memory address 64488 (for eg.) fit in the first byte of pointer variable– qwertyCommented Oct 30, 2016 at 19:23
-
What do you mean first byte of the pointer? The pointer is a block of 4 or 8 bytes, a single unit, just like an integer or long. The first byte can store 256 different values but it is expanded by the remainder of the block. Commented Oct 30, 2016 at 20:15
-
Since,the address operator only gives the address of the first byte of the pointer variable,(like for eg. if address of first byte in pointer variable is 10 ,address of second byte is 11 and so on till address of 8th byte assuming it is 64 bit OS)What address will the pointer variable will it give if the address of first byte in pointer variable is > 255.– qwertyCommented Oct 31, 2016 at 1:43
-
It looks like you think that when you get the address of a variable of any size the pointer is equal or converted from the very first byte. This is not the case; somewhere in your memory, there's a pool of reserved memory, somewhere in in that pool is a block of 4 (or 8) bytes, every value in this block is mapped to a physical byte in your memory stick. So regardless of how big or small your variable is, the pointer to it will always be 4 or 8 bytes, never 1. You can test this yourself with
printf("%lu", sizeof(pointer_to_var);
I hope I answered the right question this time :) Commented Oct 31, 2016 at 9:53 -
So, those 8 bytes(assuming 64 bit OS) of memory for pointer variable is allocated in stack memory, right?– qwertyCommented Oct 31, 2016 at 12:15
What do you think is actually only a single byte? I'm guessing that you think that since a pointer points at an address, and that every byte has an address, that only one byte is available at that address.
In fact, when memory is allocated with a malloc, an amount of memory defined in the command is allocated. If the pointer points at another var, as in your example, the system has already allocated the correct amount of memory for that var type. It would be pretty useless to only be able to use one byte! ;-)
Finally, when a pointer is created, it has a type and uses the appropriate number of bytes. In your example, int* aptr;
is a pointer to an integer type, so it always assumes that the 4 or 8 bytes, depending on architecture, stored at that address is available and used for storing an integer, not just the byte at the actual address.