Advertisement

memory readability

Started by August 12, 2000 04:30 AM
0 comments, last by RotoMuffin 24 years, 4 months ago
Anyone know of a way to tell if memory on the other end of a pointer is readable or writeable before you read or write to/from it? I can tell afterwords when I get a memory error, but by then the program has crashed. -R
For Windows you can use IsBadReadPtr and IsBadWritePtr.

You can also use exception handling. Wrap your code in a try block, do your stuff, and if the pointer is bad catch the exception. Whether or not this works in C++ using try/catch depends on your compiler and it''s settings. It does work in Windows using structured exception handling try/except/finally.

IsBadRead/WritePtr are simple to use but have limitations. They only check the pointer at that one time. If you''re using threads another thread can come along and change the memory the pointer points at and you''ll crash anyway. If you need your code to be bulletproof then use exception handling.

This topic is closed to new replies.

Advertisement