In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Today, I will talk to you about how to understand the memory management of C++, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following for you. I hope you can get something from this article.
1. C _ blank + memory distribution
Stack, also known as stack, non-static local variables / function parameters / return values, etc., the stack is growing downward.
The memory mapping segment is an efficient Ithumb O mapping method for loading a shared dynamic memory library. Users can use the system interface to create shared memory and do inter-process communication.
The heap is used for dynamic memory allocation while the program is running, and the heap can grow upward.
Data segments-stores global and static data.
Code snippet-executable code / read-only constant.
2. Dynamic memory management in C language: 1. Malloc/calloc/realloc difference:
What they have in common:
They are all library functions used for dynamic memory request in C language, and the applied space is all on the heap. After using them, free must be used to release them.
The return value type is void*, which must be forcibly transferred when accepting the returned address.
If the application for space is successful: the first address of the space is returned, and NULL is returned if it fails.
Differences:
Malloc: return value type void*
Forced transfer must be made when accepting the returned space address
Success: space first address failed: NULL
Parameter: the total number of bytes of space requested
The requested space is on the heap, and you must use free to release it after use.
Calloc: the returned values are consistent
Parameter list: parameter 1 represents the number of elements; parameter 2 represents the number of bytes of a single type
Function: basically the same as malloc, but calloc initializes the space it applies for
Realloc (void* p, size_t size): resizes the space pointed to by p to sizebytes
P points to NULL: this function is similar to malloc
Suppose: P points to a total of old bytes
Size directly returns the first address of the space to which the value p points
Size > old: expands the space pointed to by p to size bytes
Larger: return to the first address of the original space
Much larger: apply for new space; copy elements from the old space to the new space; free up the old space; return to the first address of the new space
Dynamic memory management in C++: new/delete
Why does C++ want to develop a set of dynamic memory management?
First of all: dynamic memory management in C language can still be used in C++.
Reason:
1. The method in C language is troublesome-the user needs to count the bytes manually, the returned result needs to be strongly changed, null is needed, and the header file needs to be included.
2. Malloc, free: constructors / destructors are not called; new, delete: constructors and destructors are called during space application and release
/ / dynamic memory management in C++: new/delete---- applies for a single type of space / / new [] / delete []-applies for the release of a continuous space / / Note: 1. New/delete is not a function Is the keyword in C++ | | operator / / 2, the space of new must have delete to release new [] you must use delete [] to release class Test {public: Test (): _ t (10) {cout
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.