i was debugging a program in two terminals by giving different input,but at on particular i saw this in one terminal
ins (ptr=0x0, key=1, upKey=0xbffff308, newnode=0xbffff30c)
and in another terminal
ins (ptr=0x0, key=1, upKey=0xbffff308, newnode=0xbffff30c)
where ins function is
ins(struct node *ptr, int key, int *upKey,struct node **newnode)
how can the same memory location be all开发者_StackOverflow社区ocated to a pointer. and i am running the same program on two different terminals...with different inputs
The memory addresses you are looking at are virtual addresses. Those addresses are then translated by the processor to physical addresses. This is the basis of all modern operating systems. Each process thinks it owns the entire address space (4GB in the case of a 32 bit machine, a lot more in the case of a 64bit machine). When a process accesses memory that has yet to be allocated to it a page fault is generated by the CPU. The OS can then handle that invalid memory access in one of several ways; one common way is a segmentation fault.
With virtual memory, every program running on a system acts as though it has the computer's entire address space to itself. However, every time a pointer is dereferenced, a special piece of hardware translates from the pointer's purported address (its virtual address) to some other location in memory where the data actually lives (the physical address). The operating system is built to manage and move the regions of memory to which virtual addresses are mapped, so if one program dereferences some address A it will map to a different location in physical memory than you would get if you dereferenced address A in a different process. In fact, any number of programs can all claim to use address A without hassle, since these virtual addresses all resolve to different physical addresses on the system.
Besides those addressed being virtual and "private" to each process don't forget that some physical memory can be indeed shared between processes. For example, when you fork()
the process it is not immediately fully copied - the code just remains shared and data pages are only copied on attempt to modify them ("copy on write"), so two programs can indeed have physical memory shared (transparently to them).
精彩评论