LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-17-2011, 08:46 AM   #1
alephan
Member
 
Registered: Dec 2010
Location: Bucharest
Distribution: ubuntu
Posts: 45

Rep: Reputation: 0
Question 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.
Old 02-17-2011, 08:52 AM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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.
 
Old 02-17-2011, 09:02 AM   #3
alephan
Member
 
Registered: Dec 2010
Location: Bucharest
Distribution: ubuntu
Posts: 45

Original Poster
Rep: Reputation: 0
As you can see above i run malloc and the addresses are not unique.
 
Old 02-17-2011, 10:58 AM   #4
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -
Quote:
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!

But should you be *alarmed* if they're identical?

No. Relax Be Happy

'Hope that helps .. PSM

Last edited by paulsm4; 02-17-2011 at 11:02 AM.
 
2 members found this post helpful.
Old 02-17-2011, 11:56 AM   #5
z1p
Member
 
Registered: Jan 2011
Location: the right coast of the US
Distribution: Ubuntu 10.04
Posts: 80

Rep: Reputation: 23
Quote:
Originally Posted by alephan View Post
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.
 
Old 02-17-2011, 12:15 PM   #6
alephan
Member
 
Registered: Dec 2010
Location: Bucharest
Distribution: ubuntu
Posts: 45

Original Poster
Rep: Reputation: 0
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?

Last edited by alephan; 02-17-2011 at 04:10 PM.
 
Old 02-17-2011, 03:16 PM   #7
SigTerm
Member
 
Registered: Dec 2009
Distribution: Slackware 12.2
Posts: 379

Rep: Reputation: 234Reputation: 234Reputation: 234
Quote:
Originally Posted by alephan View Post
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).
....
 
Old 02-20-2011, 12:32 AM   #8
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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.
 
Old 05-03-2011, 04:41 AM   #9
alephan
Member
 
Registered: Dec 2010
Location: Bucharest
Distribution: ubuntu
Posts: 45

Original Poster
Rep: Reputation: 0
Thanks for the help. This topic is clear to me now.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
OpenVPN point-to-point address question deadeyes Linux - Server 1 12-11-2007 11:09 AM
in-memory hashtable with values, which are db records pointers kpachopoulos Programming 0 11-03-2006 03:46 AM
how to use expr to print floating point values max_rsr Linux - Newbie 1 03-12-2005 10:00 PM
C: What's the point of 'pointers'?? KendersPlace Programming 6 09-05-2003 05:30 PM
point to point address assignment of ppp0 andyn Linux - Networking 0 10-11-2002 10:45 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:00 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration