In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of "the method of pointer and memory allocation in C++". The editor shows you the operation process through an actual case. The method of operation is simple, fast and practical. I hope this article "pointers and memory allocation methods in C++" can help you solve the problem.
Pointer
A pointer is a variable that holds the address of a memory location. We know that all declared variables have a specific address in memory. Declare a pointer variable to point to these addresses in memory.
The general syntax for declaring pointer variables is:
Int p, * ptr; / / declare variable p and pointer variable ptrp = 4; / / assign 4 to variable pptr = & p; / / assign the address of p to pointer variable ptr
In memory, these declarations will be expressed as follows:
This is the internal representation of the pointer in memory. When an address variable is assigned to a pointer variable, it points to a variable as shown in the figure above.
Because ptr has the address of the variable p, * ptr will give the value of the variable p (the variable pointed to by the pointer variable ptr).
Why do you need pointers in C++? To explain the need for pointers, it is necessary to go back to the basic memory layout.
Whenever the program is executed, the program instructions reside in the code snippet. And all methods and data will reside on the stack. The code part can only access the stack part, but not the heap part directly.
Note: because the code segment cannot access the heap segment directly, heap memory cannot be used directly, which will be wasted, resulting in stack memory overflow.
Using pointers can solve this problem. Pointers provide indirect access to heap memory for code parts
Create a pointer in the stack section that points to the memory address of the heap portion, thereby indirectly accessing the heap portion.
Dynamic memory allocation using pointers in C++:
Allocating memory in the heap occurs at run time, not at compile time, so allocating heap memory is called dynamic memory allocation.
In general, C++ does not allocate a memory in the heap because of the complexity commonly encountered when using pointers. So if we allocate only one block of memory, we will use heap memory, for example, for arrays, we will do dynamic memory allocation.
In C++, the new operator is used to allocate memory at runtime, which is allocated in bytes. The new operator represents a request for allocation of dynamic memory on the heap. If enough memory is available, the new operator initializes the memory and returns the address of the newly allocated and initialized memory to the pointer variable.
Syntax:
Datatype * pointer_name = new datatype
Specific examples
Int * ptr = new int;// when dynamically allocated, we can declare a variable in two ways. The int * ptr = new int (10); int * ptr = new int {15}; / / the new operator is also used to allocate blocks of memory (arrays) of data types. Int * ptr = new int [20]; / / the above statement dynamically allocates memory for 20 integers of type int continuously and returns the first element pointing to the sequence pointing to the "ptr" pointer.
Once we use the new keyword to allocate heap memory to a variable or class object, we can use the delete keyword to free up that memory space.
The main use of the concept of dynamic memory allocation is to declare an array by specifying its size, but to allocate memory to an array when it is uncertain.
Look at an example to understand the memory allocation usage of an array.
# include using namespace std;int main () {int len, sum = 0; cout len; int * marks = new int [len]; / / Dynamic memory allocation cout * (marks + I);} for (int I = 0; I < len; I +) {sum + = * (marks + I);} 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.