New operator
1, pointer variable = new data type, such as: int *p; p=new int;
2, new allocates a piece of memory space from the memory Zhongwei program, and returns the first address pointing to the memory, which is stored in the pointer variable
New
Allocating memory first, making complex types of constructs
Delete
Release
Delete operator
Delete pointer variable, such as: delete P;
(1) Use new to get memory space, must be released with delete
(2) A pointer can only be called once for a delete
(3) The object using the delete operator must be the first address between the memory allocated with new
Note: the free () function allocates memory in C language
New A;
delete []a;
#include <iostream>using namespace Std;void main () { int *p; p = new int; *p = 5; cout << *p; Delete p;}
It can be initialized while allocating memory with new. The form of use is:
Pointer variable = new data type (initial value)
For example, the above mentioned
p = new int;
*p = 5;
can also be written
p = new int (5);
Create a variable of data type with new
Pointer variable = new data type [array size];
such as: int *p = new Int[5];
At this point the pointer variable points to the address of the first array element. When assigning an array using new, the initial value cannot be supplied.
Scope operators:
In general, if the global variable has the same name as a local variable, then the local variable has a higher precedence within its scope
The C language specifies that the variable can only be used within the scope of a variable and cannot be used with other scope variables. The scope operator is provided in C + +::, he can specify the scope that is required.
Attention:
Cannot be used:: Access local variables in a function
Scoped operators in the C + + language:: You can also qualify members of a class.
#include <iostream>using namespace std;float a=2.4; global variable void Main () { int a = 8; Local variable cout<<a<<endl; cout<<::a<<endl; :: }
Reference
References in the C + + language are used to point to the same address using more than two variable names in different parts of the program, do not consume memory space, must be initialized when the reference is declared, and once initialized, no additional values can be assigned to him, and the pointer is different first, the pointer is space, The second pointer declaration can be uninitialized and then once the pointer is assigned, it can also be assigned.
#include <iostream>using namespace Std;void main () { int num =; int &ref = num; Ref + = Ten; cout<< "num" <<num<<endl; cout<< "ref" <<ref<<endl; num + = +; cout<< "num" <<num<<endl; cout<< "ref" <<ref<<end; }
C + + dynamic memory allocation and release references