Dynamic memory management in C++, new/delete

Tips:

Software Engineering Knowledge:
1. Usually, any class that uses dynamic allocation of memory provides a set of functions at the same time: copy constructor, destructor, overloaded assignment operator function.

Common programming mistakes:
2. When an object of a class contains a pointer to a dynamic memory allocation, it will cause a logical error if it is not provided with an overloaded assignment operator and copy constructor


C++ dynamic memory management:

Allocate and free memory controlled by any built-in or user-defined type in a C++ program.

Symbols used by C++ dynamic memory allocationnew / delete (C++ built-in types)



The advantage of dynamic memory allocation new, the size of memory can be dynamically allocated at runtime:

Declare an array of strings in C as follows:

char a[1000];

However, the following mode cannot pass, because the size of the string declared by char must be constant, and the following mode cannot pass compilation

int  a = 10000; 

char c[a];


However, the above problems do not exist for the storage space generated by new. The memory applied for by new can be dynamically determined at runtime and does not take up a lot of additional storage space.

#include <windows.h>
#include <cstdio>
#include <iostream>

using namespace std;

#pragma warning(disable:4996)

int main(){
	/*
	printf("%d\n", sizeof(unsigned long));
	printf("%d\n", sizeof(DWORD));
	printf("%d\n", MAXDWORD);
	printf("%d\n", 'D');
	printf("%d\n", (int)'A');
	int a = 0x15;
	int b = 015;
	printf("%d %x\n", a, a);
	printf("%d %o\n", b, b);
	
	printf("%d\n", VK_ESCAPE);*/

	int c = 10000;
	//char d[c]
	char *q = new char[c];

	strcpy(q, "Hi,");
	strcat(q, "KeMeng~");

	printf("%s\n", q);
	delete[] q;

	return 0;
}


Rules for dynamically allocating memory:


1. Correct usage: use delete[] to release the allocated array space

2. Correct usage: use delete to delete a single element



Incorrect usage: For an array of objects, using delete instead of delete[] will result in a runtime logic error.

Code:

#include <cstdio>
#include <iostream>

using namespace std;


class Point{
private:
	static int count;
	int x;

public:
	Point() {
		printf("constructor called\n");
		x = ++Point::count;
	}

	Point(const Point &B){
		printf("copy constructor called\n");
		x = ++Point::count;
	}


	~Point(){
		printf("xx %d\n", x);
	}

	Point XX(Point &x)
	{
		//this->x = 0;

		return x;
	}

	void print()const{
		printf("%d\n", x);
	}


};

int Point::count = 0;

int main(){
	Point* pt = new Point[100];
	
	delete pt;

	printf("haha\n");

	return 0;
}




Bad usage: The result of deleting a single element with delete[] is undefined.

#include <cstdio>
#include <iostream>

using namespace std;


class Point{
private:
	static int count;
	int x;

public:
	Point() {
		printf("constructor called\n");
		x = ++Point::count;
	}

	Point(const Point &B){
		printf("copy constructor called\n");
		x = ++Point::count;
	}


	~Point(){
		printf("xx %d\n", x);
	}

	Point XX(Point &x)
	{
		//this->x = 0;

		return x;
	}

	void print()const{
		printf("%d\n", x);
	}


};

int Point::count = 0;

int main(){
	/*
	Point* pt = new Point[100];

	delete pt;
	*/
	Point *pt = new Point;
	
	printf("haha\n");

	delete[] pt;

	return 0;
}







Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324158309&siteId=291194637