0. Pointers & Arrays
An array is a pointer to its first element, which is a pointer to an array variable. The (*) can be used for arrays, or ([]) for pointers, eg:
int mynums[5] = {0};
int* pnums = mynums;
You can use * (mynums+1), or you can use pnums[1].
1. Points to note when using the pointer
① Be sure to initialize the pointer variable to NULL;
② to verify that the pointer is valid before use ( check if null);
③new and delete to match, otherwise it will cause memory leaks ;
2, check whether the allocation request issued by new is satisfied
Method one: Using exception handling, catch (Bad_alloc)
1#include"stdafx.h"2#include <iostream>3 using namespacestd;4 5 intMain ()6 {7 Try8 {9 int* PAge =New int[536870911];Ten Delete[] pAge; One } A Catch(bad_alloc) - { -cout<<"error!"<<Endl; the } - - return 0; -}
Law II: New (Nothrow)
1#include"stdafx.h"2#include <iostream>3 using namespacestd;4 5 intMain ()6 {7 8 int* PAge = New(nothrow) int[536870911];9 if(PAge)Ten { One Delete[] pAge; A } - Else -cout<<"error!"<<Endl; the - return 0; -}
3. Reference = = Alias
Reference, you can access the memory unit of the corresponding variable .
21-day learning-through C++_DAY6