可以将连续放置在内存中的变量视为数组的一部分吗?

问题描述

我正在编写一个使用C ++进行计算的应用程序,然后使用pybind11将多维结果作为numpy数组返回。从pybind的documentation到在线上看到的examples,numpy数组的创建基本上是通过数据数组的指针并在步幅上包含细节。但是,在C ++部分中,我并不热衷于使用一维array并使用一些奇特的索引编制,而是希望使用结构。那让我开始思考放置在连续内存中的(同质)变量是否可以被视为array的一部分。

我的思路如下。 array的元素放置在连续内存中。 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] = {};

这是我的测试代码提示我的问题的答案为是。它执行一些地址打印,设置和读取元素。

#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;
}

这是对的还是我缺少什么?您对此事有何看法? (除此之外,我应该切换到std :: array。) 谢谢您的时间!

解决方法

您不知道变量是否连续放置在内存中。您的源代码可能会像这样看起来,但这已经足够了。

如果希望变量的行为类似于数组,请使用数组。

该语言没有提供使用指针算术从另一个变量中获取变量的定义方式,除非这两个元素都在同一数组中。