What you've observed is completely correct. Consider what would happen if `a' had a
new address in either the pointer or child - then any previously obtained address of `a' would become invalid, and you simply cannot have that happening.
The reason you see this behaviour is because of
virtual memory. Basically, when you request a memory address from an application program, you're actually requesting a
virtual address, which is converted to a
physical address by the kernel (well, actually by the CPU under direction from the kernel) which is the actual byte of RAM used. This allows two processes to maintain addresses over things like a fork() call and yet both have different copies of the variable.
See
the Wikipedia article on virtual memory.
Quote:
|
In order to make sure my output doesn't depend on systems which implement copy on write ( i.e. till some write modifications are made, both parent and child use same copy even after fork() )
|
Why do you think this matters? It shouldn't (and no offence, but if you don't know about virtual memory then you're probably not writing anything where it does matter...).