how two programs access the same address?

Nov 14, 2008 at 10:43pm
i created and run two programs with the same codes and at the same time

this is the code:

#include<conio.h>
#define i 8


int main(void){

int arr[5] ;
clrscr();
arr[i]=66;

printf("arr[i]=%d\n",arr[i]);
printf("the addrees of arr[%d] is %p\n", i, &arr[i]);

printf("enter number\n");
scanf("%d",&arr[1]);

printf("arr[1]=%d\n",arr[1]);
printf("arr[i]=%d\n",arr[i]);

printf("press any key to continue.");
getch();
return 1;

}



the two programs show that they access the same address of arr[8]
how that occur ?
thanx in advance.
Nov 14, 2008 at 11:17pm
Because both programs are running in their own virtual address space. Your program sees virtual addresses. Underneath the OS is allocating different physical memory pages for both programs.
Nov 14, 2008 at 11:42pm
how can i show the physical addresses ?
Nov 15, 2008 at 3:08am
Don't bother. It isn't worth the grief to override the OS and security protocols and hardware layers. [edit] Also, the physical address can change while your program is running, thanks to paging.[/edit]

Find a method of IPC instead.

Option 1: You might find a file mapping to be most convenient. Google "msdn CreateFileMapping" to learn more. (*Nix geeks google "man mmap".)

Option 2: Use TCP/IP protocols to talk to each other. This takes a little effort, but it isn't difficult --particularly if you are already familiar with the topic.

Option 3: Use pipes. This is a little tricky to set up right, but it pays for the effort in spades because communication becomes no more difficult than regular I/O (cin and cout, etc).

Hope this helps.
Last edited on Nov 15, 2008 at 3:10am
Nov 15, 2008 at 3:08am
You can't. The kernel abstracts that away from user programs entirely.
Nov 15, 2008 at 3:11am
You cannot easily show the physical addresses. And if you could, it would be very operating system specific (and unrelated to C++).

On some systems, like e.g. Linux, two processes could be set up to share some common (virtual) subspace. Under Linux, you could use the mmap system call, or the shmat system calls, for such sharing purposes.

But Linux don't know about <conio.h> and I know nothing about Windows.

I suggest reading the documentation about the memory related system calls provided by your operating system. This usually means several days or weeks of learning efforts.

Regards

--
Basile Starynkevitch http://starynkevitch.net/Basile/
Nov 15, 2008 at 3:29pm
I don't know but it's a little unusual to assign a value to the 9th int in a 5-int array:

1
2
3
4
5
6
7
8
#define i 8


int main(void){

int arr[5] ;
clrscr();
arr[i]=66;

Nov 15, 2008 at 6:17pm
ya thats unusual ,but c don't refuse to do that

i don't know why ?????

mainly i tried this codes for that reason , to know limit or the constrain to access values out the band of the array , but faced the previous problem .

thank you guys for your help
Nov 15, 2008 at 7:43pm
It works fine because arrays aren't limited...if you want to have automatic checks for out of bound data, either you have to implement them yourself (not recommended) or use an STL container (std::vector, std::deque, etc)
Nov 16, 2008 at 7:32pm
seymore points out a class case of a buffer-overflow. While it may appear to work, the results are now undefined and no longer predictable.

Just because you can, doesn't mean you should. I can go outside and touch the powerlines, doesn't mean I should ;)
Topic archived. No new replies allowed.