Memory mode in C ++ Program

Source: Internet
Author: User

For users and scholars who first came into contact with the C ++ program, it is very important to understand the concept of the C ++ language. let's first talk about what the C ++ language is, the so-called C ++ language is a widely used computer programming language.

In C ++, there are three memory allocation methods:

1) distributed from the static storage area. The program has been allocated when it is compiled, and the program exists throughout the entire runtime. For example, global variables and static variables.

2) create a stack. When a function is executed, the storage units of local variables in the function can be created on the stack. When the function is executed, these storage units are automatically released. Stack memory allocation computation is built into the processor's instruction set, which is highly efficient, but the memory capacity allocated is limited.

3) distributed from the stack, also known as dynamic memory allocation. When the program runs, it uses malloc or new to apply for any amount of memory. The programmer is responsible for releasing the memory with free or delete. The lifetime of the dynamic memory is determined by us. It is very flexible to use, but the problem is also the most.

Memory Errors are very troublesome. The compiler cannot automatically detect these errors, which can be captured only when the program is running. Most of these errors do not have obvious symptoms, but they are often invisible and increase the difficulty of error correction. Sometimes the user finds you angrily, but the program has not encountered any problems. When you leave, the error occurs again.

C ++ programmers often make this mistake because they do not realize that memory allocation will fail. A common solution is to check whether the pointer is NULL before using the memory. If the pointer p is a function parameter, use assert (p! = NULL. If you use malloc or new to apply for memory, you should use if (p = NULL) or if (p! = NULL.

There are two main causes for this mistake: first, there is no idea of initialization; second, the default memory initial values are all zero, resulting in reference initial values errors, such as arrays ). There is no uniform standard for the default initial values of the memory. Although sometimes it is zero, we prefer to trust it without any trust.

Therefore, no matter which method is used to create an array, do not forget to assign the initial value. Even the zero value cannot be omitted, so do not bother. The fault lies in the GetMemory function. The compiler always needs to make a temporary copy for each parameter of the function. The copy of the pointer parameter p is _ p, which is used by the compiler.

If the program in the function body modifies the content of _ p, the content of parameter p is modified accordingly. This is why pointers can be used as output parameters. In this example, _ p applied for a new memory, but changed the memory address indicated by _ p, but p was not changed at all. Therefore, the GetMemory function cannot output anything. In fact, each execution of GetMemory will leak a piece of memory, because the memory is not released with free.

For non-Internal data objects, maloc/free alone cannot meet the requirements of dynamic objects. The constructor must be automatically executed when the object is created. The object must automatically execute the Destructor before it dies. Since malloc/free is a library function rather than an operator and is not controlled by the compiler, it is impossible to impose the tasks of executing constructor and destructor on malloc/free.

The class Obj function Initialize simulates the constructor function, and the function Destroy simulates the destructor function. In UseMallocFree, because malloc/free cannot execute constructor and destructor, you must call the member functions Initialize and Destroy to complete initialization and clearing. The UseNewDelete function is much simpler.

Therefore, we should not attempt to use malloc/free to manage the memory of dynamic objects. We should use new/delete. Because the internal data type "object" does not have a process of construction and analysis, malloc/free and new/delete are equivalent to them.

Since the new/delete function completely covers malloc/free, why does the C ++ program not eliminate malloc/free? This is because C ++ programs often call C functions, and C Programs can only use malloc/free to manage dynamic memory.

If you use free to release the "new Dynamic Object", this object may cause program errors because it cannot execute the destructor. If you use delete to release the "dynamic memory applied by malloc", theoretically, the program will not go wrong, but the program is poorly readable. Therefore, new/delete must be paired, and the same applies to malloc/free.

There is a very important phenomenon to tell you. For 32-bit applications, no matter how malloc and new are used, it is almost impossible to cause "memory depletion ". In Windows 98, I wrote a test program using Visual C ++. See example 7-9. This program will run endlessly and will not be terminated at all. Because the 32-bit operating system supports "virtual storage" and the memory is used up, the hard disk space is automatically replaced. I only heard the sound of the hard drive. Window 98 was so tired that it didn't respond to the keyboard or mouse.

  • How does C ++ Test automatically generate a function?
  • How to better design C ++ stack objects
  • C ++ syntax Summary
  • Summary: C ++ Lexical Analysis Design Method
  • Analysis on Windows event log monitoring by C ++ Program

I can conclude that for 32-bit or more applications, the "memory used up" error handler is useless. Now, Unix and Windows programmers are happy with this: the error handler does not work, and I will not write it, saving a lot of trouble.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.