差的B / W在1或去逐行分配内存以二维数组 [英] difference b/w allocating memory to 2D array in 1 go or row by row

查看:132
本文介绍了差的B / W在1或去逐行分配内存以二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这将是在阵列的访问或在这两种情况下的内存分配的影响:

1

  INT **改编;
    ARR =的malloc(sizeof的(INT)*行*列);

2

  INT **改编;
    ARR =的malloc(sizeof的(* ARR)*行);
    对于(i = 0; I<排;我++)
        改编[I] =的malloc(sizeof的(** ARR)*列));


解决方案

  • 首先,影响是你的第一种方法是打破了。它不会通过工作的 INT ** 指针。

    为了一次性分配一个二维数组,你正在尝试做它在你的第一个方法,你确实有分配足够大小的一维数组

     为int * ARR =的malloc(行*列* sizeof的* ARR);
    //注意:`为int *`,`不是INT **`

    和手动指数重新计算,例如进行访问而不是做改编[I] [J] 你要做的改编[我*柱+ J]

    试图在 INT **分配的指针存储改编,然后访问阵列为改编[I] [J] 只会导致死机。


  • 其次,你的第二个方法是确定。它只是在第二种方法你是不是真的需要分配由多个独立的的malloc 通话二级存储。您可以一次性分配整个第二级存储

      INT ** ARR =的malloc(行* sizeof的* ARR);
    为int * arr_data =的malloc(行*列* sizeof的* arr_data);

    然后就分配行之间的pre-分配的第二级存储

     为(i = 0; I<排;我++)
      改编[I] = arr_data + I *列;

    (当然,你也可以独立地分配行,如果你愿意的话,也将正常工作。我想分配他们一出手就是为了更好地说明第一和第二种方法之间的相似性作为评价的理由下文。)


现在,通过观察这两种方法,你可以很容易地看到他们两人基本上是做同样的事情。唯一的区别是,在第一种方法,你通过计算改编+ I *列每次(注意,<$ C $找到即时行的开始C>改编[我*柱+ J] 等同于(ARR + I *列)[J] )。在第二种方法你pre-计算使用相同的 arr_data + I *列配方事先所有的行开始,并将其存储以便以后使用单独的行索引数组改编

因此​​,这基本上归结于存储器的使用(第一种方法需要更少的存储器)和速度之间的权衡(第二种方法是可能的,但不是必须的,更快)。同时第二个方法支持二维数组访问的自然语法 - 改编[I] [J] ,而在第一种方法,你必须使用比较绕口与指数的重新计算1D访问语法。

what would be the impact in accessing of array or in memory allocation for these two cases:

1.

    int **arr;
    arr = malloc( sizeof(int) * row * column );

2.

    int **arr;
    arr = malloc( sizeof(*arr) * row);
    for(i=0; i<row; i++)
        arr[i] = malloc( sizeof( **arr) * column));

解决方案

  • Firstly, the "impact" is that your first method is broken. It will not work through an int ** pointer.

    In order to allocate a 2D array in one shot as you are trying to do it in your first method, you actually have to allocate a 1D array of sufficient size

    int *arr = malloc( row * column * sizeof *arr );
    // Note: `int *`, not `int **`
    

    and perform access by manual index re-calculation, e.g. instead of doing arr[i][j] you have to do arr[i * column + j].

    Trying to store the allocated pointer in int **arr and then access your array as arr[i][j] will simply lead to crashes.

  • Secondly, your second method is OK. It is just that in the second method your are not really required to allocate the second-level memory by multiple independent malloc calls. You can allocate the whole second-level memory in one shot

    int **arr = malloc( row  * sizeof *arr );
    int *arr_data = malloc( row * column * sizeof *arr_data );
    

    and then just distribute that pre-allocated second-level memory between the rows

    for (i = 0; i < row; i++)
      arr[i] = arr_data + i * column;
    

    (Of course, you can allocate the rows independently, if you wish so. It will also work. The reason I wanted to allocate them in one shot is to better illustrate the similarity between the first and the second approach, as commented below.)

Now, by looking at these two methods you can easily see that both of them essentially do the same thing. The only difference is that in the first method you find the beginning of the row on-the-fly by calculating arr + i * column every time (note that arr[i * column + j] is equivalent to (arr + i * column)[j]). In the second method you pre-calculate all row beginnings in advance by using the same arr_data + i * column formula, and store them for further usage in a separate "row index" array arr.

So, it basically boils down to trade-off between memory usage (first method requires less memory) and speed (the second method is potentially, but not necessarily, faster). At the same time the second method supports the "natural" syntax for 2D array access - arr[i][j], while in the first method you have to use more convoluted 1D access syntax with index recalculation.

这篇关于差的B / W在1或去逐行分配内存以二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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