#include "stdafx.h"
#include <iostream>
#include <string.h>
using namespace std;
char *GetMemory()
{
char p[] = "hello world";
printf("%x\n", p);
return p;
}
void Test()
{
char *str = NULL;
printf("%x\n", str);
str = GetMemory();
printf("%x\n", GetMemory());
printf("%x\n", str);
}
int main()
{
Test();
return 0;
}
这是我在做题时遇到的一个问题,求这个代码的问题,根据代码可以看出函数定义了一个局部变量的数组,并将数组的地址返回给str。
在MinGW中结果为
0
61fda4
61fda4
0
0
在VS2013中结果为
0
35f9c4
35f9c4
35f9c4
35f9c4
由运行结果可以发现,在MinGW编译环境下,并不能将局部变量的地址返回,但在VS2013的编译环境下可以将局部变量的地址返回。