Necessity detailed analytic function configuration appears

O memory needs!

Hello classmates, you have learned with scanf, cin input and output instructions. But if you want to enter an article in English can also be used if the two commands do? The answer is no, each computer will have its own random processor (RAM), the following is my computer parameters:
Here Insert Picture Description
We can find my computer has 16G of memory, but there will only be a small part 16G give Dev -C ++, open the task Manager memory allocation can be seen, only about about 1M able cin and scanf call.Here Insert Picture Description

Traditional coping strategies C language

#include<stdlib.h>          //调用malloc函数的头文件
s=(char*) malloc(128*sizeof(char));//用malloc函数申请动态内存
free(s);//释放空间

The interpretation of the code :

  • Since the malloc function returns void, null pointer type, and s is a character pointer, a direct assignment may be given. Therefore, plus cast (char *) at the front.
  • Sometimes we do not remember the number of bytes occupied by the characters apply, so here use sizeof ();
  • 128 * 128 char type represents the application address space.
  • Finally, after the end of the program and does not own dynamic memory release, resulting in a memory leak, you need to call free () to be released.

Benefits of applying dynamic memory

char m_szname1[25];     //申请长度为25的char数组
m_szname2=(char*) malloc(255);
char * m_szname2;       //申请一个char类型的指针
free(m_szname2);

The interpretation of the code :

  • The first way would be to apply a char array defines its size, this problem m_szname1 accommodate up to 25 characters.
  • Apply a second pointer to a string, the pointer is not limited length string
  • Since the pointer must address, you need to apply in advance dynamic memory using malloc pointer.
  • Program does not self-release dynamic memory, you need to call free to be released.

There is no worse feeling very malloc

Destructor debut

What destructor:

1, contrary destructor (destructor) constructor , when the object end of their life cycle (e.g. where the object function call has been completed), the system automatically performs the destructor. Destructors are often used to make "clean up the aftermath of" work (for example, when creating a new object is opened up a memory space.

2, destructor if we do not write it, C ++ will help us automatically into one , that is: C ++ will automatically help us write a destructor. In many cases, the automatically generated destructor can work well, but some important deeds, we must to write our own destructor.

3, C ++ as required, as long as there should be a corresponding new delete. The new is new in the constructor, the time is born. So when dead, that is, when destructor is called, we must delete operations on pointers.

Destructor benefits:

He somehow similar to the free () function, however, is to clean up the constructors dynamic memory applications , but we note that free () function is the need to use human judgment call people , which requires the programmer to judge for themselves when no longer need to apply for dynamic memory, demanding. Destructor will call itself after the constructor by the compiler , the release of dynamic memory, showing its superiority.

wuli Kankan references

You can find out more about heap, malloc and free information, click here Wallpaper .
You can find out more about new and delete objects created information, please click here Wallpaper .

Wrong with a lot of advice!

Published an original article · won praise 0 · Views 204

Guess you like

Origin blog.csdn.net/liuyiming2019/article/details/104825514