0

Can someone help me with C this code..

i found this code in a memory management book and it says that the code can crash a computer running in real mode. can someone please explain this code to me and tell me what is "real mode" in a computer?? void main() { unsigned char *ptr; int i; ptr = (unsigned char *) 0x0; for(i=0; i<1024; i++) { ptr[i] = 0x0; } return; }

15th Aug 2017, 2:08 PM
Vyshak Shetty
Vyshak Shetty - avatar
4 Antworten
+ 4
If you try to run this code on plain old windows, the program will proabably just crash and that's it. When you run a program, your operating system gives you a block of memory you can use, and if you try to access a memory location that doesn't belong to you (like stuff between 0 and 1024), it says "nope", your program segfaults, and the computer continues business as usual. That's because your operating system is running in "proteced mode" which offers memory protection and lots of other stuff. If you run your code in protected mode you aren't even looking at address 0 to 1024, because by means of "paging" the addresses you use are virtualized, that is to say, some part of your operating system will translate every address you use to another address which points somewhere in memory. That info is stored in the "paging table" and the big advantage is that to the program that's running it looks like there's infinite memory, while the operating system does some smart stuff to shove less used memory to your hard drive and actively used memory into RAM to make sure every program gets the RAM it needs at any time. Now when you first start your computer, it starts in real mode. The operating system will then load some code and switch to protected mode (and that's way before the windows logo shows up). And in real mode, you have none of that. There's no memory protection and no fancy stuff like paging. I don't know the specifics but at address 0 to 1024 there's probably important data, and setting that to 0 crashes the computer.
15th Aug 2017, 2:36 PM
Schindlabua
Schindlabua - avatar
+ 3
The void main = no return just return; returns nothing you have at least two things wrong with you code
15th Aug 2017, 2:25 PM
Manual
Manual - avatar
+ 2
@Schindlabua That would be more legit, if you mentioned some of the C code. Also if you did not add "dab" to the mix.
15th Aug 2017, 2:32 PM
Manual
Manual - avatar
0
You cannot get a return value from void.
15th Aug 2017, 2:23 PM
Manual
Manual - avatar