分配与C整数矩阵 [英] Allocate matrix of integer with c

查看:126
本文介绍了分配与C整数矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个整数2D矩阵NUMI并称为3D双矩阵 PROB

I have an integer 2d matrix NUMI and a 3d double matrix called PROB.

下面的2个分配:

   int main ( int argc, char* argv[]){
        double  ***PROB;
        int  **NUMI;    
        NUMI = (int **)malloc((dim)*sizeof(int *));
        PROB = (double ***)malloc((dim)*sizeof(double**));
        ...    
        for( i = 0; i < n ; i++){
            PROB[ACT][ACTSTART][i] = value;
            NUMI[i][i]= value2;
        }


}


  • 多少行列数有 NUMI ?是一个昏暗朦胧点¯x矩阵 ???

    PROB 是一个三维矩阵......这里的分配昏暗昏暗的点¯x朦胧点¯x

    PROB is a 3d matrix...here the allocation is dim x dim x dim?

    推荐答案

    NUMI不具有行和列,这是一个指针到指针到int和发生在具有室温的分配的内存被指向为昏暗指针到整型,不 *黯淡朦胧整数。这将等同于治疗为已申报为int * NUMI [DIM]

    NUMI does not have rows and columns, it is a pointer-to-pointer-to-int, and happens to be pointing at an allocated memory that has room for dim pointers-to-int, not dim * dim ints. This would be equivalent to treating as having been declared int* NUMI[dim]

    呼叫

    int* NUMI;
    NUMI= malloc( dim*dim*sizeof(int) );
    

    将分配一个昏暗昏暗整数矩阵。

    不过,请注意,多维数组,比如 INT示例[A] [B] ,分配的区域的大小相当于为int * example_ =的malloc(A * b *的sizeof(INT)),编译器的工作原理进行多维指标转换为一个单维度指标​​,即例子[C] [D] - > example_ [C * A + D]

    However, please note that with multi-dimensional arrays, say int example[a][b], the size of the allocated area is equivalent to int* example_ = malloc(a*b*sizeof(int)), and the compiler works out the conversion from multidimensional indices to a single-dimension index, i.e. example[c][d] -> example_[c*a+d]

    所以,当你做

    int* NUMI;
    NUMI= malloc( dim*dim*sizeof(int) );
    //...
    NUMI[i][i]= value2;
    

    ,编译器不具有以从多个尺寸转换成单维等效所需的信息。

    The compiler doesn't have the information required to convert from the multiple dimension to the single-dimension equivalent.

    PROB是相似的,指向同室的存储区域暗淡指针到指针到双,而不是昏暗*黯淡*朦胧增加一倍。
    要获得昏暗立方矩阵,你需要

    PROB is similar, pointing to a memory area with room for dim pointers-to-pointers-to-double, not dim * dim * dim doubles. To get a dim cubed matrix, you'd need

    double  *PROB;
    PROB = (double *)malloc( dim*dim*dim*sizeof(double) );
    

    和运行与多维索引相同的问题。

    and run into the same problem with multi-dimensional indices.

    如果暗淡是编译时间常数,你可以直接声明多维数组没有的malloc

    If dim is a compile-time constant, you can declare the multidimensional arrays directly without malloc

    double PROB[dim][dim][dim];
    int    NUMI[dim][dim];
    

    如预期在主月底()循环现在应该工作。

    The loop at the end of the main() should now work as expected.

    如果你必须使用malloc,或者使用malloc如上文所述:

    If you must use malloc, either use malloc as described above:

    NUMI= (int *)    malloc( dim*dim*sizeof(int) );
    PROB = (double *)malloc( dim*dim*dim*sizeof(double) );
    

    和修改循环体

    PROB[(ACT * dim * dim) + (ACTSTART * dim ) + i] = value;
    NUMI[i + dim * i]= value2;
    

    另外,由阿列克谢和Paolo描述调用malloc在多重循环。
    我的解决方案中,有一个的malloc每个变量()调用,因此每个变量指单一连续的存储区。随着多个的malloc()调用循环,您有多个分配的内存区域是不太可能是连续的。

    Alternatively, call malloc in multiple loops as described by Alexey and Paolo. With my solution, there is one malloc() call per variable, so each variable refer to a single contiguous memory area. With multiple malloc() calls in loops, you have multiple allocated memory areas that is unlikely to be contiguous.

    这篇关于分配与C整数矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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