Originally Posted By: txesmi
The other point touches another doubt I had: what a pointer describes exactly. Is it a global address? or an offset into some sort of application memory block? From your sentence, it seems those blocks do exists.


Chaoscoder didn't really give you an either correct nor satisfying answer (imho), so let me try to give it a shot, thanks to some historic decisions it is a tad complicated.

For all intents and purposes, a pointer is a global address, which also happens to be an offset. The null pointer points 0 bytes into the RAM, a pointer with the value 0x1000 points 4096 bytes into the RAM. The important thing is that the smallest addressable unit is one byte, so a pointer with value 0x1 points to the second byte in memory.

Now, it would be huge security issue if every program would be in the same address space, as every program could read and write over other program memory. Historically, in DOS times, this used to be the case. But even without security concerns, one wrong pointer could take down the whole system. So, modern CPUs and operating systems have virtual address spaces. For your program it looks like its got a whole 4gb address space that it doesn't share with anyone else. A small hardware unit called MMU (memory management unit) translates your virtual address space pointers into physical addresses and does the RAM access. The operating system is in charge of handling all virtual address spaces and providing physical memory backing for memory pages.

This has some huge advantages. Security on one hand, but, shared libraries for example can be loaded once into memory and be mapped read only into all the programs virtual address spaces that uses it. The system allows for arbitrary mappings between virtual and physical pages, and that on a per virtual address space basis. And it's all transparent to your program, it never has to know what physical address your pointer resolves to, for you it just looks like one large continous address space.

Now, since we are talking about x86 here: Technically there is one more thing that can affect what a pointer resolves to: The segment registers. Intel thought it was a great idea back in a long time ago to allow for segmentation of memory. This was kind of like poor mans virtual address spaces, before they came about. Segmentation is tricky, and unnecessary, because all modern operating systems have one segment for the whole address space and call it a day (in which case there is no extra pointer translation).

The major thing to take away though is that pointers really are just 32bit integers! They hold integer values which represent addresses into memory. Wether you call it an offset or not is your thing, the analogy would be street numbers. Is house number 38 the 38th house from the beginning of the street, or is "Foo Street 38" an absolute address?


Shitlord by trade and passion. Graphics programmer at Laminar Research.
I write blog posts at feresignum.com