Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

eighty

Reading from random locations in memory

This topic is 5394 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Why does reading from random locations in memory crash your program? I realise it''s something you''re not supposed to do and that it''s probably wanted behavior. Surely there must be ones and zeroes whereever you look for them. Is it by design or does the program just crash because something went wrong? If it''s the former, is it for security (so you can''t read data from other programs) or just to make sure other programs aren''t messed up? By "reading from random locations in memory" I mean doing something silly like: std::cout << *(char*)rand();

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
you''re not accessing memory directly even if you think you are. you''re accessing virtual memory. the actual physical memory may be cache, sdram or on a disk, even more depending on os and hardware. if you access virtual memory that hasn''t been allocated by the os, the os can''t know what you mean, because that part of virtual address space has no physical counterpart attached to it. so it just crashes the program (it''s thus easier to detect such crash errors than if it did something weird by default like returning 0)

Share this post


Link to post
Share on other sites
Also, most processors can protect memory on hardware. This essentially explains so-called "Protected-mode" systems.

-Nik

Share this post


Link to post
Share on other sites
quote:
Original post by DrPizza
quote:
Surely there must be ones and zeroes whereever you look for them

Nope.



Binary = Ones, Zeroes and LIONS! GRRR!

Actually, there are bits there if its a valid memory address, but you can''t get to them (sometimes). Sometimes you can, though! On my first tile-based game, I forgot to set a wall flag and was able to walk to into random memory locations where the bytes were represented as one of the 256 tiles in memory. It got pretty interesting and I was able to walk for a REALLY long time (I was curious at the time). Eventually I just quit and went to go fix the bug.

Share this post


Link to post
Share on other sites
quote:
Original post by eighty
Why does reading from random locations in memory crash your program?
Because you''re not reading from memory; you''re asking the OS to do it for you. And since the OS knows who allocated what and who has access to what, you''ve just caused an access violation, which is grounds for program termination.

If you really want to read from arbitrary locations, look into Windows Assembly language and the Windows Device Driver SDK. Or just run DOS. Just remember (in DOS) not to touch the high 1MB in which the OS (command interpreter, basically) resides unless you really mean to.

Share this post


Link to post
Share on other sites
On most systems, you should only be able to access random memory locations if they belong to your process. It largely is for both stability and security (more the former, I suppose) that you cannot read and write memory from other processes, at least not without getting special permission from the OS by using whatever functions it supplies.

In Rick Scott''s example, he was reading from his own memory. This can occur sometimes when you accidentally do some incorrect addition or subtraction on a valid pointer, or index an array using an out-of-bounds index. In that case, the new pointer is still in roughly the same area, which is owned by your process. However, completely random locations are almost always sure to fail.

Share this post


Link to post
Share on other sites
Thanks for the replies.

Yes, I noticed that if I continued walking forwards on the heap past my allocated memory, that I could go 31k until it crashed. On the stack I could walk for about 500 bytes. (So I guess I initially have 512B free memory to play around with on the stack and 32k on the heap?)

I also noticed that the entire envpath thinger was at the beginning of the heap memory (I allocated one char and then kept reading bytes ahead from there). Why''s that? Or was that just a silly coincidence?

Is it Protected Mode that keeps me from reading memory from other applications? So generally, I can''t read other programs'' memory with C++ alone (if I want to stay in protected mode, that is)?

Are you getting a small portion of memory from previously run programs when you read from your own memory or should it essentially be the same data every time you start the program?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You aren''t accessing *real* memory, you''re addressing what''s essentially a phony address space. So address 57 to you isn''t really address 57 on the memory chip. It''s address 57 specifically for your process. If another process looks as address 57 then it is an entirely different location.

Share this post


Link to post
Share on other sites
Bit of a tangent here, but for Linux, I read it''s possible to tell the OS at boot time to only access a portion of actual physical memory, and that programs may directly access the unreserved physical memory via pointers.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!