Class 5: Scope for, dynamic memory allocation, nullptr

1.范围for
int arr1(){1,2,3,4,5,6,7};
for(auto x:arr1)
cout<<x<<endl;

int arr2(){2,2,3,1,3,4,2}
for(auto &x:arr2)
cout<<x<<endl;


2. Dynamic memory allocation
in C ++, the memory is divided into five regions in detail:
Stack: the local variable in this function, automatic allocation and deallocation by the compiler
stack: programmers malloc \ new allocated, with free \ delete to release, after forgetting to release, the system will recover
global, static variables memory: put global variables and static variables static, the system is released at the end of the program
the constant memory areas:
program code area:

// Use the difference between heap and stack and
stack: space is limited, dispensing speed
reactor: the actual physical memory without departing have, also allows the operating system within the maximum memory size can be allocated, the allocation can be
assigned speed ratio stack slow, you can always use malloc \ new to allocate, use free \ delete to free, very flexible.

// malloc and free: malloc and free to allocate and free memory from the heap with. malloc and free () is a function
// malloc (memory allocation): Dynamic memory allocation

// general form:
void * the malloc (int NumBytes): // NumBytes: the number of bytes to be allocated, the allocation succeeds the point pointer points to the allocated memory to return, or NULL on failure allocation
@ Free:
void Free (void * FirstByte): previously allocated with malloc memory back program (operating system), which means that the release of this memory. This memory system is recovered and orderly distribution through out when needed

P = NULL * int;
P = the malloc (the sizeof (int)); // 4 bytes allocated in the heap
IF (P = NULL!) {
// allocation is successful
* P =. 5;
COUT * P << << endl;
Free (P); // release heap memory
}

char *point = NULL;
point = (char *)malloc(100 * sizeof(char))
if(point != NULL)
{
strcpy_s(point,100,"hello,world"); //strcpy_s函数: 字符串拷贝
cout<<point<<endl;
}

-----------------------------------

int * p = (int *) malloc (100 * sizeof (int)); // allocate a first region, and then use this pointer to a space
if (p = NULL!) {

int *q = p;
*q++ = 1;
*q++ = 5;
cout<<*p<<endl;
cout<<*(p+1)<<endl;
free(p);

}

------------------------------------
// new new and delete: is the operator (identifier), C ++ using new and delete to allocate and free memory is no longer used malloc and free to allocate and free memory
// new, delete than malloc, free more powerful
new format is generally used:
a pointer variable name = new type identifier
pointer type name = new type identifier (initial value);
pointer type name = new type identifier [number of memory cells];

int *myint = new int; //
if(myint !=NULL){

myint. 8 = *;
COUT * << endl << myint;
Delete myint; // free space in a single myint

}

-------------------------------
int *p = new int(18);
cout<<*p<<endl;
delete p;
-------------------------------
int *pa = new int[100];
if(pa != NULL){

int *pb = pa;
*pb++ = 10;
*pb++ = 20;
cout<<*pa<<endl;
cout<<*(pa+1)<<endl;

}

3.nullptr: it represents a null pointer, C ++ 11 introduced new keywords, in order to avoid confusion integers and pointers

char * p = NULL; // NULL is actually 0
char * Q = nullptr a;
char * P = nullptr a;

if(p == nullptr){
cout<<"nullptr==NULL"<<endl;
}

// NULL and compare nullptr
typeid (NULL) .name ()
typeid (nullptr) .name ()
Conclusion: initialize the pointer, in the past and used the occasion NULL pointer-related, instead of all use nullpotr

Guess you like

Origin www.cnblogs.com/cy2837/p/12203392.html