array地址问题(为何指向赋值,仍然地址不同)
2015-12-25 11:39
316 查看
代码quote:c++ primer plus ---- example 4.24
#include <iostream>
#include <vector>
#include <array>
int main(int argc, const char * argv[]) {
// insert code here...
using namespace std;
double a1[4] = {1.2, 2.4, 3.6, 4.8};
vector<double> a2(4);
a2[0] = 1.0/3.0;
a2[1] = 1.0/5.0;
a2[2] = 1.0/7.0;
a2[3] = 1.0/9.0;
array<double, 4> a3 = {3.14, 2.72, 1.62, 1.41};
array<double, 4> a4;
a4 = a3;
cout << "a1[2]: " << a1[2] << " at " << &a1[2] << endl;
cout << "a2[2]: " << a2[2] << " at " << &a2[2] << endl;
cout << "a3[2]: " << a3[2] << " at " << &a3[2] << endl;
cout << "a4[2]: " << a4[2] << " at " << &a4[2] << endl;
a1[-2] = 20.2;
cout << " a1[-2]: " << a1[-2] << " at " << &a1[-2] << endl;
cout << " a3[2]: " << a3[2] << " at " << &a3[2] << endl;
cout << " a4[2]: " << a4[2] << " at " << &a4[2] << endl;
return 0;
}
计算机结果
a1[2]: 3.6 at 0x7fff5fbff7f0
a2[2]: 0.142857 at 0x100105520
a3[2]: 1.62 at 0x7fff5fbff648
a4[2]: 1.62 at 0x7fff5fbff628
a1[-2]: 20.2 at 0x7fff5fbff7d0
a3[2]: 1.62 at 0x7fff5fbff648
a4[2]: 1.62 at 0x7fff5fbff628
Program ended with exit code: 0
编译环境:mac os, xcode 7.1.1
为何array a4 指向 array a3,地址不同。
猜想:c++重载了 = , 当a4 = a3的时候,识别类别array,进行赋值操作,因而指针可以不同,在array<type , n_element>的时候已经初始化了
http://stackoverflow.com/questions/34459937/how-class-array-workc-does-it-get-a-memory-while-declaration?noredirect=1#comment56662812_34459937
#include <iostream>
#include <vector>
#include <array>
int main(int argc, const char * argv[]) {
// insert code here...
using namespace std;
double a1[4] = {1.2, 2.4, 3.6, 4.8};
vector<double> a2(4);
a2[0] = 1.0/3.0;
a2[1] = 1.0/5.0;
a2[2] = 1.0/7.0;
a2[3] = 1.0/9.0;
array<double, 4> a3 = {3.14, 2.72, 1.62, 1.41};
array<double, 4> a4;
a4 = a3;
cout << "a1[2]: " << a1[2] << " at " << &a1[2] << endl;
cout << "a2[2]: " << a2[2] << " at " << &a2[2] << endl;
cout << "a3[2]: " << a3[2] << " at " << &a3[2] << endl;
cout << "a4[2]: " << a4[2] << " at " << &a4[2] << endl;
a1[-2] = 20.2;
cout << " a1[-2]: " << a1[-2] << " at " << &a1[-2] << endl;
cout << " a3[2]: " << a3[2] << " at " << &a3[2] << endl;
cout << " a4[2]: " << a4[2] << " at " << &a4[2] << endl;
return 0;
}
计算机结果
a1[2]: 3.6 at 0x7fff5fbff7f0
a2[2]: 0.142857 at 0x100105520
a3[2]: 1.62 at 0x7fff5fbff648
a4[2]: 1.62 at 0x7fff5fbff628
a1[-2]: 20.2 at 0x7fff5fbff7d0
a3[2]: 1.62 at 0x7fff5fbff648
a4[2]: 1.62 at 0x7fff5fbff628
Program ended with exit code: 0
编译环境:mac os, xcode 7.1.1
为何array a4 指向 array a3,地址不同。
猜想:c++重载了 = , 当a4 = a3的时候,识别类别array,进行赋值操作,因而指针可以不同,在array<type , n_element>的时候已经初始化了
http://stackoverflow.com/questions/34459937/how-class-array-workc-does-it-get-a-memory-while-declaration?noredirect=1#comment56662812_34459937
相关文章推荐
- 修改Bundle identify引起的问题
- ASM创建spfile失败 $ORACLE_HOME/bin/oracle
- servlet中文乱码拦截器
- oracle 常用函数
- bootstrap-glyphicons图标
- 一知半见的load与get
- 为activity设置主题theme
- Joomla反序列化漏洞的查漏补缺
- Android项目重构之路:架构篇
- LeetCode---Sudoku Solver
- #!bin/bash
- MySQL 触发器简单实例
- 调用第三方应用导航
- 基于jquery实现图片相关操作(重绘、获取尺寸、调整大小、缩放)
- 视觉设计-CRUD
- 行为型:设计模式之备忘录模式(二十)
- ubuntu 安装Jenkins
- 配置apt-get高速下载源
- Android Style开发之Theme
- VS2010命令行编译C#和VC项目