• Announcements

Archived

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

Why does this code work?

Recommended Posts

ok, i''m useing MSVC++ 6.0 on a win98 machine. when i compile and run this code it works fine... but it shouldn''t...
  #include class CTest { public: CTest(){cout << "Ctest Created" << endl;} ~CTest(){cout << "CTest Destroyed" << endl;} void Stuff(const char *stuff){cout << stuff <<": ";}; void Stuff2(){cout << i <
i did this trying to see some of the errors it would produce so that if i accidently did it, i could figure out what was going on more quickly. Any help would be apreciated. ------------------------------------------------- Don''t take life too seriously, you''''ll never get out of it alive. -Bugs Bunny

Share on other sites
In debug mode, you can overbound and array without a crash (most of the time)

------------------------------
"I''m a decorated astronaut, I don''t make those kind of mistakes."
"Oh now wait a minute. Look I''ll show ya. I''ll enter the same calculations using what we like to call ''The Right Way''."

-Rem
Zirem Software

Share on other sites
If you want bounds checking you need to use an array class that checks it

  templateclass CSortaSafeArray{public:CSortaSafeArray(int i=100) : m_array(0), m_iBounds(i){m_array = new T[m_iBounds];}~CSortaSafeArray(){if(m_array)delete[] m_array;}T& operator[](int i){_ASSERT(i>=0);_ASSERT(i

Magmai Kai Holmlor
- The disgruntled & disillusioned

Share on other sites
As others have said C and C++ do not to bounds checking on arrays. The reason is that these languages were made to be fast. They also wanted these languages to be used to write things that are normally written in ASM. Most instructions from C can be translated very easily to ASM. C doesn''t do a lot of extra stuff.

Anyway, it is up to the OS to give you errors on bounds checking. A lot of it depends on the hardware platform. I don''t know how the PC does it because the PC is fairly complicated with segmentation as well as virtual memory. On a normal non-segmented architecture with virtual memory, the memory is divided into pages. You can usually write to memory while you are still in the same page. (However, since that memory isn''t assigned to the array you may be writing over other stuff.) However, if you try to write to another page the address translation (virtual address to physical address) will likely fail and the OS will give an error.

So, in many cases it will not give you an error and other cases it will. In DOS, for example, you''ll probably just lock your system up.

If anything I wrote is wrong please correct me. Also, I''m curious how this all works with segmentation.

Share on other sites
I found something amusing in my compilers book while I was working on my final project over the weekend. This is from Andrew Appel's (designer of the ML programming language, and several other notable accomplishments in programming languages) Modern Compiler Implementation in Java book:

"When a program has a bug, it should detect that fact as soon as possible, and announce that fact (or take corrective action) before the bug causes any harm.
"Some bugs are very subtle. But it should not take a genius to detect an out-of-bounds subscript ... There is no excuse for a compiler that is unable to emit code for checking array bounds. Optimizing compilers can often SAFELY remove the checks by compile time analysis ...
"One might say, by way of excuse, 'but the language in which I program has the kind of address arithmetic which makes it impossible to know the bounds of an array.' Yes, and the man who shot his mother and father threw himself upon the mercy of the court because he was an orphan."

I spent the next 5 minutes laughing at that rather harsh attack on C. While I think he may have gone a bit overboard there, it is important to remember that bounds checking is really useful for tracking down bugs.

Edited by - c_wraith on June 7, 2001 12:55:30 PM

Share on other sites
pouya--excellect example.

c_wraith: well, the author''s intentions aren''t altruistic--I mean, he has his OWN language ideas he''s touting, right? But it''s a very valid point (and a pretty hilarious metaphor). Still, sounds a bit like sour grapes to me, kinda like the way Stroustroup always bags on Java.

• Partner Spotlight

• Forum Statistics

• Total Topics
627674
• Total Posts
2978558

• 11
• 11
• 10
• 12
• 22