错误在RELEASE但没有在DEBUG [英] bug on RELEASE but not on DEBUG
问题描述
std::string s("foo");
sprintf(buf,"%s",s);
为什么至少在MSVC 2010下,这行代码在DEBUG中没有错误, ?
Why at least under MSVC 2010 this line of code doesn't bug in DEBUG, but bug in RELEASE ?
推荐答案
%s
格式说明符需要NULL终止 char *
。您正在传递一个 std :: string
实例。如果它在DEBUG中工作,那只是纯粹的运气。
The %s
format specifier expects a NULL terminated char*
. You are passing in a std::string
instance. If it works in DEBUG, that's just pure luck.
您应该使用:
std::string s("foo");
sprintf(buf, "%s", s.c_str());
这将提取 char *
可能在运行时库 std :: string
中有不同的实现用于DEBUG和RELEASE。尝试使用这两个设置编译,但添加调试符号到RELEASE构建,然后逐步通过代码。查看存储 s
的内存位置。有什么区别吗?
It is possible that in the runtime library std::string
has different implementations for DEBUG and RELEASE. Try compiling using both settings, but adding debug symbols to the RELEASE build and then step through the code. Look at the memory location where s
is stored. Is there any difference?
这篇关于错误在RELEASE但没有在DEBUG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!