In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the example analysis of storage scheme and dynamic allocation in C++, which has a certain reference value. Interested friends can refer to it. I hope you will learn a lot after reading this article. Let's take a look at it.
Storage scheme and dynamic allocation
In the previous article, we discussed five schemes that C++ uses to allocate memory to variables, but these schemes do not apply to memory allocated using the new operator, which is called dynamic memory.
As we have shown in previous articles, dynamic memory is controlled by new and delete, not by scope and linking rules. So we can allocate dynamic memory in one function and release it in another.
Usually, there are three separate blocks of memory in C++ compilers, one for static variables, one for automatic variables, and one for dynamic storage.
Although the concept of storage scheme does not apply to dynamic memory, it applies to automatic and static pointers to dynamic memory. There is an example in C++ Primer. We have the following statement in a function:
Float * p_fees = new float [20]
Obviously, we created a 20-length float array through new, and the memory of this array will remain in memory until it is freed using the delete statement. But when the function finishes running, the p_fees pointer will disappear. If you want to be able to use this array elsewhere, you need to return or pass the address in some way.
If we declare the linkiness of p_fees as external, we can access it elsewhere, and the keyword extern can also be used if access is needed in other files.
Initialization
I talked about how to apply for memory using new. What if we want to initialize variables while allocating memory?
If you want to allocate space and initialize the built-in scalar type, you can add the initial value after the type name and enclose it in parentheses:
Int * pi = new int (3); double * pd = new double
If we want to initialize a structure or an array, we need to initialize it with a list of curly braces, which requires the compiler to support Category 11, which is a new feature in cantilever 11:
Struct P {int x, y;}; P * p = new P {3,4}; int * arr = new int [4] {2,3,4,5}
List initialization for single-valued variables is also supported in Category 11:
Double * pd = new double (99.99); Thank you for reading this article carefully. I hope the article "sample Analysis of Storage schemes and dynamic allocation in C++" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
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.