Dynamic storage


1. new & delete

1.1 new

  • Use new to allocate memory while the program is running. The general format for allocating memory for data objects is: typename * pointer_name = new typename
  • Need to specify the storage data type of the allocated memory and the data type associated with the pointer variable
  • The new memory has no name, it can only be managed by the pointer it returns
  • The difference between the memory block allocated by new and the memory allocated by conventional variables: (1) The memory of conventional variables is allocated at compile time, and its memory is in the stack ; (2) Using new is to allocate memory when the program is running , which is a memory heap (heap) or free area (free store) in.
  • Q: (1) What is the difference between heap and stack space? (2) Why are new memory and conventional variable memory allocated from the heap and stack respectively? (3) How does new allocate memory?
  • A: No

1.2 delete

  • Use delete to release memory: The memory allocated using new is released by delete. If there is only new but no delete, a memory leak will occur , that is, the memory released by new does not return to the memory pool and cannot be used by new again.
  • Use format of delete : delete pointer variable , the pointer variable points to the address of the new memory, not the address of a regular variable.

1.3 Precautions for using new and delete

  • Do not use delete to release memory that is not allocated by new: the memory allocated by new must be released by delete, and delete can only release memory allocated by new.
  • Do not use delete to release the memory that has been released, the result of doing so will be uncertain
  • If you use new [] to allocate memory for the array, you should use delete [] to release
  • If you use new to allocate memory for an entity, you should use delete (without square brackets) to release
  • It is safe to apply delete to a null pointer

1.4 Example

1.4.1 Copy the code on the book: dynamic allocation of arrays

void App(int* & pa, int len);
int main()
{
    
    
	int* ary = NULL, * t;
	int i, n;
	cout << "n=";
	cin >> n;
	App(ary, n);
	for (t=ary; t < ary+n; t++)
	{
    
    
		cout << *t << " ";
	}
	cout << endl;
	for (i = 0; i < n; i++)
	{
    
    
		ary[i] = 10 + i;
	}
	for (i = 0; i < n; i++)
	{
    
    
		cout << ary[i] << " ";
	}
	cout << endl;
	delete[]ary;
	ary = NULL;
 }
void App(int * & pa,int len)//这里pa的参数类型需是指针引用参数
{
    
    
	pa = new int[len];
	if (pa==NULL)
	{
    
    
		cout << "allocation faiure\n";
		return;
	}
	for (int i = 0; i < len; i++)
	{
    
    
		pa[i] = 0;
	}
}

analysis:

  • Dynamically allocated arrays can be accessed using subscripts or pointers
  • The format of the dynamically allocated array: type * pointer = new type [array length]
  • Release the dynamic array: delete [] pointer
  • In order to prevent memory leaks, assign the value of ary to t when using pointer access , otherwise the final delete will not point to the new dynamic array
  • In the App function, pa uses a pointer reference parameter. If the reference parameter is not used, ary just passes its value to pa , that is, pa=NULL , but the address of the dynamic array received by pa cannot be passed to ary . When the App is called, pa Will be released by the system (can be understood as a local variable). The result is that the value of ary is still NULL , and the new dynamic array cannot be deleted because pa has been released by the system. If you don't use a reference parameter, the App() function needs to return a pointer to the dynamic array to ary.

1.4.2 copy book code: basic types

int main()
{
    
    
	int* p = NULL;
	p = new int(100);
	if (p==NULL)
	{
    
    
		cout << "allocation faiure\n";
	}
	else
	{
    
    
		cout << *p << endl;
		delete p;
		p = NULL;
	}
 }

analysis:

  • Basic types use new to apply for space: type * pointer variable name = new type (initial value)
  • You can use parentheses to initialize the value of the requested memory
  • After the delete, the pointer variable does not disappear. At this time, assign it to NULL to clear the original address value, so as not to release the memory again.

Guess you like

Origin blog.csdn.net/qq_36439722/article/details/105758683