Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

C language and the method of memory Management in C++

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the related knowledge of C language and the methods of memory management in C++, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe that you will gain something after reading this article on the methods of memory management in C language and C++. Let's take a look.

Memory distribution

Main segment and its distribution

After each program runs, it will have its own independent virtual address space. The size of this virtual address space is related to the number of bits of the operating system. The virtual address space of a 32-bit hardware platform can range from 0 ~ 2 ^ 32-1, that is, 0x00000000~0xFFFFFFFF, with a total 4GB size. The virtual address space of 64-bit hardware platform will be very large. The layout of the CAccord Craft + program in virtual memory is roughly as follows (only the relevant main segments are listed):

As shown in the above figure:

1, stack area (stack)-automatically allocated and released by the compiler, storing the parameter values of the function, the values of local variables, etc. It operates in a manner similar to the stack in the data structure.

2, heap area (heap)-generally allocated by programmers to release, if programmers do not release, the end of the program may be recycled by OS. Note that it is different from the heap in the data structure, but the allocation is similar to the linked list, and it will cause a memory leak if it is not released.

3. Data segment (static zone) (static)-the global and static variables are stored together, the initialized global variables and static variables are in the same area, and the uninitialized global variables and uninitialized static variables are in the adjacent area. -released by the system at the end of the program.

4. Memory mapping segment is an efficient way to load a shared dynamic memory library. Users can use the system interface to create shared memory and do inter-process communication.

5. Code snippet-the binary code that stores the body of the function, and the direct operands are also stored in this location. Such as int axiom 4;.

Dynamic memory management-dynamic memory management in heap area C language

Four malloc/calloc/realloc/free functions are used in C language for dynamic memory management.

1. Malloc: used to dynamically apply for a piece of memory, but not initialize it.

2. Calloc: dynamically apply for a piece of memory, but initialize the applied memory to 0.

3. Realloc: when the applied memory is insufficient, realloc will be used to dynamically expand the capacity (there will be a certain degree of consumption).

4. Free: used to free dynamically requested memory (when memory is not in use, free must be used to free it, otherwise it will cause memory leak)

C++ dynamic memory Management

Because C++ is compatible with C can also use the above functions for memory management, but C++ introduced two operators new/delete to apply for memory and release.

The usage of new and delete

1. Operation built-in type

/ / apply for a single object int * p1=new int;// dynamically apply for a space of int type. Int * p2=new int (3); / / dynamically request a piece of space of type int and initialize it. Delete p1 / delete p2ramp / dynamically apply for a contiguous space int * p3=new int [10]; / / [] is the number of objects / / release delete [] p3

Note: apply for and free space for individual elements, use new and delete operators, request and release contiguous space, use new [] and delete [].

2. Operate custom types

Class Test {public: Test (): _ data (0) {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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report