现在的位置: 首页 > 综合 > 正文

从GetMemory()看C/C++内存分配机制

2014年01月22日 ⁄ 综合 ⁄ 共 1330字 ⁄ 字号 评论关闭
先看下面一段代码

#include<iostream>

using namespace std;

void GetMemory(char *p, int num)
{
	p=new char[num];
}

void main()
{
	char *str=NULL;
	GetMemory(str,100);
	strcpy(str,"hello world!");
	cout<<str<<endl;
}

平台是windows 7+ VC6.0  ,编译通过,链接通过,但是程序运行的时候却崩溃了。原因就在于GetMemory(str,100)传递的是str的副本,所以str并没有被分配到内存,str依然指向NULL.而strcpy(char * strDest,const char * strSrc)的源码中并没有检查strDest是否为空,最终导致程序的崩溃。

正确的做法应该是传递指针的指针,代码如下:

#include<iostream>

using namespace std;

void GetMemory(char **p, int num)
{
	*p=new char[num];
}

void main()
{
	char *str=NULL;
	GetMemory(&str,100);
	strcpy(str,"hello world!");
	cout<<str<<endl;
}

使用了双重指针**p。将str的地址传进去。 这个比较难理解,其实可以这样看。在调用GetMemory(&str,100)的时候,&str=p,那么*p就是str,**p就是指向str的指针了,然后为**p申请空间,就是*p=new char【num】;
当然还可以用另外一种方法,通过返回地址空间的指针,

代码如下

#include<iostream>

using namespace std;

char* GetMemory(char *p, int num)
{
	p=new char[num];
	return p;
}

void main()
{
	char *str=NULL;
	str=GetMemory(str,100);
	strcpy(str,"hello world!");
	cout<<str<<endl;
}

以上两种,无论是用new  或者使用malloc()申请内存,都是在堆中分配内存,堆中分配的内存使用完后要及时释放,否在会造成内存泄露,看下面两种在栈中的内存分配:

#include<iostream>

using namespace std;

char* GetMemory()
{
	char str[]="hello world!";
	return str;
}

void main()
{
	char* p=GetMemory();
	cout<<p;
}

打印出来时乱码,这就在于函数中分配的内存存在于栈中,函数调用结束后,栈就被释放了,所以*p依然指向栈中的一个地址,但栈中的内容又不断变化,所以就指向了一个不确定的代码。

#include<iostream>

using namespace std;

char* GetMemory()
{
	char *str="hello world!";
	return str;
}

void main()
{
	char* p=GetMemory();
	cout<<p;
}

但是这样就可以,可能是*str分配的内存不在栈中吧,有知道的可以告诉我。

抱歉!评论已关闭.