C++动态内存分配

1、动态申请内存操作符new,释放内存操作符delete;

动态内存分配相对于景泰内

存分配的特点:

①、不需要预先分配存储空间;

②、分配的空间可以根据程序的需要扩大或缩小。

例:动态内存分配举例

#include<iostream.h>
struct date
{
	int month;
	int day;
	int year;
};
void main()
{
	int index,*point1,*point2;
	point1=&index;
	*point1=77;
	point2=new int;				//动态int型内存地址分配给point2
	*point2=173;
	cout<<"index="<<index<<"\n"
		<<"*point1="<<*point1
		<<"\n"<<"*point2="<<*point2<<endl;
	delete point2;				//释放point2地址内存空间
	point1=new int;
	point2=point1;
	*point1=999;
	cout<<"index="<<index<<"\n"
		<<"*point1="<<*point1
		<<"\n"<<"*point2="<<*point2<<endl;
	delete point1;

	date *date_point;			//date类型指针
	date_point=new date;		//为指针分配date类型内存空间
	date_point->month=7;
	date_point->day=7;
	date_point->year=2014;
	cout<<date_point->month<<'/'<<date_point->day<<'/'<<date_point->year<<endl;
	delete date_point;
}

动态创建对象举例

#include<iostream.h>
class Location
{
	int x,y;
public:
	Location();
	Location(int xx,int yy);
	~Location();
	void Move(int m,int n);
	int GetX(){return x;}
	int GetY(){return y;}
};
Location::Location()
{
	x=y=0;
	cout<<"Default Constructor called."<<endl;
}
Location::Location(int xx,int yy):x(xx),y(yy)
{cout<<"Construcator called."<<endl;}
Location::~Location()
{
	cout<<"Destructor called."<<endl;
}
void Location::Move(int m,int n)
{x=m;y=n;}
void main()
{
	cout<<"Step One:"<<endl;
	Location *Ptr1=new Location;	//分配内存空间给Location对象,调用默认构造函数
	delete Ptr1;
	cout<<"Step Two:"<<endl;
	Ptr1=new Location(1,2);			//调用重载构造函数。
	delete Ptr1;
}

例:动态创建对象数组的举例

①一位数组举例

#include<iostream.h>
class Location               //和上例相同
{
	int x,y;
public:
	Location();
	Location(int xx,int yy);
	~Location();
	void Move(int m,int n);
	int GetX(){return x;}
	int GetY(){return y;}
};
Location::Location()
{
	x=y=0;
	cout<<"Default Constructor called."<<endl;
}
Location::Location(int xx,int yy):x(xx),y(yy)
{cout<<"Construcator called."<<endl;}
Location::~Location()
{
	cout<<"Destructor called."<<endl;
}
void Location::Move(int m,int n)
{x=m;y=n;}
void main()
{
	Location *Ptr=new Point[2];//创建对象数组
	Ptr[0].Move(5,10);		   //通过指针访问数组成员
	Ptr[1].Move(15,20);
	cout<<"Deleting..."<<endl;
	delete[]Ptr;			//通过指针删除整个对象数组。
}

②二维数组举例

#include<iostream.h>
void main()
{
	int (*cp)[3];		//指针cp是指向有3个元素一维数组的指针
	int i,j;
	cp=new int[2][3];	//为cp开辟空间二维数组[2][3]
	for(i=0;i<2;i++)
		for(j=0;j<3;j++)
			*(*(cp+i)+j)=i*10+j;//通过指针访问数组元素。
}

2、malloc函数

注:以下来自百度百科

----在使用上,malloc 和 new 至少有两个不同: new 返回指定类型的指针,并且可以自动计算所需要大小。比如:

int*p;
p=newint;
//返回类型为int*类型(整数型指针),分配大小为sizeof(int);

----或:

int*parr;
parr=newint[100];
//返回类型为int*类型(整数型指针),分配大小为sizeof(int)*100;

----而 malloc 则必须要由我们计算字节数,并且在返回后强行转换为实际类型的指针。

int*p;
p=(int*)malloc(sizeof(int)*128);
//分配128个(可根据实际需要替换该数值)整型存储单元,
//并将这128个连续的整型存储单元的首地址存储到指针变量p中
double*pd=(double*)malloc(sizeof(double)*12);
//分配12个double型存储单元,
//并将首地址存储到指针变量pd中

----第一、malloc 函数返回的是 void * 类型。对于C++,如果你写成:p = malloc (sizeof(int)); 则程序无法通过编译,报错:“不能将 void* 赋值给 int * 类型变量”。所以必须通过 (int *) 来将强制转换。而对于C,没有这个要求,但为了使C程序更方便的移植到C++中来,建议养成强制转换的习惯。
----第二、函数的实参为 sizeof(int) ,用于指明一个整型数据需要的大小。
在Linux中可以有这样:malloc(0),这是因为Linux中malloc有一个下限值16Bytes,注意malloc(-1)是禁止的;
但是在某些系统中是不允许malloc(0)的。在规范的程序中我们有必要按照这样的格式去使用malloc及free:

type*p;
if(NULL==(p=(type*)malloc(sizeof(type))))
/*请使用if来判断,这是有必要的*/
{
perror("error...");
exit(1);
}
.../*其它代码*/
free(p);
p=NULL;/*请加上这句*/

----malloc 也可以达到 new [] 的效果,申请出一段连续的内存,方法无非是指定你所需要内存大小。
比如想分配100个int类型的空间:

int*p=(int*)malloc(sizeof(int)*100);
//分配可以放得下100个整数的内存空间。

----另外有一点不能直接看出的区别是,malloc 只管分配内存,并不能对所得的内存进行初始化,所以得到的一片新内存中,其值将是随机的。
除了分配及最后释放的方法不一样以外,通过malloc或new得到指针,在其它操作上保持一致。
对其做一个特例补充

char*ptr;
if((ptr=(char*)malloc(0))==NULL)
puts("Gotanullpointer");
else
puts("Gotavalidpointer");

----此时得到的是Got a valid pointer。把0赋给malloc能得到一个合法的指针。


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值