Comprehensive comparison of pointer variables, common variables, memory and address, and comparison of pointer variable memory

Source: Internet
Author: User

Comprehensive comparison of pointer variables, common variables, memory and address, and comparison of pointer variable memory

This article introduces four concepts:

1. Common variables

2. pointer Variables

3. Memory (memory space)

4. Address

What is the memory first? Memory is a real hardware that can store data! There is a large memory in the interior of one of our programmable chips.

The memory is like a small grid. The size of each grid is one byte, which can store one byte of data.

How can we differentiate so many memories? You have to rely on the address. The address is the memory identifier, and each address corresponds to a memory. Therefore, memory and address are closely related.

 

 

Next, let's look at what is a common variable?

For example, char a; is a common variable. The common variable a is actually created by the language itself to facilitate memory expression and memory creation. Access to a is actually a direct access to the memory. As for the memory address represented by a, programmers generally do not need to worry about it. The compiler will automatically allocate an address, that is, to assign an address to. If you want to know the address of a, you can also learn through &.

Let's look at the pointer variable. The difference between the pointer variable and the common variable is that the common variable is associated with a memory space. The pointer variable is associated with two Memory Spaces:

1. Save the space of the pointer variable. The space size is fixed, and the 32-bit system contains 4 bytes.

2. memory space pointed to by the pointer.

For example, char * a; pointer variable a requires a space, that is, (1 ).

(1) The content stored in this space is the first address of another memory space. You can change the pointer variable to access the memory space elsewhere.

If there are two common variables:

1. a indicates a piece of memory space.

2. & a indicates the address of the current memory space

Pointer variables have three forms:

1. a indicates the memory space corresponding to pointer.

2. & a indicates the first address of the memory space corresponding to the current pointer

3. * a indicates the entire memory space corresponding to the variable pointed to by pointer.

If it is hard to say 4th forms, that is, p-> x, this form occurs when the struct variable accesses its own members. P-> after the combination of x, we can see that a whole represents the memory corresponding to x. Note that the "->" symbol should not be interpreted as the pointer p pointing to x, but p-> x should be considered as a whole, "->" is just an operator that combines p and x to indicate the memory of x.

The above conclusions are universal. Think about it. If p is a struct pointer, what is * p? Although the value of * p is of little significance. * P is the variable memory space corresponding to the entire struct. This common * a is interpreted as the same -- it indicates the entire memory space corresponding to the variable pointed to by pointer. (This can be proved by a program. For the main line of the article, I want to explain the process separately in the article "what is the value of the referenced struct pointer)

The above briefly introduces the four concepts of common variables, pointer variables, memory space, and address, and compares the differences between common variables and pointer variables in detail.

 

 

 

Next, let's take a look at the differences between addresses, pointers, and numerical constants.

1. A pointer is also called a pointer variable. The address is a constant. Pointer to address. The address is only the memory ID.

2. How to forcibly convert a number to a pointer type.

Forcibly convert a number to the pointer type. For example, if int * (0) is used, int * (0) is a pointer instead of an address. (The Compiler allocates memory for int * (0)

Actually: int * p = int * (0); then p is equivalent to this int * (0 ).

This Pointer Points to the address 0. Therefore, 0 indicates the address, and int * (0) indicates the pointer.

3. Although the pointer is not an address, but it matches the address, you can assign the address to the pointer. Of course, you can also assign a numeric constant to the pointer directly. But do not do this. It should be the address represented by this number. It may be that you should not access it and may cause a segment error.

Therefore, the pointer value is generally used to assign a value to the pointer for the variable to take the address, or assign a value to the pointer through the pointer (p = & a or p = p1 ).

 

 

 

Finally, let's briefly describe it. The relationship between the pointer and the memory space-the pointer is the controller of the memory space. Different types of pointers have different memory management capabilities. Such as int * a and

Char * A manages the memory in the same way. Further understanding, you can jump to the struct variable pointer (class variable pointer), each having a variety of memory management methods.

If you can understand that the pointer is a memory controller, you can understand the implementation of the linked list. The linked list will be detailed in another blog.

For further analysis of pointers and memory, I am also posting it to another blog ~~ So far ~~

Related Article

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.