[C ++ learning diary] -07- pointer

pointer

Each variable has a memory location, each memory location addresses are defined using a hyphen (&) operator access, it represents an address in memory.

Consider the following examples:

int score = 5;
cout << &score << endl;

//输出 "0x29fee8"

This will address the definition of output variables. .


What is Pointer

Pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location.

All pointers actual data type value, whether integer, float, string, or other data types are the same, is a long hexadecimal memory address of the representative.

Note: The only difference between the different types of data pointers, the pointer points to a different constant or variable data type.
A pointer is a variable, just like other variables or constants, you must address before using other variables pointer storage, be declared.


Pointer declaration

The asterisk * is used to declare a pointer, the asterisk is used in the same multiplication. However, in this statement, the asterisk is used to specify a variable is a pointer.

The following is a valid pointer declaration:

int    *ip;    /* 一个整型的指针 */
double *dp;    /* 一个 double 型的指针 */
float  *fp;    /* 一个浮点型的指针 */
char   *ch;    /* 一个字符型的指针 */

Like variables, we give the name and a pointer to a pointer to the definition of this type.

Note: An asterisk next to the data type or variable name can be placed, can also be placed in the middle.


Use pointer

Here, we will assign a variable address pointer:

int score = 5;
int *scorePtr;
scorePtr = &score;

cout << scorePtr << endl;

//输出 "0x29fee8"

The above code declares a pointer to a pointer integer scorePtr, using the symbol & (address) operator for assignment memory location score variables.

Now, the value is the score scorePtr memory location.


Pointer operator

In C ++, there are two operators pointers:

The address operator (&): returns the memory address of the operand.

Dereferencing operator (*): Returns the number of manipulated variable value is specified by the address.

Below is an example:

int var = 30;
int *p;
p = &var;

cout << var << endl;
// 输出 30 (var的值)

cout << p << endl;
// 输出 0x28ff18 (var的内存位置)

cout << *p << endl;
/* 输出 30 (变量的值存储在指针p中) */

Note: An asterisk (*) is used to declare a pointer, a pointer is used to indicate that it (the asterisk is its complex specifier type part) of the simple object. Please do not get the value at the specified address is used to dereference operator confusion. They are just two different things represented with the same symbols.


Dynamic Memory

Understand the dynamic memory in C ++ is how it works is to become a qualified C ++ programmers essential.

C ++ program memory is divided into two parts:

Stack: all variables declared inside a function will take up the stack memory.
Heap: This is the program memory is not used, the program runs can be used to dynamically allocate memory.
In C ++, you can use the new operator for a given type of variable to allocate memory in the heap at run time, it returns the address of the allocated space.

Here is the new operator to the general syntax of any dynamically allocated data types:

new data-type;

Here, data-type may be any built-in data types, including arrays, may be any type of data structure or class comprises including user-defined.

Below is an example:

new int;

This allocated memory size needed to store an integer in the heap, and returns the address.


Dynamic Memory Access

In C ++, the address assignment may be stored in a pointer, then the solution can lead to access variables.

Below is an example:

int *p = new int;
*p = 10;

We are an integer dynamically allocate memory, and assign 10.

Pointer p as a local variable stored in the stack, heap and save its value to assign addresses. 10 address value stored in the stack.


Release memory

For the stack of local variables, memory management is performed automatically.

At any time, when you feel that a variable has been dynamically allocated memory is no longer needed, you can use the delete operator to release the memory it used. As follows:

delete pvalue;        // 释放 pvalue 所指向的内存

Below is an example:

int *p = new int; // 为变量请求内存
*p = 10; // 在分配的地址存储值

cout << *p << endl; 

delete p; // 释放内存

Tip: Forget free the memory allocated using the new keyword will cause a memory leak, because the memory remains allocated until the program is closed.


Dangling pointer

In C ++, delete operator to release the memory allocated to a variable, but does not delete the pointer itself, because pointers are stored in the stack.

Points to memory location pointer does not exist is called dangling pointer.

Below is an example:

int *p = new int; // 请求内存
*p = 10; // 存储值

delete p; // 释放内存
// 现在p是一个悬空指针

p = new int; // 重新使用一个新的地址

prompt:

Is a NULL pointer value of zero constants defined in several standard library (including the iostream) in.

When you declare it, will be assigned to a NULL pointer variable is a good habit, in case you do not have the exact address assignments. NULL pointer is assigned is called a null pointer. For example: int * PTR =
NULL;


Dynamic memory allocated for the array.

Below is an example:

	int *p = NULL; // 指针初始化为null
	p = new int[30]; // 请求内存
	p[0] = 1; //对数组下标为0的进行赋值
	cout << p[0];//输出
	delete [] p; // 删除由p指向的数组

Dynamic memory allocation is useful in many situations, such as when the program depends on the input. For example, when your application needs to read an image file, it does not know in advance the desired image file size and storage memory.

When array of objects freed with delete, be careful not to lose the symbol '[]'


Small exercises

1. Declare a pointer "ptr" is an int, and the value of the address assigned var

2. Statement two variables a and b, pa and pb two pointers point to a and b. Then the variables are assigned to the output through pa and pb


Recommended Reading C / C ++ pointer Detailed

Previous [C ++ learning diary] -06- array

Published 12 original articles · won praise 12 · views 5408

Guess you like

Origin blog.csdn.net/qq_18604209/article/details/103991207