In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
How to analyze the points for attention when learning C++, I believe that many inexperienced people are at a loss about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
Many programmers learn C++ to sum up experience, some feel that C++ language is an independent language, not on the basis of C language, you can directly learn C++ without first starting from C, you can learn C++ directly without starting from C.
1. Memory allocation method
There are three ways to allocate memory:
(1) allocate from static storage area. The memory program has been allocated when it is compiled, and this memory block exists throughout the run of the program. For example, global variable, static variable.
(2) create it on the stack. When the function is executed, the storage units of local variables within the function can be created on the stack, and these storage units are automatically released at the end of the function execution. Learning C++ memory allocation operation is built into the instruction set of the processor, which is very efficient, but the amount of memory allocated is limited.
(3) allocation from the heap, also known as dynamic memory allocation. When the program is running, it uses malloc or new to request any amount of memory, and the programmer is responsible for when to release memory with free or delete. The lifetime of dynamic memory is determined by us, and it is very flexible to use, but it also has the most problems.
2. Common memory errors and their countermeasures
Memory errors are very troublesome. The compiler cannot automatically find these errors and usually catches them when the program is running. However, most of these mistakes have no obvious symptoms and appear from time to time, which increases the difficulty of correcting them. Sometimes users bring you here angrily.
There is nothing wrong with the program, and as soon as you leave, the error occurs again. Common memory errors and their countermeasures are as follows: memory allocation was not successful, but it was used.
Novice programmers often make this mistake because they do not realize that memory allocation will not be successful. A common solution is to check that the pointer is NULL before using memory. If the pointer p is an argument to the function, check it with assert (paired null) at the entry of the function. If you are using malloc or new to request memory, you should use if (p==NULL) or if (pumped memory null) for error prevention processing. Although the memory allocation was successful, it was referenced before it was initialized.
There are two main reasons for this error: one is that there is no concept of initialization, and the other is the mistaken belief that the default initial values of memory are all zero, resulting in errors in referencing initial values (such as arrays). There is no uniform standard for what the default initial value of memory is, although sometimes it is zero, and we would rather believe it.
For example, the operation of subscript "more 1" or "less 1" often occurs when using arrays. Especially in the for loop statement, the number of loops is very easy to make mistakes, resulting in array operations out of bounds. Forgot to release memory, resulting in a memory leak. A function containing this error loses a piece of memory each time it is called. At the beginning, the system has plenty of memory, and you can't see the error. There was a time when the program suddenly died and the system gave a hint: memory was exhausted.
The application and release of dynamic memory must be matched, and the number of uses of malloc and free in the program must be the same, otherwise there must be errors (same as new/delete). Frees up memory but continues to use it. In learning C++, pointers and arrays can be used interchangeably in many places, giving people the illusion that they are equivalent.
So no matter how you create the array, don't forget to assign the initial value, even if you assign a zero value, don't be troublesome. The memory allocation was successful and initialized, but the operation crossed the memory boundary. Arrays are created either in static storage (such as global arrays) or on the stack. The array name corresponds to (rather than points to) a piece of memory whose address and capacity remain the same throughout its lifetime, and only the contents of the array can be changed.
After reading the above, have you mastered the method of how to analyze the points for attention when learning C++? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.