使用Armadillo c++在声明后使用辅助内存创建vec
Creating vec with auxiliary memory after declaration with Armadillo c++
我正在使用c++中的犰狳矩阵库,我想创建一个使用"辅助内存"的vec。标准的方法是
vec qq(6); qq<<1<<2<<3<<4<<5<<6;
double *qqd = qq.memptr();
vec b1(qqd, 6, false);
所以这里,如果我改变b1中的元素,qq中的元素也会改变,这就是我想要的。然而,在我的程序中,我全局声明了b1
向量,所以当我定义它时,我不能调用使b1
使用"辅助内存"的构造函数。armadillo中有我想要的功能吗?为什么我得到不同的结果,当我运行下面的代码?
vec b1= vec(qq.memptr(), 3, false); //changing b1 element changes one of qq's element
vec b1;
b1= vec(qq.memptr(), 3, false); //changing b1 element does not chagne qq's
那么,当它被声明为全局时,我如何使一个向量使用来自另一个向量的内存?
使用全局变量通常不是一个好主意。
然而,如果你坚持使用它们,这里有一种使用全局Armadillo向量的辅助内存的可能方法:
#include <armadillo>
using namespace arma;
vec* x; // global declaration as a pointer; points to garbage by default
int main(int argc, char** argv) {
double data[] = { 1, 2, 3, 4 };
// create vector x and make it use the data array
x = new vec(data, 4, false);
vec& y = (*x); // use y as an "easier to use" alias of x
y.print("y:");
// as x is a pointer pointing to an instance of the vec class,
// we need to delete the memory used by the vec class before exiting
// (this doesn't touch the data array, as it's external to the vector)
delete x;
return 0;
}
相关文章:
- 如何在C++类内存结构中创建"spacer"?
- 使用Boost Interprocess创建托管共享内存需要很长时间
- 在共享缓冲区内存中创建 ::std::string 对象
- 当注入程序的内存空间时,如何读取从 0 到 0xFFFFFFFFF 的每个字节?我正在创建模式扫描仪
- 如果不分配内存,我如何能够为变量创建和分配值?
- 此函数是否会在C++中创建内存泄漏?
- 如何在不使用 "new" 关键字的情况下解除分配创建的对象的内存?
- 在 C/C++ 中,是否可以通过使用指针更改"important"内存地址的值来创建简单的恶意软件?
- 如何以编程方式区分在内存的堆和堆栈部分创建的对象
- 是否可以在专用内存空间中分配一个为提升管理共享内存而创建的对象
- 我正在尝试创建一个C++映射,该映射在boost内存映射文件中具有向量值
- 如何创建非 POD 类型的连续内存池?
- 当我们在C++中创建类的对象时,为成员函数分配的内存在哪里?
- 使用加速进程间创建消息队列 - 内存访问冲突
- 如何从userland中的分段内存创建映射
- 从内存创建DDS纹理
- 从常数内存创建输入流
- 从内存创建变量
- 使用Armadillo c++在声明后使用辅助内存创建vec
- C++:使用动态内存创建一个动态数组