我对 VS C++ 有一些误解。在 2010 版中,以下代码可以正常工作:我可以获得一个字符串,并且可以释放内存后缀。
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define MAX 14
void GetString(char **str);
int main(int argc, char *argv[])
{
char *str = NULL;
GetString(&str);
printf("%s\n", str);
getchar();
free(str);
return 0;
}
void GetString(char **str)
{
char *s = (char *) malloc(sizeof(char) * MAX);
strcpy(s, "HELLO, WORLD!");
*str = s;
}
但在 VS 2008 中,上面的代码最终会出现内存损坏错误。我想,使用的标准存在细微差别。我对吗?如果不是,请告诉我,为什么相同的代码在不同版本的 Visual Studio 中不起作用?
预先感谢您的回答。
ps 我很好奇会发生什么,但还没有找到有关该主题的任何信息。
pps 使用的语言 - C