N维阵列的存储器布局选择

时间:2014-01-15 08:52:46

标签: c arrays memory-layout

我开始为n维向量编写一个库。

我想在此支持一些设计功能 库:

  • 内存布局是连续的。这样,共享内存,通过网络发送和保存到文件只是传递指针的问题。
  • 不同的维度可以支持不同的元素大小(即[0][0]可能是1字节,而[1][0]可能是2字节。

在绘制出一些内存布局的表示之后 看起来像某些尺寸的矢量,我意识到了 布局结构有两种方法:

这是第一种方式。两种表示都是可视化的 a [2] [4] [3]数组。

 ---------------------------
| index             | 0:0:0 |
| elementSize       |       |
| elementCount      |       |
| expansionBoundary |       |
| slotCount         |       |
 ---------------------------
 ---------------------------
| index             | 1:0:0 |
| elementSize       |       |
| elementCount      |       |
| expansionBoundary |       |
| slotCount         |       |
 ---------------------------
     ---------------------------
    | index             | 0:0:0 |
    | elementSize       |       |
    | elementCount      |       |
    | expansionBoundary |       |
    | slotCount         |       |
     ---------------------------
     ---------------------------
    | index             | 0:1:0 |
    | elementSize       |       |
    | elementCount      |       |
    | expansionBoundary |       |
    | slotCount         |       |
     ---------------------------
     ---------------------------
    | index             | 0:2:0 |
    | elementSize       |       |
    | elementCount      |       |
    | expansionBoundary |       |
    | slotCount         |       |
     ---------------------------
     ---------------------------
    | index             | 0:3:0 |
    | elementSize       |       |
    | elementCount      |       |
    | expansionBoundary |       |
    | slotCount         |       |
     ---------------------------
     ---------------------------
    | index             | 1:0:0 |
    | elementSize       |       |
    | elementCount      |       |
    | expansionBoundary |       |
    | slotCount         |       |
     ---------------------------
     ---------------------------
    | index             | 1:1:0 |
    | elementSize       |       |
    | elementCount      |       |
    | expansionBoundary |       |
    | slotCount         |       |
     ---------------------------
     ---------------------------
    | index             | 1:2:0 |
    | elementSize       |       |
    | elementCount      |       |
    | expansionBoundary |       |
    | slotCount         |       |
     ---------------------------
     ---------------------------
    | index             | 1:3:0 |
    | elementSize       |       |
    | elementCount      |       |
    | expansionBoundary |       |
    | slotCount         |       |
     ---------------------------
         ---------------------------
        | Index             | 0:0:0 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 0:0:1 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 0:0:2 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 0:1:0 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 0:1:1 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 0:1:2 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 0:2:0 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 0:2:1 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 0:2:2 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 0:3:0 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 0:3:1 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 0:3:2 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 1:0:0 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 1:0:1 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 1:0:2 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 1:1:0 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 1:1:1 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 1:1:2 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 1:2:0 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 1:2:1 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 1:2:2 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 1:3:0 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 1:3:1 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 1:3:2 |
        | Element           |       |
         ---------------------------

这是第二种方式。

 ---------------------------
| index             | 0:0:0 |
| elementSize       |       |
| elementCount      |       |
| expansionBoundary |       |
| slotCount         |       |
 ---------------------------
     ---------------------------
    | index             | 0:0:0 |
    | elementSize       |       |
    | elementCount      |       |
    | expansionBoundary |       |
    | slotCount         |       |
     ---------------------------
         ---------------------------
        | Index             | 0:0:0 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 0:0:1 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 0:0:2 |
        | Element           |       |
         ---------------------------
     ---------------------------
    | index             | 0:1:0 |
    | elementSize       |       |
    | elementCount      |       |
    | expansionBoundary |       |
    | slotCount         |       |
     ---------------------------
         ---------------------------
        | Index             | 0:1:0 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 0:1:1 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 0:1:2 |
        | Element           |       |
         ---------------------------
     ---------------------------
    | index             | 0:2:0 |
    | elementSize       |       |
    | elementCount      |       |
    | expansionBoundary |       |
    | slotCount         |       |
     ---------------------------
         ---------------------------
        | Index             | 0:2:0 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 0:2:1 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 0:2:2 |
        | Element           |       |
         ---------------------------
     ---------------------------
    | index             | 0:3:0 |
    | elementSize       |       |
    | elementCount      |       |
    | expansionBoundary |       |
    | slotCount         |       |
     ---------------------------
         ---------------------------
        | Index             | 0:3:0 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 0:3:1 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 0:3:2 |
        | Element           |       |
         ---------------------------
 ---------------------------
| index             | 1:0:0 |
| elementSize       |       |
| elementCount      |       |
| expansionBoundary |       |
| slotCount         |       |
 ---------------------------
     ---------------------------
    | index             | 1:0:0 |
    | elementSize       |       |
    | elementCount      |       |
    | expansionBoundary |       |
    | slotCount         |       |
     ---------------------------
         ---------------------------
        | Index             | 1:0:0 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 1:0:1 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 1:0:2 |
        | Element           |       |
         ---------------------------
     ---------------------------
    | index             | 1:1:0 |
    | elementSize       |       |
    | elementCount      |       |
    | expansionBoundary |       |
    | slotCount         |       |
     ---------------------------
         ---------------------------
        | Index             | 1:1:0 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 1:1:1 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 1:1:2 |
        | Element           |       |
         ---------------------------
     ---------------------------
    | index             | 1:2:0 |
    | elementSize       |       |
    | elementCount      |       |
    | expansionBoundary |       |
    | slotCount         |       |
     ---------------------------
         ---------------------------
        | Index             | 1:2:0 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 1:2:1 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 1:2:2 |
        | Element           |       |
         ---------------------------
     ---------------------------
    | index             | 1:3:0 |
    | elementSize       |       |
    | elementCount      |       |
    | expansionBoundary |       |
    | slotCount         |       |
     ---------------------------
         ---------------------------
        | Index             | 1:3:0 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 1:3:1 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 1:3:2 |
        | Element           |       |
         ---------------------------

选择一种布局比另一种布局有什么优点/缺点吗?

0 个答案:

没有答案