+ 3

Memory Address / Pointer Size Confusion

I'm confused about the sizes of memory addresses and pointers. Suppose we have a 64-bit computer. When I display the address of any variable, we see a 16-character long hexadecimal address. A hexadecimal character can be represented by 4 bits, so 16 * 4 bits = 64 bits. For example, let's say we have two adjacent addresses: 0x00000000123ABCDE and 0x00000000123ABCDF. Their difference is 1 or 1 bit (e.g. 0xE = 0b1110, 0xF = 0b1111). Fair enough. Now if I didn't already make a mistake in that last paragraph, this is probably where I got things mixed up. We know integers in C are 4 bytes large (sizeof(int)). If we have an array of two integers, and we display their addresses, we can see that the difference of their addresses is 4. That implies that when we increment an address, we move it by 1 byte or 8 bits. However, that contradicts the understanding that addresses are 1 bit apart. I'm so confused.

5th Nov 2020, 6:00 PM
Zerokles
Zerokles - avatar
9 Respostas
+ 8
NotAPythonNinja "Yes, if you increment an address by one, you increment by one byte, not one bit." The address doesn't increment by one byte but by sizeof( a_given_type ) bytes and sizeof( int ) is not always 4 bytes. also the size of a pointer doesn't depend on the type pointed. It is equal for every type pointed.
5th Nov 2020, 6:45 PM
Davide
Davide - avatar
+ 5
Addresses are always for bytes and not for bits. That is why for an int array the addresses increase in steps of 4
5th Nov 2020, 6:06 PM
Benjamin Jürgens
Benjamin Jürgens - avatar
+ 4
Zerokles check this code I made once to answer a related question https://code.sololearn.com/cg0dY8X4JP5u/?ref=app
5th Nov 2020, 6:39 PM
Davide
Davide - avatar
+ 2
Right. I was thinking the address itself "increments by 1 bit", but they are technically 1 byte apart.
5th Nov 2020, 6:37 PM
Zerokles
Zerokles - avatar
+ 1
Maybe I shouldn't have put pointers in the title since I didn't even talk about it. I just came across it because I found it counter-intuitive how pointers are supposed to be 8 bytes, but there are 16 characters in the addresses they store. I realized that they're the same since each character is stored in 4 bits or half a byte anyway.
5th Nov 2020, 7:12 PM
Zerokles
Zerokles - avatar
+ 1
1. When you have a C pointer and increment it, it is incremented by the size of the object it is pointing to. If you have a pointer to a struct, incrementing it will add the size of the structure to the pointer. 2. The fact that you have a 64 bit machine does not imply your ints are 64 bits. C only implies: sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) The actual size depends on your machine, your compiler, and your memory model. The memory model indicates the size of your ints, longs, and the pointer width. Using 32 bit pointers on a 64 bit machine is possible in most cpu architectures since they grew from earlier 32 bit architectures. For small programs that doesn't need more than 4GB, it can be better since 31 bit pointers requires shorter instructions, takes less memory, and execute slightly faster. In the old 16 bit days we had 5-6 memory models: https://en.m.wikipedia.org/wiki/Intel_Memory_Model Today, for 32/64 bit, we have these: http://nickdesaulniers.github.io/blog/2016/05/30/da
6th Nov 2020, 10:15 AM
Udi Finkelstein
Udi Finkelstein - avatar
0
Benjamin Jürgens NotAPythonNinja So what it actually means is each address can hold a byte of information, and what the 64-bit is referring to is there are 2^64 "number of memory addresses" available?
5th Nov 2020, 6:17 PM
Zerokles
Zerokles - avatar
0
Damn I just made myself look stupid lol
5th Nov 2020, 6:49 PM
Zerokles
Zerokles - avatar