(摘)C++ Primer Plus, Fourth Edition---Using a Dynamic Array
2006-09-11 17:28
513 查看
After you create a dynamic array, how do you use it? First, think about the problem conceptually. The statement
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int * psome = new int [10]; // get a block of 10 ints
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
creates a pointer psome that points to the first element of a block of ten int values. Think of it as a finger pointing to that element. Suppose an int occupies four bytes. Then, by moving your finger four bytes in the correct direction, you can point to the second element. Altogether, there are ten elements, which is the range over which you can move your finger. Thus, the new statement supplies you with all the information you need to identify every element in the block.
Now think about the problem practically. How do you access one of these elements? The first element is no problem. Because psome points to the first element of the array, *psome is the value of the first element. That leaves nine more elements to access. The simplest way may surprise you if you haven't worked with C: Just use the pointer as if it were an array name. That is, you can use psome[0] instead of *psome for the first element, psome[1] for the second element, and so on. It turns out to be very simple to use a pointer to access a dynamic array, even if it may not immediately be obvious why the method works. The reason you can do this is that C and C++ handle arrays internally by using pointers anyway. This near equivalence of arrays and pointers is one of the beauties of C and C++. We'll elaborate on this equivalence in a moment. First, Listing 4.13 shows how you can use new to create a dynamic array and then use array notation to access the elements. It also points out a fundamental difference between a pointer and a true array name.
Listing 4.13 arraynew.cpp
Here is the output:
As you can see, arraynew.cpp uses the pointer p3 as if it were the name of an array, with p3[0] as the first element, and so on. The fundamental difference between an array name and a pointer shows in the following line:
You can't change the value of an array name. But a pointer is a variable, hence you can change its value. Note the effect of adding 1 to p3. The expression p3[0] now refers to the former second element of the array. Thus adding 1 to p3 causes it to point to the second element instead of the first. Subtracting one takes the pointer back to its original value so that the program can provide delete [] with the correct address.
The actual addresses of consecutive ints typically differ by two or four bytes, so the fact that adding 1 to p3 gives the address of the next element suggests that there is something special about pointer arithmetic. There is.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int * psome = new int [10]; // get a block of 10 ints
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
creates a pointer psome that points to the first element of a block of ten int values. Think of it as a finger pointing to that element. Suppose an int occupies four bytes. Then, by moving your finger four bytes in the correct direction, you can point to the second element. Altogether, there are ten elements, which is the range over which you can move your finger. Thus, the new statement supplies you with all the information you need to identify every element in the block.
Now think about the problem practically. How do you access one of these elements? The first element is no problem. Because psome points to the first element of the array, *psome is the value of the first element. That leaves nine more elements to access. The simplest way may surprise you if you haven't worked with C: Just use the pointer as if it were an array name. That is, you can use psome[0] instead of *psome for the first element, psome[1] for the second element, and so on. It turns out to be very simple to use a pointer to access a dynamic array, even if it may not immediately be obvious why the method works. The reason you can do this is that C and C++ handle arrays internally by using pointers anyway. This near equivalence of arrays and pointers is one of the beauties of C and C++. We'll elaborate on this equivalence in a moment. First, Listing 4.13 shows how you can use new to create a dynamic array and then use array notation to access the elements. It also points out a fundamental difference between a pointer and a true array name.
Listing 4.13 arraynew.cpp
// arraynew.cpp _ using the new operator for arrays
#include <iostream>
using namespace std;
int main()
![]()
...{
double * p3 = new double [3]; // space for 3 doubles
p3[0] = 0.2; // treat p3 like an array name
p3[1] = 0.5;
p3[2] = 0.8;
cout << "p3[1] is " << p3[1] << ". ";
p3 = p3 + 1; // increment the pointer
cout << "Now p3[0] is " << p3[0] << " and ";
cout << "p3[1] is " << p3[1] << ". ";
p3 = p3 - 1; // point back to beginning
delete [] p3; // free the memory
return 0;
}
Here is the output:
p3[1] is 0.5. Now p3[0] is 0.5 and p3[1] is 0.8.
As you can see, arraynew.cpp uses the pointer p3 as if it were the name of an array, with p3[0] as the first element, and so on. The fundamental difference between an array name and a pointer shows in the following line:
p3 = p3 + 1; // okay for pointers, wrong for array names
You can't change the value of an array name. But a pointer is a variable, hence you can change its value. Note the effect of adding 1 to p3. The expression p3[0] now refers to the former second element of the array. Thus adding 1 to p3 causes it to point to the second element instead of the first. Subtracting one takes the pointer back to its original value so that the program can provide delete [] with the correct address.
The actual addresses of consecutive ints typically differ by two or four bytes, so the fact that adding 1 to p3 gives the address of the next element suggests that there is something special about pointer arithmetic. There is.
相关文章推荐
- c++ primer plus阅读笔记10---定位new using namespace
- 《C++ Primer Plus》03vector,array,数组的区别
- C++ Primer plus 学习笔记之 数组、array和vector异同点
- C++ Primer Plus (第6版)课后习题答案 第二章
- C++primer plus第六版课后编程题答案16.7
- C++primer plus第六版课后编程题答案17.5
- C++ Primer plus 学习笔记之第九章内存模型和名称空间(1)
- C++ Primer Plus学习:第十一章 使用类(1)
- 【10.1】c++ primer plus 课后编程答案
- C++ Primer Plus 学习笔记(第八章)
- C++ Primer Plus学习:第十二章 类和动态内存(1)
- C++ Primer Plus第六版编程练习12.2解答
- c++ primer plus 第六版第二章编程练习答案
- c++ primer plus阅读笔记8---decltype
- C++ Primer Plus第六版 第八章 编程练习答案
- 【C++】primer plus 练习7.1
- C++ primer plus 6th ed. 中文版读书笔记【第二章】
- 【C++】primer plus 练习7.3
- c++primer plus第七章编程练习的解答1-8
- C++ Primer Plus编程练习 7.13-1