对于连续二维数组分配内存 [英] Assigning memory for contiguous 2D array

查看:148
本文介绍了对于连续二维数组分配内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个名为时,分配的连续内存的维数组的通用函数。目标是实现类似下面

I am trying to create a generic function which when called allocated contiguous memory for a dimensional array. Goal is to achieve something like below

在这里输入的形象描述

所以要实现它 - 方程我现在用的就是

so to achieve it - equation I am using is

Type **pArray;
int total_elements = ((rows * cols) + rows); 
pArray = (Type **) malloc(total_elements * sizeof(Type));

我也搞不清楚就访问元素的一部分。我发现很难想象如何低于code将填补上述数组中的元素

I am also confused with respect to accessing elements part. I am finding it hard to visualize how below code will fill the elements of above array

for (row = 0; row < dim0; ++row)
   {
      for (col = 0; col < dim1; ++col)
      {
         /* For this to work Type must be a 1D array type. */
         for (ix = 0; ix < (int)(sizeof(item)/sizeof(item[0])); ++ix)
         {
            /*            printf("%4d\n", testValue); */
            ppObj[row][col][ix] = testValue;
            if (testValue == SCHAR_MAX)
               testValue = SCHAR_MIN;
            else
               ++testValue;
         }
      }
   } 

目标是的的下面创建阵列格式

Goal is not to create below array format

在这里输入的形象描述

推荐答案

这是行不通的。你假设你的键入* 的大小与同你的键入这是大多数的时间并不属实。但是,你有什么需要的行指针,反正?我的第一个实现的想法是这样的:

This won't work. You assume your Type * has the same size as your Type which is most of the time not true. But, what do you need the row pointers for, anyway? My first implementation idea would be something like this:

typedef struct TypeArray
{
    size_t cols;
    Type element[];
} TypeArray;

TypeArray *TypeArray_create(size_t rows, size_t cols)
{
    TypeArray *self = calloc(1, sizeof(TypeArray) + rows * cols * sizeof(Type));
    self->cols = cols;
    return self;
}

使用例如写getter和setter 自&gt;元素[*行自&GT; COLS +行。

这里经过这次讨论它的可行的是这样的:

[edit]: Following this discussion here it is doable like this:

typedef long long Type;


Type **createArray(size_t rows, size_t cols)
{
    size_t r;

    /* allocate chunk: rows times the pointer, rows * cols times the value */
    Type **array = malloc(rows * sizeof(Type *) + rows * cols * sizeof(Type));

    /* calculate pointer to first row: point directly behind the pointers,
     * then cast */
    Type *row = (Type *) (array + rows);

    /* set all row pointers */
    for (r = 0; r < rows; ++r)
    {
        array[r] = row;
        row += cols;
    }

    return array;
}

用法可能是这样的:

Usage could look like this:

int main()
{
    Type **array = createArray(3, 4);

    for (int r = 0; r < 3; ++r)
    {
        for (int c = 0; c < 4; ++c)
        {
            array[r][c] = (r+1) * (c+1);
        }
    }
    for (int r = 0; r < 3; ++r)
    {
        for (int c = 0; c < 4; ++c)
        {
            printf("array[%d][%d] = %lld\n", r, c, array[r][c]);
        }
    }

    free(array);
    return 0;
}

这假定没有类型需要比数据指针更大的调整,否则,你将不得不计算的填充字节的量的三分球后插入。为了安全起见,你可以使用的sizeof(类型)和一些模数计算(使用的char *插入填充字节指针),但是这会浪费内存的很多如果你的键入是例如大结构

This assumes that no type needs a bigger alignment than a data pointer, otherwise you would have to calculate an amount of padding bytes to insert after your pointers. To be safe, you could use the sizeof(Type) and some modulo calculation for that (inserting the padding bytes using a char * pointer), but that would waste a lot of memory if your Type is for example a big struct.

总而言之,这个分配是由书面真正的真正的无能的老师。

All in all, this assignment is written by a really really clueless teacher.

这篇关于对于连续二维数组分配内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆