[SOLVED] Two pointers, that have the same address point to different values???
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Two pointers, that have the same address point to different values???
I have the most strange problem ever in programming. I fork a process into a parent and a child. In every forked process i declare a pointer, malloc and define a different value for every pointer.
When i printf the value and the address guess what? They both have the SAME ADDRESS but DIFFERENT values, as assigned..
Here's the portion of my code:
pid = fork ();
switch (pid)
{
case -1:
perror("fork failed");
break;
case 0:
//Child / client
printf("Child: Allocating pointer...\n");
int *p2 = (int*)malloc(sizeof(int));
*p2 = 200;
printf("Child: The pointer holds the value of %d at address %p\n", *p2, p2);
default:
//Parent / server
printf("Parent: Allocating pointer...\n");
int *p1 = (int*)malloc(sizeof(int));
*p1 = 100;
printf("Parent: The pointer holds the value of %d at address %p\n", *p1, p1);
}
Output:
Parent: Allocating pointer...
Child: Allocating pointer...
Parent: The pointer holds the value of 100 at address 0x8c22008
Child: The pointer holds the value of 200 at address 0x8c22008
SAME ADDRESS??? Why???
As i know:
You can create a new process by calling fork. This system call duplicates the current process, creating a
new entry in the process table with many of the same attributes as the current process. The new process
is almost identical to the original, executing the same code but with its own data space, environment,
and file descriptors.
I tried all workarounds that i could think of. Please help!
Click here to see the post LQ members have rated as the most helpful post in this thread.
The memory addresses are virtual, going though 2 or 3 registers to access the physical memory. Running malloc would allocate free memory, which would be located physically in a different location.
You can create a new process by calling fork. This system call duplicates the current process, creating a
new entry in the process table with many of the same attributes as the current process.
<= Yes, absolutely correct
Quote:
The new processn is almost identical to the original, executing the same code but with its own data space, environment, and file descriptors.
<= Yes, also correct
Quote:
SAME ADDRESS??? Why???
<= You just answered your own question
Quote:
As you can see above i run malloc and the addresses are not unique.
<= So what? Why does this bother you?
Remember: "malloc()" just grabs the next free block of memory. At the moment you fork your program, the heaps (where malloc gets the data from) are IDENTICAL. The SIZE of the memory blocks the parent and child want to allocate are IDENTICAL. All virtual addresses in both of the two unique programs are, for the moment, IDENTICAL.
So why SHOULDN'T the addresses be the same?
*Must* they be the same? No.
Should you write code that depends on their being the same? Absolutely not!
As you can see above i run malloc and the addresses are not unique.
Remember each process will have its own process space (including virtual memory). The address you see is the address in the process's virtual memory not the address into physical ram.
You are correct the ADDRESSES THE SAME, but those addresses in the process's virtual memory DO NOT MAP to the SAME LOCATION in system memory.
Hope this [along with the info from PSM] helps clear up what is going on.
Ok. How can i see the real memory address of a pointer? Here is what i want to do: i want to define 2 pointers in 2 processes, send using a socket the address of the pointer defined and allocated in child to the parent and in the parent i want to try to access that memory. This should end up in sigfault. That's what i want to see. I want to test security in memory page - the fact that a process cannot access another process's memory space. How can i do this?
Ok. How can i see the real memory address of a pointer?
It looks like this article contains info you're looking for.
Quote:
Virtual Memory Areas
....
The memory areas of a process can be seen by looking in /proc/pid/maps(where pid, of course, is replaced by a process ID).
....
You can use the shm (getshm() or posix shm_open() functions). There are plenty of examples on the web. There is a "shm_overview" manpage that describes all the posix shared memory functions.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.