的malloc,阿婷在功能多维数组 [英] malloc-ating multidimensional array in function

查看:84
本文介绍了的malloc,阿婷在功能多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想分配在C程序的二维数组。它在这样的主要功能工作正常(如解释的这里):

I'm trying to allocate a 2d array in a C program. It works fine in the main function like this (as explained here):

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char ** argv)
{
    int ** grid;
    int i, nrows=10, ncols=10;
    grid = malloc( sizeof(int *) * nrows);

    if (grid == NULL){
        printf("ERROR: out of memory\n");
        return 1;
    }

    for (i=0;i<nrows;i++){
        grid[i] = malloc( sizeof(int) * ncols);
        if (grid[i] == NULL){
            printf("ERROR: out of memory\n");
            return 1;
        }
    }
    printf("Allocated!\n");

    grid[5][6] = 15;
    printf("%d\n", grid[5][6]);
    return 0;
}

但因为我必须这样做几次不同的阵列中,我试图在code移动到一个单独的函数。

But since I have to do this several times with different arrays, I was trying to move the code into a separate function.

#include <stdio.h>
#include <stdlib.h>

int malloc2d(int ** grid, int nrows, int ncols){
    int i;
    grid = malloc( sizeof(int *) * nrows);

    if (grid == NULL){
        printf("ERROR: out of memory\n");
        return 1;
    }

    for (i=0;i<nrows;i++){
        grid[i] = malloc( sizeof(int) * ncols);
        if (grid[i] == NULL){
            printf("ERROR: out of memory\n");
            return 1;
        }
    }
    printf("Allocated!\n");
    return 0;
}

int main(int argc, char ** argv)
{
    int ** grid;

    malloc2d(grid, 10, 10);
    grid[5][6] = 15;
    printf("%d\n", grid[5][6]);
    return 0;
}

不过,虽然同时分配不抱怨,我得到段错误访问数组时。我读了腐朽阵列和类似主题的不同岗位,但我仍然无法弄清楚如何来解决这个问题。我想我没有正确地传递二维数组给函数。

However, although it doesn't complain while allocating, I get segmentation fault when accessing the array. I read different posts on decayed arrays and similar topics, but I still can't figure out how to solve this problem. I imagine I'm not passing the 2d array correctly to the function.

非常感谢。

推荐答案

这是不是一个多维数组;它是包含指向一维阵列的单个二维数组。多维数组不包含指针;他们是单一的内存块。

That is not a multidimensional array; it is a single dimensional array containing pointers to single dimensional arrays. Multidimensional arrays do not contain pointers; they are single memory blocks.

在这里你的问题是,你有一个指针的指针,而你试图通过一个参数从函数返回。如果你要做到这一点,你会需要一个指针的指针的指针作为参数,你将有一个指针的地址传递给一个指针的方法。如果你不这样做,你不是在改变变量电网的价值主要 - 你'重新改变其被复制作为参数传递给 malloc2d 功能之一。因为电网剩下未初始化的,你会得到一个未定义的行为。

Your problem here is that you have a pointer to a pointer, and you're trying to return it from your function through a parameter. If you're going to do that, you're going to need a pointer to a pointer to a pointer as your parameter, and you're going to have to pass the address of a pointer to a pointer to the method. If you don't do this, you're not changing the value of the variable grid in main -- you're changing the one which was copied as the parameter to the malloc2d function. Because the grid in main is left uninitialized, you get a undefined behavior.

下面是我的意思是修复一个例子:

Here's an example of what I mean as the fix:

#include <stdio.h>
#include <stdlib.h>

int malloc2d(int *** grid, int nrows, int ncols){
    int i;
    *grid = malloc( sizeof(int *) * nrows);

    if (*grid == NULL){
        printf("ERROR: out of memory\n");
        return 1;
    }

    for (i=0;i<nrows;i++){
        (*grid)[i] = malloc( sizeof(int) * ncols);
        if ((*grid)[i] == NULL){
            printf("ERROR: out of memory\n");
            return 1;
        }
    }
    printf("Allocated!\n");
    return 0;
}

int main(int argc, char ** argv)
{
    int ** grid;

    malloc2d(&grid, 10, 10);
    grid[5][6] = 15;
    printf("%d\n", grid[5][6]);
    return 0;
}

其他注意事项:


  • 如果一个分配失败,则泄漏的第一阵列的分配,以及为所有previous行分配。您需要在返回之前调用免费这些。

  • 您是通过一个参数回来,即使你真的不必。如果我写这个,我会做的方法返回 INT ** 和信号错误通过返回 0

  • If a single allocation fails, you leak the allocation for the first array, as well as the allocations for all previous rows. You need to call free on those before returning.
  • You're returning through a parameter, even though you really don't have to. If I were writing this, I'd make the method return int **, and signal error by returning 0.

这篇关于的malloc,阿婷在功能多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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