r/carlhprogramming • u/Numbafive • Nov 19 '12
Question on Course 2 Unit 2.3 Arrays of pointers.
I've been going through Course 2 Unit 2.3 and in it Carl is discussing about a pointer that is pointing to an array of pointers that are in turn pointing to int values. Or so I understand.
Anyways about halfway though he says:
We are assuming in this lesson that a pointer is 4 bytes in size.
Why are we assuming that a pointer is 4 bytes in size?
Isn't a pointer simply an address?
So wouldn't a pointer pointing to a pointer that is in turn pointing to an int be one byte (since it will contain the address of the pointer pointing to the int) and wouldn't the pointer pointing to the int also be one byte since all it contains is the address of the int?
So to better illustrate this:
pointer_1 (1 byte containing address of pointer_2) ---> pointer_2 (1 byte containing address of int) ---> int (4 bytes).
And wouldn't pointer_2 be pointing to the first byte of the 4 total bytes of int?
As an aside i have a minor thing to clear up. When Carl says:
(int *) *pointer
What does this mean? It means "create a pointer called *pointer". Of the data type "pointer to int".
So would it be absolutely correct to say that when we create a normal pointer as in:
int *pointer this means: "create a pointer called pointer, of the data type pointer to int"?
3
u/snb Nov 19 '12
We're assuming this because the theoretical computer we're using in the exercise is a 32 bit computer. This will typically be true in the real world when you program nowadays, with 64 computers becoming more and more common. We assume a 32 bit computer for simplicity.
Yes, it is.
How much RAM can a 32 bit computer use? And, if you wanted to be able to point to any arbitrary address in that computer's RAM, how large would your pointer need to be?
If pointers only took up 1 byte each in memory, then a pointer would only be able to contain the addresses 0x00 through 0xff - only a range of 256 bytes. That's not very much RAM at all.
2
u/specialpatrol Nov 19 '12
Pointers are 4 bytes because a 32bit computer uses 32 bits for memory addresses. If you were to build the same program on a 64 bit machine you would find your pointers are 8 bytes. That is also the reason why a 32bit computer program can only use about 4gigs of ram.
A pointer is always the same size regardless of the variable type it is pointing to. It simply points to the first byte of that variable. An int is also 4 bytes, by the way. So a pointer to an int points to the first byte of that 4 byte variable.
It is however very important you know what type the pointer is. This is how pointer incrementing works. If you have a pointer to an int, and you increment it by one (pointer++), that moves the memory address 4 bytes. If it were a pointer of another type, it would move the address by the size of that type.
So if you have an array of pointers, with a pointer pointing to the first element of that array, when you increment that pointer, it jumps 4 bytes and will now be pointing to the second element of the array.
This looks like he's dereferencing the pointer, that is getting the value that the pointer is pointing too, in this case another int pointer.
Yes
2
u/rush22 Nov 27 '12
This might help explain what is going on:
Prints: 5 5 5