Dud*_*y01 2 c++ memory arrays pybind11
我正在编写一个使用 C++ 进行计算的应用程序,然后使用 pybind11 将多维结果作为 numpy 数组返回。从pybind的文档和在线看到的示例来看,numpy 数组创建基本上是传递数据数组的指针并包含步幅的详细信息。然而,在 C++ 部分,我并不热衷于使用一维array和使用一些花哨的索引,但我宁愿使用结构。这让我思考是否可以将放置在连续内存中的(同质)变量视为array.
我的思路如下。an 的元素array被放置在连续内存中。a 的元素struct也按其声明的顺序连续放置(当不涉及填充时)。因此,从内存放置的角度来看,以下四个变量声明是相同的,例如,如果我要指向第一个元素的指针,那么我可以通过一次执行一个整数值的步骤来遍历所有元素:
struct struct_array
{
int elem[4] = {};
};
struct struct_ints
{
int a = {};
int b = {};
int c = {};
int d = {};
};
// integer matrix of shape 3x4
int one_dim_array[3 * 4] = {};
int two_dim_array[3][4] = {};
struct_array array_of_struct_arrays[3] = {};
struct_ints array_of_struct_ints[3] = {};
Run Code Online (Sandbox Code Playgroud)
这是我的测试代码,表明我的问题的答案是肯定的。它执行一些地址打印、设置和读取元素。
#include <iostream>
struct struct_array
{
int elem[4] = {};
};
struct struct_ints
{
int a = {};
int b = {};
int c = {};
int d = {};
};
int main(void)
{
const int rows = 3;
const int cols = 4;
int one_dim_array[rows * cols] = {};
int two_dim_array[rows][cols] = {};
struct_array array_of_struct_arrays[rows] = {};
struct_ints array_of_struct_ints[rows] = {};
std::cout << sizeof(int) << " is the size of an int in bytes\n";
std::cout << "\nOne dim array\n";
for (int i = 0; i < 12; ++i)
{
one_dim_array[i] = i;
std::cout << &one_dim_array[i] << "\n";
}
std::cout << "\nTwo dim array\n";
for (int i = 0; i < rows; ++i)
{
for (int j = 0; j < cols; ++j)
{
two_dim_array[i][j] = i * cols + j;
std::cout << &two_dim_array[i][j] << "\n";
}
}
std::cout << "\nArray of struct arrays\n";
for (int i = 0; i < rows; ++i)
{
for (int j = 0; j < cols; ++j)
{
array_of_struct_arrays[i].elem[j] = i * cols + j;
std::cout << &array_of_struct_arrays[i] << " " << &array_of_struct_arrays[i].elem[j] << "\n";
}
}
std::cout << "\nArray of struct ints\n";
for (int i = 0; i < rows; ++i)
{
array_of_struct_ints[i].a = i * cols + 0;
array_of_struct_ints[i].b = i * cols + 1;
array_of_struct_ints[i].c = i * cols + 2;
array_of_struct_ints[i].d = i * cols + 3;
std::cout << &array_of_struct_ints[i] << " " << &array_of_struct_ints[i].a << "\n";
std::cout << &array_of_struct_ints[i] << " " << &array_of_struct_ints[i].b << "\n";
std::cout << &array_of_struct_ints[i] << " " << &array_of_struct_ints[i].c << "\n";
std::cout << &array_of_struct_ints[i] << " " << &array_of_struct_ints[i].d << "\n";
}
for (int i = 0; i < 4; ++i)
{
// Maybe using a reinterpret_cast would be more modern
void *void_p = nullptr;
switch (i)
{
case 0:
void_p = &one_dim_array;
std::cout << "\nOne dim array\n";
break;
case 1:
void_p = &two_dim_array;
std::cout << "\nTwo dim array\n";
break;
case 2:
void_p = &array_of_struct_arrays;
std::cout << "\nArray of struct arrays\n";
break;
case 3:
void_p = &array_of_struct_ints;
std::cout << "\nArray of struct ints\n";
}
int *int_p = (int *)void_p;
for (int i = 0; i < 12; ++i)
{
std::cout << *(int_p + i) << "\n";
}
}
std::cout << "Hello world!";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是正确的还是我错过了什么?你对这个问题有什么想法?(除此之外,我应该切换到 std::array。)谢谢您的时间!
您不知道您的变量在内存中是连续放置的。您的源代码可能看起来像那样,但仅此而已。
如果你想让你的变量表现得像一个数组,那么使用一个数组。
该语言没有提供使用指针算术从另一个变量到达变量的定义方法,除非两者都是同一数组中的元素。