+3

# Memory Address / Pointer Size Confusion

11/5/2020 6:00:22 PM

Zerokles

+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.

+5

Addresses are always for bytes and not for bits. That is why for an int array the addresses increase in steps of 4

+4

Zerokles check this code I made once to answer a related question https://code.sololearn.com/cg0dY8X4JP5u/?ref=app

+2

Right. I was thinking the address itself "increments by 1 bit", but they are technically 1 byte apart.

+2

Here is a simple piece of code that shows the size of primitive types and pointers. https://code.sololearn.com/cBss8k5523i1/#cpp

+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.

+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

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?

0

Damn I just made myself look stupid lol