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

先看下面一段代码

#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分配的内存不在栈中吧,有知道的可以告诉我。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值