Reputation: 531
This is the code I wrote to overwrite the RAM data and eventually leading in crashing the OS.
#include<stdio.h>
#include<conio.h>
int main(){
int i=10;
int *j;
j=&i;
int m=0;
while(true){
*(j+m)=m*m; //next location of i
printf("New Value is. %d \n",(m));
m++;
}
printf("Complete");
getch();
return 0;
}
But the only after m is 46 my program is crashing(the value of m can be different from compiler to compiler). Why this program is behaving in such manner? Is it because our OS provides some fixed space of memory to load & run a program and overreaching the memory limit would cause crashing the program?
Upvotes: 0
Views: 270
Reputation: 3825
Understand one thing first... Accessing a block of memory that was not initially defined leads to undefined behavior according to the rules of C language!
Now undefined behavior is seriously not undefined. In my case when I run the program, it crashes only after m=1.
There is no logic behind defining the sense of undefined. Undefined behavior may not only depend upon what causes it but several other issues.
Upvotes: 2
Reputation: 27904
You are overwriting the stack, destroying data from local variables and return from procedure pointers from your current function through all other functions called before it, until you reach a part of memory that is read-only because it contains the actual code of your program, and this part is protected. When you try to overwrite that too, the system will prevent it and the application will crash with a "Memory cannot be written" error of something like that.
Not sure about Linux, but in Windows you can disprotect this memory with VirtualProtect()
. But doing that will only make it crash a little down the road as you overwrite the loop code with garbage and die in a Segmentation Fault error.
But still, you won't have access to the actual RAM of your computer, you can't even know the actual address in the real RAM you are in. When you are a process under a operational system, you are in the realm of virtual memory, where the OS will guarantee you cannot interfer directly with the system's or other processes' memory, not without going through the channels the system offers you anyway.
Upvotes: 2
Reputation: 36431
Besides the fact that you program contains an undefined behavior because you tried to access memory you didn't allocate in some way, what you observe is some protection ensured by OS your program is running on.
It is very common for OS to manage space allocated to running programs by chunks of space. Such a chunk is usually called a page. A page is the finer grain of memory an OS can manage on behalf the process. So even if accessing the next memory address of variable i
is logically false (undefined behavior), the generated code for the machine probably obviously try to get the memory content. At that time OS/CPU/MMU only verify if that address is in a page of your process. So the crash appears only when you access a page that is not in the space of your process.
If you want more details, read about virtual memory, page fault...
Upvotes: 1
Reputation: 134356
The problem is, after the very first iteration, *(j+m)
is invalid memory access, which causes undefined behavior. Then, nothing, absolutely nothing is guaranteed.
In your case, it just happens that until index 46, the memory location is accessible from the process (i.e, the location belongs to the process's virtual address space, though that does not mean you are allowed to access, you may be overwriting some other data) and past that index, the memory location is not accessible from your process, so the access violation happens, causing the segfault.
Upvotes: 2