如何将2d数组复制到临时2d数组并返回?

时间:2021-06-20 20:57:48

I'm trying to copy a 2-d array of ints to a temporary 2-d array of ints and return it. I've had a go at it below, but I get a pretty suspicious malloc error. I tried inspecting it with valgrind, but couldn't find anything useful

我正在尝试将一个2维的int数组复制到一个临时的2-d数组并返回它。我已经在下面进行了操作,但是我得到了一个非常可疑的malloc错误。我尝试用valgrind检查它,但找不到任何有用的东西

int **get_grid_state(int **grid, int height, int length) {
    int **grid_state;
    int i;

    grid_state = malloc(height * sizeof(int*));

    for (i = 0; i < height; i++) {
        grid_state[i] = malloc(length);
        memcpy(grid_state[i], grid[i], length);
    }
    return grid_state;
}

Un-settling error message is as follows:

解决错误消息如下:

program: malloc.c:2372: sysmalloc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 *(sizeof(size_t))) - 1)) & ~((2 *(sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long) old_end & pagemask) == 0)' failed.
Aborted

1 个解决方案

#1


2  

Your approach is correct, except for the size length in the inner loop: it should length * sizeof(int) or sizeof(**grid):

你的方法是正确的,除了内循环中的大小长度:它应该是length * sizeof(int)或sizeof(** grid):

        grid_state[i] = malloc(length * sizeof(**grid));
        memcpy(grid_state[i], grid[i], length * sizeof(**grid));

The cause for the unsettling error is the subarrays are allocated too small and you probably modify them in some other part of the program, causing some corruption of the malloc internal data that is detected at some later call to one of the allocation functions: malloc(), free(), calloc(), realloc()...

令人不安的错误的原因是子数组的分配太小而你可能在程序的其他部分修改它们,导致某些以后调用其中一个分配函数时检测到的malloc内部数据损坏:malloc( ),free(),calloc(),realloc()......

Note also that you do not check the return values of these malloc() calls. If for some reason malloc cannot allocate the memory, you will invoke undefined behavior instead of returning NULL gracefully.

另请注意,您不检查这些malloc()调用的返回值。如果由于某种原因,malloc无法分配内存,您将调用未定义的行为,而不是正常返回NULL。

#1


2  

Your approach is correct, except for the size length in the inner loop: it should length * sizeof(int) or sizeof(**grid):

你的方法是正确的,除了内循环中的大小长度:它应该是length * sizeof(int)或sizeof(** grid):

        grid_state[i] = malloc(length * sizeof(**grid));
        memcpy(grid_state[i], grid[i], length * sizeof(**grid));

The cause for the unsettling error is the subarrays are allocated too small and you probably modify them in some other part of the program, causing some corruption of the malloc internal data that is detected at some later call to one of the allocation functions: malloc(), free(), calloc(), realloc()...

令人不安的错误的原因是子数组的分配太小而你可能在程序的其他部分修改它们,导致某些以后调用其中一个分配函数时检测到的malloc内部数据损坏:malloc( ),free(),calloc(),realloc()......

Note also that you do not check the return values of these malloc() calls. If for some reason malloc cannot allocate the memory, you will invoke undefined behavior instead of returning NULL gracefully.

另请注意,您不检查这些malloc()调用的返回值。如果由于某种原因,malloc无法分配内存,您将调用未定义的行为,而不是正常返回NULL。