25

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?

8
  • 6
    "running at the same time" goes far more deep than it sounds. Commented Dec 21, 2013 at 7:14
  • 4
    Pointers in different running programs cannot be equal, because there is no way to apply the == equality operator to them. Commented Dec 21, 2013 at 7: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). Commented Dec 21, 2013 at 8:18
  • 13
    I 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"... Commented Dec 21, 2013 at 12:07
  • 11
    Yes, quite common, in particular when both pointers are NULL. Commented Dec 21, 2013 at 12:48

6 Answers 6

49

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.

Sign up to request clarification or add additional context in comments.

Comments

26

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).

1 Comment

+1 for Advanced Linux Programming.
10

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.

2 Comments

Now I want to make a posix-compliant OS which prints just that when you try to format a pointer.
@KarlDamgaardAsmussen Agreed! I'd love that! :D
6

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.

1 Comment

+1 for mentioning that C doesn't specify anything about this.
3

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.

Comments

0

If you want to do this , you can use share memory between two Process.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.