User forums > Using Code::Blocks
strlen() fails when the code is compiled with mingw gcc
sylwa:
Hi !
I've just installed code::block bundled with mingw.
To test the compiler/debugger tools, I've compiled this snippet :
--- Code: ---#include <tchar.h>
#include <memory.h> // for memset
#include <string>
using namespace std;
const unsigned kuiConfigFileNameMaxLength = 128;
int _tmain(int argc, _TCHAR* argv[])
{
int iErr = 0;
char cConfigFileName [kuiConfigFileNameMaxLength];
memset( (void*)cConfigFileName , 0, kuiConfigFileNameMaxLength );
// Get input file name
if ( strlen( (const char *) argv[1] ) > kuiConfigFileNameMaxLength )
{
iErr = 1;
}
else
{
memcpy( (void*)cConfigFileName, (const void*)argv[1], strlen( (const char *) argv[1] ));
}
string strConfigFileName( (const char*)(cConfigFileName), strlen( (const char *)cConfigFileName) );
return iErr;
}
--- End code ---
While debugging, the program crahses @ line :
--- Code: --- if ( strlen( (const char *) argv[1] ) > kuiConfigFileNameMaxLength )
--- End code ---
Here the debugger messages :
Program received signal SIGSEGV, Segmentation fault.
In std::string::c_str() const () ()
Program received signal SIGSEGV, Segmentation fault.
Still, this works perfectly when compiled with Visual Studio.
Any hints ?
Regards
Sylvain
ollydbg:
Maybe, you have the same issue like Watching std::string in debugger causes segfault?!?
sylwa:
Dear OllyDbg,
Many thanks for your reply.
I went through the post link you provided to me.
Nevertheless, I'am not expert enough to understand why, in my case, calling the C function strlen() triggers std::string::c_str() C++ method which in turn makes my program crash !
May I ask you some enlightenments or some biblio references about that behaviour ?
Best Regards
jarod42:
I would say that argv[1] is NULL. (Do you provide debug args, and do you have a project ?)
and with some optimisation, crash "occurs" in string constructor.
sylwa:
Dear jarod42,
yes, I provided some non null string into argv[1].
Moreover, following memcpy works fine !
You talk about string constructor crash, but it's invoked / executed later in the code.
The Seg fault that I pointed out occurs at the strlen() code line.
Regards
Sylvain
Navigation
[0] Message Index
[#] Next page
Go to full version