When two programs are running at the same time, and you print the address to which the pointer points to, can it happen that both programs print the same value?
-
6"running at the same time" goes far more deep than it sounds. – Yaseen Dec 21 '13 at 07:14
-
4Pointers in different running programs cannot be *equal*, because there is no way to apply the `==` equality operator to them. – Keith Thompson Dec 21 '13 at 07:59
-
@KeithThompson: one could imagine a process printing with `%p` a pointer and another reading it... So you could imagine a contrived way to compare them (even if it is nonsensical). – Basile Starynkevitch Dec 21 '13 at 08:18
-
13I am a bit surprised by the high score given to this question. I don't feel that "this question shows a lot of research effort, and is useful and clear"... – Basile Starynkevitch Dec 21 '13 at 12:07
-
11Yes, quite common, in particular when both pointers are `NULL`. – MSalters Dec 21 '13 at 12:48
-
The question requires more detail for a single correct answer. If both programs are running on different computers, for instance, the answer will be different than if the programs are running on the same computer. If one is running in a VM, the answer will be different than if they are running on the same operating system. IF they are both running on the same OS, the answer will depend of whether that OS is DOS 6 or Windows 8 or Linux or RTLinux, etc. The current answers make a lot of assumptions which may make them incorrect for your needs. – Adam Davis Dec 21 '13 at 14:37
-
@BasileStarynkevitch Exactly. Here's [another "nice" one](http://stackoverflow.com/questions/10962085/lua-string-to-int#comment14311524_10962085). – Dec 21 '13 at 16:19
-
All addresses are equal, but some are more equal than others. – James Morris Dec 21 '13 at 17:32
6 Answers
Yes. The program runs in a virtual memory allocated by the OS. The amount of virtual memory is determined by the processor architecture.
The address you see refers to the virtual memory address and not to the physical RAM address.
I would add that each process running on a system gets a huge address space (2^32 on a 32-bit OS and 2^64 on a 64-bit OS) allocated to it. It's on this virtual address space that a process runs.

- 6,058
- 5
- 27
- 49
On operating systems like Linux, a running program is called a process. Each process has its own address space and uses virtual memory. So the same address 0x12345
usually refers to different memory cells in process A and in process B.
Read Advanced Linux Programming which has some chapters explaining that (from a Linux perspective). See also fork system call, and read fork(2), mmap(2), execve(2) man pages.
Other operating systems (Windows, MacOSX) also have processes running in their own individual address space using virtual memory.
Details can be quite complex (and actually, some RAM could be shared between processes....). Read about copy on write, shared memory etc...
Read also some good book about Operating Systems, e.g. Tanenbaum's book, or Operating Systems : Three Easy Pieces (freely downloadable online).

- 223,805
- 18
- 296
- 547
Your question title doesn't quite match the body. The title asks:
Can address of pointers in two program be equal?
Yes, that's possible, as others have already pointed out that there's virtual memory and all sorts of other trickery going on.
Also, a NULL
pointer constant is typically always the same in each instance of a program (honestly, I don't know of a platform where it would vary from run to run). So if in both programs, you print NULL
, it's even expected that the results will be identical.
Now in the question, you are asking about printing those pointers, which is an entirely different thing:
When two programs are running at the same time, and you print the address to which the pointer points to, can it happen that both programs print the same value?
Since this is tagged with c
, I'll answer it from a C point of view:
Yes. Assuming you meant printf("%p", (void *)thePointer)
, it's perfectly possible. The %p
conversion specifier formats the pointer in an implementation-defined manner. Also, if you are printing it as an integer after having done proper type conversion, then again, the result of the conversion is implementation-defined. So your program may always print 0xffffffff
or foobar
or why are you even curious of internals like a pointer's value
each time you attempt to print a pointer. So yes, it's possible that the two programs will have the same output.
-
Now I want to make a posix-compliant OS which prints just that when you try to format a pointer. – Kile Kasmir Asmussen Dec 21 '13 at 13:01
-
The C language does not specify the interaction between two different processes. There is no guarantee that pointers in two different programs will have any meaningful connection to each other.
If you specify the operating system, C compiler, and how the programs are executed an answer may be provided that will help you.
However this is not something the C language attempts to control, and is entirely up to the operating system, and hardware running the programs.

- 91,931
- 60
- 264
- 330
Yes, It can happen. The program runs on Virtual memory. If a process starts executing, a process address space
is created for each process. Not only 2 process, multiple process can have the same address when printed.
https://stackoverflow.com/a/18479996/1814023 will give you how a process address space will look like... And each process has a similar copy allocated by OS.

- 1
- 1