Storage of Variables

Source: Internet
Author: User

In C ++, a data type that is important and hard to grasp is the pointer type. The pointer is the address in the memory. It may be the address of the variable or the function entry address. If the address stored in the pointer variable is the address of the variable, we call it the pointer of the variable (or the variable pointer). If the address stored in the pointer variable is the entry address of the function, we call this pointer A function pointer (or a function pointer ).
Note: pointer variables and pointer variables have different meanings: pointer variables are also referred to as pointers. They are a variable and are pointer types; A variable pointer refers to a variable, which belongs to the pointer type and stores the address of another variable.
We know that the memory space of local variables is automatically allocated by the compiler in the stack, and its scope and lifetime are limited to the definedProgramBlock. We can also use the new operator in the heap to apply for memory. the lifetime of the memory is controlled by the programmer. An important purpose of pointers is to allow anonymous access to the memory allocated in the heap through the new operator.
Generally, there are two methods to access a variable: directly access the variable name or indirectly access the variable through a pointer. In the program we introduced earlier, most of the access to variables is through the variable name, and variables can also be indirectly accessed through pointers, that is, the value of the variable is found through the pointer of the variable, this is what we will learn below. In addition, we also need to learn the pointer of the function.
A reference provides an alias for a variable, which is equivalent to accessing through a variable name. References provide the same capabilities as pointers, but are more intuitive and easier to understand than pointers.

 

A pointer is a data type. A variable with a pointer type is called a pointer variable. In fact, we can regard pointer variables (or pointers for short) as a special variable, which is used to store the addresses of certain types of variables. A pointer stores the address value of a variable. This Pointer Points to the variable of the address to be stored. In short, a pointer is a memory address. Its value indicates the address of the stored data, rather than the content to be stored.
to further clarify the meaning of pointers, you need to understand how data is stored and accessed on machines. We know that memory is a storage space in 8 bytes. Each byte has a serial number, which is called a memory address. It is like a number in each room in a building. Data stored in the memory includes various types of data, addresses, and program commands Code . Variables stored in the memory generally occupy several bytes. We call them memory units. A memory unit stores the value of a variable. The size of memory units occupied by different data types in the machine memory is generally different. For example, the integer value occupies two bytes, and the floating point value occupies four bytes. However, on the same machine, the same data type occupies a storage unit of the same size, while on different machine systems, even if the same data type occupies different storage units, for example, on a 16-bit machine, an integer occupies two bytes while on a 32-bit machine, an integer occupies 4 bytes.
to access data in a unit, you must know the address of the Unit in the memory. This is similar to our actual life. For example, when we are looking for a person, we must know his current address. Otherwise, we cannot achieve our goal.
here, be sure to figure out the difference between the address of a memory unit and the content of the memory unit.
for example, there are two ways to open a drawer. One is to put the key of Drawer A on the body and find the key to open the drawer when necessary, take out the required things. Another way is to lock the key of Drawer A in drawer B for safety. If you need to open drawer A, you need to first find the key in drawer B, open drawer B, take out the key in drawer A, then open drawer A, take out the things in drawer, this is "indirect access ". The pointer variable is equivalent to the B drawer, and the things in the B drawer are equivalent to the address (if the key is compared to the address, it is not exact). The things in the drawer are equivalent to the content of the storage unit.

when a C ++ program is compiled, the memory of the computer is divided into four areas, one including the program code, one including all global variables, and the other is the stack, another is heap. We call heap a free memory zone. We can place objects in this zone through new and delete. You can allocate and release free buckets anywhere. Note that because the objects allocated to the heap do not have scope restrictions, you must delete them once new. Otherwise, the program will crash, which is a memory leak. (C # has solved this headache through memory hosting ). C ++ allocates memory through new. The new parameter is an expression that returns the number of memory bytes to be allocated. This is my previous knowledge about new.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.