使用结构内的指针,动态二维数组 [英] Using a pointer to a dynamic 2D Array within a struct

查看:159
本文介绍了使用结构内的指针,动态二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在写我的电磁仿真课程一块code和我所遇到的一个问题。我决定做一点额外的通过扩大原有的计算,可达10 ^ 8个元素的真正大的网,所以现在我不得不使用malloc()。

I have been writing a piece of code for my coursework in electromagnetic simulation and I have run into a problem. I decided to do a bit extra by expanding the original calculations to really large meshes of up to 10^8 elements, so now I have to use malloc().

到目前为止,一切都很好,但因为我preFER让我的code库中,然后用编译器的内嵌选项编译,我需要一种方法来传递函数之间的信息。所以,我开始使用结构来跟踪网格的参数,以及指针的信息的阵列。我定义的结构方式如下:

So far, so good, but since I prefer to keep my code in libraries and then compile with the inline option of the compiler, I needed a way to pass information between functions. So, I started using structs to keep track of the parameters of the mesh, as well as the pointer to the array of information. I defined the struct the following way:

typedef struct {
    int    height;
    int    width;
    int    bottom; //position of the bottom node
    unsigned int***  dat_ptr;//the pointer to the array with all the data
    } array_info;

凡三重指向一个unsigned int是指向一个二维数组。我必须这样做,这样一来,因为否则它是按值传递,我无法从函数内改变。

Where the triple pointer to an unsigned int is the pointer to a 2D array. I have to do it this way because otherwise it is passed by value and I cannot change it from within the function.

现在,当我尝试使用以下功能结构分配内存:

Now, when I try to allocate memory for the struct with the following function:

void create_array(array_info A)//the function accepts struct of type "array_info" as argument
{
    int i;

    unsigned int** array = malloc(sizeof(*array) * A.height);//creates an array of arrays
    for(i = 0; i<A.height; ++i)
    {
        array[i] = malloc(sizeof(**array) * A.width);//creates an array for each row
    }
    *A.dat_ptr=array;//assigns the position of the array to the input pointer
}

我在执行操作时出现段故障。我不明白为什么的sizeof(* A.dat_ptr)是一样的sizeof(数组)。因此,在最坏的情况下,我应该得到的地方乱码的路线,而不是在分配行,对吧?

I get a segmentation fault upon executing the operation. I cannot see why: sizeof(*A.dat_ptr) is the same as sizeof(array). Thus, in the worst case I should be getting gibberish somewhere down the line, not in the assignment line, right?

推荐答案

您要么需要从函数返回的 array_info 结构(修订本)或(更通常)一个指针传递给 array_info 结构到功能,使您所做的更改会影响调用函数的值。

You either need to return the array_info structure (as amended) from the function or (more usually) pass a pointer to the array_info structure into the function so that the changes you make affect the value in the calling function.

typedef struct
{
    int    height;
    int    width;
    int    bottom;
    unsigned int **dat_ptr;  // Double pointer, not triple pointer
} array_info;

void create_array(array_info *A)
{
    unsigned int **array = malloc(sizeof(*array) * A->height);
    for (int i = 0; i < A->height; ++i)
        array[i] = malloc(sizeof(**array) * A->width);
    A->dat_ptr = array;
}

我假设你做的内存分配一些地方检查;逻辑的地方就是这个功能,虽然。从故障中的一部分的方式,通过恢复是繁琐的(但必要的,如果你打算从函数返回,而不是退出程序)。

I assume you do some checking on the memory allocations somewhere; the logical place is this function, though. Recovery from a failure part way through is fiddly (but necessary if you are going to return from the function rather than exit from the program).

void create_array(array_info *A)
{
    unsigned int **array = malloc(sizeof(*array) * A->height);
    if (array != 0)
    {
        for (int i = 0; i < A->height; ++i)
        {
             if ((array[i] = malloc(sizeof(**array) * A->width)) == 0)
             {
                 for (int j = 0; j < i; j++)
                      free(array[j]);
                 free(array);
                 array = 0;
                 break;
             }
        }
    }
    A->dat_ptr = array;
}

调用函数知道,如果 dat_ptr 成员是从 create_array返回NULL功能失败()。这可能是更好的提供成功/失败的返回值。

The calling function knows that the function failed if the dat_ptr member is null on return from create_array(). It might be better to provide a success/failure return value.

我使用的是C99,因此调用code可能是:

I'm using C99, so the calling code might be:

array_info array = { .height = 10, .width = 20, .dat_ptr = 0 };
create_array(&array);
if (array->dat_ptr == 0)
    ...error handling...

请注意,在 create_array的code()可能需要检查空指针,为负或零宽度或高度。我不清除元素应该包含什么,所以我把它初始化,这给了我使用指定的初始值一半的借口。你也可以写初始化很清楚,而无需使用指定的初始值。

Note that the code in create_array() might need to check for a null pointer, for negative or zero width or height. I'm not clear what the bottom element should contain, so I left it uninitialized, which gives me half an excuse for using designated initializers. You can also write the initializer quite clearly without using designated initializers.

这篇关于使用结构内的指针,动态二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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