PDA

View Full Version : memory address c++



scottish
29-05-2012, 06:01 PM
is it possible given an address that using c++ you can find the value in that address?

i know you can get pointers to get memory address of a variable, but is that possible? :P

Tomm
29-05-2012, 06:47 PM
Yes you can but why do you want to do this? If the address is invalid or the OS does not want you to access that address then you're going to get an access violation on windows or segmentation violation on linux.

scottish
29-05-2012, 07:11 PM
Can you access any memory address on linux, or only ones used by that program?

And it's for educational purposes, but i've never used C++ so before learning C++ need to know it's possible :P

Tomm
29-05-2012, 08:33 PM
If its for educational purposes then don't do it. Unless you're doing some embedded stuff then you'll find modern operating systems use virtual memory management so each process is going to have its own virtual address space. This means you can't access other process' memory directly (obvious security implications..).

In Windows you can access memory from another process by using the ReadProcessMemory and WriteProcessMemory API functions. The application must be running as an administrator for this to work (technically, you need PROCESS_VM_READ and PROCESS_VM_WRITE repetitively for the process you're trying to access)

Linux is a bit different. You can access physical memory by using /dev/mem and you'll find process memory mappings in /proc/<pid>/maps . However, depending on how the linux kernel was compiled and any other security measures in place you might find that access to /dev/mem is seriously restricted. For example you can deny userspace access (so that include userspace applications running as root) to all physical memory except peripherals.

The only reason I could think of that you would need to access memory like this is if you were writing some debugging stuff, drivers/low level stuff or implementing some shady software (viruses or hacks/cheats for games) so I'm not sure what educational need this kind of thing serves :P


Can you access any memory address on linux, or only ones used by that program?

And it's for educational purposes, but i've never used C++ so before learning C++ need to know it's possible :P

N!ck
29-05-2012, 11:17 PM
Are you talking about accessing memory that hasn't been allocated by the program you're writing? If so then what Tomm has said applies. Otherwise just use the * operator to dereference a pointer.

Want to hide these adverts? Register an account for free!