Windows 7 64 SP1,
Boost 1.42,
MS VS 2010 Ultimate,
C++
This stripped down code compiles and runs fine within the “Visual Studio x64 Command Prompt (2010)” using these switches:
cl /EHsc /W4 /nologo
#include <string>
using namespace std; // I know not to use.
int main() {
string sentence = "abc";
string word_found = "";
string::const_iterator it = sentence.begin();
while ( *it != ' ' && it != sentence.end() )
word_found += *it++;
}
However, after compiling within the VS IDE, when run, it crashes with the error:
Expression: string iterator not dereferencable
The problem is apparently with the *it in ( *it != ' ' && it != sentence.end() ). I just need to short-circuit the expression so the now-right-hand expression, the *it != ' ', doesn’t evaluate:
while ( it != sentence.end() && *it != ' ' )
Then it runs well.
But why does it run flawlessly after compiling the original code from the command prompt? There is no other unusual behavior in the much larger program this subset is derived from. What doesn’t the string::iterator cause the same problem?
FWIW, these are the default MS VS command line options: /ZI /nologo /W3 /WX- /Od /Oy- /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_UNICODE" /D "UNICODE" /Gm /EHsc /RTC1 /GS /fp:precise /Zc:wchar_t /Zc:forScope /Fp"Debug\test short.pch" /Fa"Debug\" /Fo"Debug\" /Fd"Debug\vc100.pdb" /Gd /analyze- /errorReport:queue
Why do the command-line compiler and the IDE compiler produce different executables? Is there a switch I can add to the command prompt compiler that would make the executable behave in the same manner as when compiling from the VS IDE
Those runtime checks are only enabled by default in debug builds. You either need to
build with one of the debug runtimes by compiling with the
/MDdor/MTdoption, orenable checked iterators by changing the
_ITERATOR_DEBUG_LEVELto1(/D_ITERATOR_DEBUG_LEVEL=1).