C / C ++ to use two pointers

Require special attention when using the pointer, we believe it will cause a memory access violation, resulting in the application flash back. Especially when using a multi-stage when the pointer should pay attention to this issue. Below an example two pointers proper use two hands, as well as the use of two exemplary error pointers.

 

For example: a string requires two types of pointer to the program used.

1. Only a pointer to allocate memory for

This method is only for an allocated memory, then a pointer to the address pointer assigned two objects.

 

Note : pstrs value is strs address can not be used the following methods to access strs array.

 

Such access only on the first cycle access to strs [0] object after pstrs [1] ~ pstrs [4 ] are illegal access, the program will result in an exception.

 

2. for each pointer is assigned a memory

This method is assigned a pointer for each memory, as follows:

 

strs corresponds to a type of STD :: String * , of length n array. Therefore, to each strs [i] allocate memory, but also a pointer for each memory recall. The following example does not give an allocated memory to be accessed exception is generated. At this time, the reason is not yet to strs [i] allocate memory, strs [i] is stored is not necessarily a std :: string type variables, exception is generated.

 

Abnormal follows:

 

Guess you like

Origin www.cnblogs.com/shichangxing/p/11427336.html