Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: Can I get out of the bounds of my app's memory with a pointer?

If I have some stupid code like this:

int nBlah = 123;
int* pnBlah = &nBlah;
pnBlah += 80000;
*pnBlah = 65;

Can I change another app's memory?

You have explained me this is evil, I know. But I was just interested.
And this isn't something to simply try. I don't know what would happen.

Thanks

like image 300
Martijn Courteaux Avatar asked Jun 04 '10 16:06

Martijn Courteaux


People also ask

What memory regions do pointers point to in C?

Pointers can point to any addressable memory. This means they cannot point to registers. The type of memory is anything that is memory-mapped, so this could be RAM or ROM for example, or even a file etc.

How pointer is stored in memory?

Pointers are used to store and manage the addresses of dynamically allocated blocks of memory. Such blocks are used to store data objects or arrays of objects. Most structured and object-oriented languages provide an area of memory, called the heap or free store, from which objects are dynamically allocated.

Why pointer is removed?

1)Pointers lead to confusion for a programmer. 2)Pointers may crash a program easily, for example, when we add two pointers, the program crashers immediately. 3)Pointers break security. Using pointers, harmful programs like Virus and other hacking programs can be developed.


2 Answers

In C++ terms, this is undefined behavior. What will actually happen depends on many factors, but most importantly it depends on the operating system (OS) you are using. On modern memory-managed OS's, your application will be terminated with a "segmentation fault" (the actual term is OS-dependent) for attempting to access memory outside of your process address space. Some OS's however don't have this protection, and you can willy-nilly poke and destroy things that belong to other programs. This is also usually the case if your code is inside kernel space, e.g. in a device driver.

like image 169
Brian Neal Avatar answered Oct 15 '22 06:10

Brian Neal


Nope, it's not that simple. :)

Modern operating systems use virtual memory.

Every process is provided with a full virtual address space.

Every process is given its own "view" of all addresses (from 0x00000000 to 0xffffffff on a 32-bit system). Processes A and B can both write to the same address, without affecting each others, because they're not accessing physical memory addresses, but virtual addresses. When a process tries to access a virtual address, the OS translates that into some other physical address to avoid collisions.

Essentially, the OS keeps track of a table of allocate memory pages for every process. It tracks which address ranges have been allocated to a process, and which physical addresses they're mapped to. If a process tries to access an address not allocated to it, you get an access violation/segmentation fault. And if you try to access an address that is allocated to your process, you get your own data. So there is no way to read other processes data just by typing in the "wrong" address.

like image 27
jalf Avatar answered Oct 15 '22 04:10

jalf