Winter Vacation [c ++] -new and delete expressions

A, new expressions work steps
three steps that occur when using the new expression:

  1. Calls the standard library function named operator new allocates large enough raw untyped memory,
    to save a specified type of object
  2. The operation of a type constructor initializes the object
  3. Constructor returns a pointer to the new object is allocated and configured

Two, nedelete expression working step
two steps that occur when using the delete expression:
4. calls the destructor, recycling object APPLICATIONS
The standard library function calls the called operator delete free the memory used by the object

Three, operator overloaded versions of new and operator delete functions

//operator new库函数
void * operator new(size_t);
void * operator new[](size_t);
//operator delete库函数
void operator delete(void *);
void operator delete[](void *);
	void *operator new(size_t sz)
	{
		void * ret = malloc(sz);
		//cout << "调用operator new" << endl;
		return ret;
	}
	void operator delete(void * pointer)
	{
		free(pointer);
		//cout << "调用operator delete" << endl;
	}

You can only create stack objects

#define _CRT_SECURE_NO_WARNINGS
#include "stdafx.h"
#include "string.h"
#include <iostream>
//#include "stdio.h"

using std::cout;
using std::endl;

class Student
{
//public:
private:
	void *operator new(size_t sz)
	{
		void * ret = malloc(sz);
		cout << "调用operator new" << endl;
		return ret;
	}
public:
	Student(int id,const char * name)			//为什么这里要加const?
		:_id(id),
		_name(new char[strlen(name)+1])
	{
		strcpy(_name, name);
		cout << "调用构造函数" << endl;
	}
public:
//private:
	~Student()
	{
		delete [] _name;
		cout << "调用析构函数" << endl;
	}
public:
	//void destroy()
	//{
	//	delete this;
	//}
	void operator delete(void * pointer)
	{
		free(pointer);
		cout << "调用operator delete" << endl;
	}
	void print() const
	{
		cout << "_id:" << _id << endl;
		cout << "_name:" << _name << endl;
	}
private:
	char* _name;
	int _id;
};


//只能创建栈对象
//只能创建堆对象

int main()
{
	//栈对象
	Student stu_z(99, "lin");
	stu_z.print();
	//堆对象
	//Student * stu_d = new Student(100, "KANG");
	//stu_d->print();
	////delete stu_d;
	//stu_d->destroy();

	system("pause");
    return 0;
}

Call the constructor of
the _id: 99
the _name: LIN
Press any key to continue...
Calling the destructor

You can only create heap object


#define _CRT_SECURE_NO_WARNINGS
#include "stdafx.h"
#include "string.h"
#include <iostream>
//#include "stdio.h"

using std::cout;
using std::endl;

class Student
{
public:
//private:
	void *operator new(size_t sz)
	{
		void * ret = malloc(sz);
		cout << "调用operator new" << endl;
		return ret;
	}
public:
	Student(int id,const char * name)			//为什么这里要加const?
		:_id(id),
		_name(new char[strlen(name)+1])
	{
		strcpy(_name, name);
		cout << "调用构造函数" << endl;
	}
//public:
private:
	~Student()
	{
		delete [] _name;
		cout << "调用析构函数" << endl;
	}
public:
	void destroy()
	{
		delete this;
	}
	void operator delete(void * pointer)
	{
		free(pointer);
		cout << "调用operator delete" << endl;
	}
	void print() const
	{
		cout << "_id:" << _id << endl;
		cout << "_name:" << _name << endl;
	}
private:
	char* _name;
	int _id;
};
//只能创建堆对象

int main()
{
	//栈对象
	//Student stu_z(99, "lin");
	//stu_z.print();
	//堆对象
	Student * stu_d = new Student(100, "KANG");
	stu_d->print();
	//delete stu_d;
	stu_d->destroy();

	system("pause");
    return 0;
}

Call operator new
constructor is called
the _id: 100
the _name: KANG
calling the destructor
call operator delete
Press any key to continue.

Published 43 original articles · won praise 4 · Views 1199

Guess you like

Origin blog.csdn.net/weixin_42176221/article/details/103858937