sách gpt4 ăn đã đi

c++ - 为什么 Eigen 矩阵到 C 数组的转换会为前两个索引提供垃圾值?

In lại 作者:太空狗 更新时间:2023-10-29 23:01:23 33 4
mua khóa gpt4 giày nike

我有一个要转换为 C 数组的特征矩阵。我可以使用以下示例重现该问题。

#include 
#include

int *test()
{
Eigen::MatrixXi arr = Eigen::MatrixXi::Ones(6,1);
// just to check
arr(4)=3;
arr(5)=19;
return arr.data();
}

int chính()
{
int *c_arr;
c_arr = test();

for (int i=0; i<6;++i)
{
std::cout << c_arr[i] << std::endl;
}

trả về 0;
}

Đầu ra:

0
0
1
1
3
19

现在,如果我从 Bài kiểm tra 函数中打印转换后的 C 数组值,则这些值是正确的。但是,如果我打印 chủ yếu 中的值(如上所示),前两个索引始终是垃圾。所以我想知道函数调用中发生了什么?我用不同的特征矩阵(类型、大小)尝试过这个,我得到了相同的结果。

câu trả lời hay nhất

首先我会说我不是 100% 熟悉 Eigen 库(只是出于好奇下载它来查看它)并且它的文档有点缺乏但是你的问题是一个基本的 C 问题可以是有几种补救方法。

首先,我们将首先解释您的代码中发生了什么以提供垃圾值:

int *test()
{
/* create an auto scoped variable on the stack;
this variable is only "visible" to this function
and any references to it or it's underlying data
outside the scope of this function will result
in "undefined behaviour" */
Eigen::MatrixXi arr = Eigen::MatrixXi::Ones(6,1);
arr(4)=3;
arr(5)=19;
/* arr.data() is defined as returning a pointer to the scalar underlying type (or
a C-style array in other words). Regardless of the type being returned, it is pointer based
and you are returning a pointer to a location in memory, not the actual data being held in
the memory. */
return arr.data();
} /* the variable arr is destroyed here since we left function scope and the return value (the pointer location)
is put in the return register and "program flow" is returned back to the main function where the pointer being
returned now points to "invalid" memory */

int chính()
{
int *c_arr; // create a pointer type that can reference int types
c_arr = test(); // point it to the result of the test function (see notes above)
/* c_arr now points to a memory location returned from test, but since the
arr variable no longer exists here, when you go through and print the values pointed
to at those memory locations you will get what is at those locations and could be "anything"
except a valid reference to the original arr variable and it's underlying data. */

for (int i=0; i<6;++i)
{
std::cout << c_arr[i] << std::endl;
}

trả về 0;
}

所以这就是lý do,至于如何解决它,有几种方法可以解决您的问题;一种是将返回数组作为变量传递给您的 Bài kiểm tra 函数(例如 void test(int*& val)),然后您可以选择分配新内存到测试函数中的变量,或者假设用户已经这样做了,并且还必须假设用户将自己清理并调用 delete[](不仅仅是 delete 因为你在操作数据数组)。

但这有很多需要知道要分配多少空间并确保在完成后释放的警告。我不确定为什么你特别需要一个 C 风格的数组,但由于你使用的是 C++,如果你使用一些可用的 STL 和容器函数来帮助你,可能会更谨慎,例如:

#include 
#include
#include

std::vector test()
{
Eigen::MatrixXi arr = Eigen::MatrixXi::Ones(6,1);
arr(4)=3;
arr(5)=19;
// we need the size so we know how big of a container to allocate
std::size_t sz = arr.innerSize() * arr.outerSize();
std::vector ret(sz);
// get a temporary C array pointer so we can reference the data
int* tmp = arr.data();
// copy from tmp[0] to tmp[sz] and insert the data into the first element of ret
std::copy(tmp, tmp+sz, ret.begin());
// return the (copied) data
return ret;
}

int chính()
{
std::vector c_arr = test();
// c_arr now points to valid data it holds and can be iterated on
for (std::size_t i = 0; i < c_arr.size(); ++i) {
std::cout << c_arr[i] << std::endl;
}
// if you need a C-style array from here, you can easily copy the data
// from the vector to your C-array
trả về 0;
}

我研究了使用 cast()类的功能,但无法完全弄清楚使它比仅以上述方式复制它更不痛苦的语法,因为看起来您必须将 dàn diễn viên 函数调用到不同的 Eigen 类型,然后从那里再次转换,但要知道有一个 dàn diễn viên 函数和其他方法可以在需要时获取 MatrixX 类的基础数据访问它。

Hy vọng điều này sẽ giúp.

关于c++ - 为什么 Eigen 矩阵到 C 数组的转换会为前两个索引提供垃圾值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31363571/

33 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress