Tilted Forum Project - TFP - Sexuality, Philosophy and Political Discussion

Go Back   Tilted Forum Project - TFP - Sexuality, Philosophy and Political Discussion > Interests > Tilted Technology

Reply
 
LinkBack Thread Tools
Old 10-08-2006, 01:53 AM   #1 (permalink)
Crazy
 
Join Date: Apr 2003
Location: Raleigh, NC
[c] memory locations between apps

I tried to write two programs, one that would set a value to an int and display its memory address to the user, and another one that would print the value found at a user-entered address. I could never get the value I wanted. Is there a reason that a memory address could not be accessed in this way?
__________________
"Good artists copy, great artists steal." - Pablo Picasso
Digilogic is offline   Reply With Quote
Old 10-08-2006, 03:10 PM   #2 (permalink)
Lover - Protector - Teacher
 
Jinn's Avatar
 
Join Date: May 2005
Displaying the address of user-entered int should perform as you intend:

int blah;
printf("Address of %d is %d", blah, &blah);

Note the &.

However, you should not be able to do it the other way around. If C or C++ or any language for that matter allowed address control like that, you could crash the entire computer with your program. Here, let me write to protected OS stack memory. A program should never allow "illegal operations" on protected memory, for the sake of the user's computer who is using the machine.

In addition, if you're terminating the first program before starting the second, you have absolutely no idea if that memory address is still valid. What if another program currently running stored something there, temporarily? It was freed up when the first exited. And if you didnt terminate the first before starting the second, then a program has control of that memory access and the OS shouldn't let another program access it.
__________________
If you struggle with something your entire life, try harder.
Awareness without action is worthless, and failure is not an accident.
Jinn is offline   Reply With Quote
Old 10-08-2006, 03:55 PM   #3 (permalink)
It's Just A Game
 
n0nsensical's Avatar
 
Join Date: Sep 2003
Location: San Francisco
On any modern OS, with some exceptions like the embedded single-process variety, every application has its own virtual memory space and it's impossible to access any other application's space without help from the OS (well, technically you need help from the OS to access any memory at all since you never deal with hardware addresses directly, but you need extra-special help for this), which is referred to generally as interprocess communication (IPC).

On windows, you can use the APIs ReadProcessMemory and WriteProcessMemory. Here is a program that reads the first 10 bytes at memory location 0x00400000 of process with ID 666:
Code:
#include <windows.h>

int main()
{
	char c[10];
	void * p = (void*)0x00400000;
	HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, 666);
	ReadProcessMemory(hProcess, p, c, 10, NULL);
	CloseHandle(hProcess); // ok, its a little redundant
	return 0;
}
It's also possible to set up shared memory pages which both apps can access.
n0nsensical is offline   Reply With Quote
Old 10-09-2006, 08:34 AM   #4 (permalink)
Crazy
 
Join Date: Apr 2003
Location: Raleigh, NC
Thanks, I did keep the first app running but then I was confused as to why the memory address was always the same on every run. The virtual memory explanation was helpful thanks.
__________________
"Good artists copy, great artists steal." - Pablo Picasso
Digilogic is offline   Reply With Quote
Reply

Bookmarks

Tags
apps, locations, memory

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT -7. The time now is 04:17 PM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0
All text (c) 2002-2008 Tilted Forum Project
"Insignia" vBulletin 3.5 - b6gm6n - x7x7x7.com