c - Release模式下 memcpy 出现问题

标签 c visual-studio-2008

我有一段代码在 Visual Studio 的 Debug模式下运行良好,但在 Release模式下崩溃。经过大量猜测,我找到了导致崩溃的代码段。

char *buff ="some random text";
char *temp = NULL;
char *pos = NULL;
int len = strlen("random text");

pos = strstr(buff,"random");
temp = (char *) malloc(len+1);
memset(temp,0,len+1);
memcpy(temp,pos,len);

这在 Debug模式下完美运行,但在 Release模式下崩溃。谁能指出其中的错误吗?

最佳答案

显示的代码部分看起来不错。作为 unwind 答案的补充。失败的可能原因可能是

1)未初始化的变量

2) 预处理器符号_DEBUG 和NDEBUG。如果 #ifdef _DEBUG/#endif block 中有任何代码。

3)编译器优化

关于c - Release模式下 memcpy 出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17183835/

相关文章:

c - 如何在不使用strtok的情况下根据特定字符拆分字符串

php - 相同的算法在 C 和 PHP 中运行不同

c++ - 使用 Visual Studio 2008 从 x86 编译 x64 dll : unresolved external __imp_ symbols

c# - 使用外部 EXE 作为启动项目

c++ - 不用设置Linker->General->Additional Library Directories可以搜索到什么样的dll或lib

c - IAR 中 C 函数的别名

c - 如何在没有警告的情况下在c99中使用ftruncate

c - 串行读取时丢失数据

visual-studio - Visual Studio 调试器 - 自动变量分配

.net - 如何避免在 Visual Studio 2008 中自动附加到 T-SQL 进行调试?