简体   繁体   English

函数中的C memcpy 2D数组到3D数组

[英]C memcpy 2D array to 3D array within function

In the example below, I have a 3D array which I pass to a function, "fun", but only pass a "slice" of the 3D array ie a 2D array. 在下面的示例中,我有一个3D数组,该数组传递给函数“ fun”,但仅传递3D数组的“切片”,即2D数组。 Within the function, I have another 2D variable, which takes on some values within a double for loop. 在函数中,我还有另一个2D变量,该变量在double for循环中具有一些值。 Before exiting the function, I need to copy the 2D temporary array to the "slice" of the 3D array. 在退出功能之前,我需要将2D临时数组复制到3D数组的“切片”。 I tried the following, but my use of memcpy is "destroying" the original 3D array outside the function. 我尝试了以下操作,但是对memcpy的使用是“破坏”函数外部的原始3D数组。 What is the proper way to perform this operation? 什么是执行此操作的正确方法?

void fun(double **array, int XDIM, int YDIM);   // Declaration

int main()
{
    double ***array
    int XDIM=10,YDIM=10,TDIM=2;

    allocate3d(&array,XDIM,YDIM,TDIM); // This works fine
    fun(array[0],XDIM,YDIM);

    //
    //  Other code note important to problem here
    //

    deallocate3d(array,XDIM,YDIM,TDIM); // This works fine
}


void fun(double **array, int XDIM, int YDIM)
{
    double **tmp;

    allocate2d(&tmp,XDIM,YDIM); // This works fine

    for(int j = 0; j < YDIM; j++)
    {
        for(int i = 0; i < XDIM; i++)
        {
            tmp[j][i] = 2.5;
        }
    }

    memcpy(array,tmp,XDIM*YDIM*sizeof(double));

    deallocate2d(tmp,XDIM,YDIM); // This works fine

}

EDIT: I want to add that "array" has dimensions of 编辑:我想添加“数组”的尺寸为

array[TDIM][YDIM][XDIM]

and "tmp" has dimensions of 而“ tmp”的尺寸为

tmp[YDIM][XDIM]

I did address this in a comment, but thought I should point it out in the main question. 我确实在评论中谈到了这一点,但认为我应该在主要问题中指出这一点。 I also showed that I free the memory of tmp within the function. 我还显示了我在函数中释放了tmp的内存。

EDIT 2: I'm including the "allocate3D" and "deallocate3D" functions for further analysis of memory storage. 编辑2:我包括“ allocate3D”和“ deallocate3D”函数,以进一步分析内存存储。

int allocate3D(double ****array, int XDIM, int YDIM, int TDIM)
{
    // Allocate 3rd dimension
    *array = calloc(TDIM,sizeof(double**));
    if (array==NULL)
    {
        printf("Error in memory allocation\n");
        return (EXIT_FAILURE);
    }

    // Allocate each 3rd dimensional pointer with array of pointers
    for(int i = 0; i < TDIM; i++)
    {
        (*array)[i] = calloc(YDIM,sizeof(double*));
    }

    if (array==NULL)
    {
        printf("Error in memory allocation\n");
        return (EXIT_FAILURE);
    }

    // Allocate remaining dimension
    for(int i = 0; i < TDIM; i++)
    {
        for(int j = 0; j < YDIM; j++)
        {
            (*array)[i][j] = calloc(XDIM,sizeof(double));
        }
    }

    if(array == NULL)
    {
        printf("Error in memory allocation\n");
        return(EXIT_FAILURE);
    }

    return 0;
}

void deallocate3D(double ***array, int XDIM, int YDIM, int TDIM)
{
    //  Deallocate each 3rd dimensional pointer
    for(int i = 0; i < TDIM; i++)
    {
        for(int j = 0; j < YDIM; j++)
        {
            free((array)[i][j]);
        }

        free((array)[i]);
    }

    // Deallocate 3rd dimension
    free(array);
}

The functions "allocate2D" and "deallocate2D" follow the same concept, minus the "TDIM" dimension, so I will leave it out for the sake of simplicity. 函数“ allocate2D”和“ deallocate2D”遵循相同的概念,减去“ TDIM”维,因此为简单起见,我将其省略。 It has been brought to my attention that the portion of the array which I'm trying to use with "memcpy" may not be contiguous and is "jagged". 引起我注意的是,我尝试与“ memcpy”一起使用的数组部分可能不是连续的,而是“锯齿状的”。 In addition to providing the allocation function, is there a simple way to identify contiguous memory storage? 除了提供分配功能外,还有一种简单的方法来识别连续的内存存储吗? How do 3D arrays get stored in memory? 3D阵列如何存储在内存中?

EDIT 3: I think I may have found a clue...in the example above, I'm setting YDIM == 1. I believe this affects the contiguous memory alignment and my allocation and free functions are causing errors. 编辑3:我想我可能已经找到了一个线索...在上面的示例中,我正在设置YDIM ==1。我相信这会影响连续的内存对齐,并且我的分配和自由函数会导致错误。 I've used Valgrind and it flags my allocation and free functions when YDIM == 1. In addition, this would affect how memcpy is behaving. 我使用过Valgrind,当YDIM == 1时,它会标记我的分配和自由函数。此外,这将影响memcpy的行为方式。 Any thoughts on my speculations? 对我的推测有何想法? Why set YDIM == 1....cause sometimes that is the case. 为什么设置YDIM == 1 ....有时是这种情况。 I'm trying to keep things universal in application. 我正在尝试使应用程序具有通用性。

I think that what you want to do in fun is: 我认为您想做的fun是:

for(int j = 0; j < YDIM; j++)
{
    for(int i = 0; i < XDIM; i++)
    {
        array[j][i] = tmp[j][i];
    }
}

You cannot use a single memcpy because your array is not a contiguous bloc of memory; 您不能使用单个memcpy因为您的数组不是连续的内存块; it is a jagged array (each 1-dimensional block is stored in a separate allocation). 它是一个锯齿状的数组 (每个1维块存储在单独的分配中)。

I'm assuming you have your dimensions in the right order (you didn't post the body of allocate3d so that can't be verified) 我假设您的尺寸是正确的顺序(您未发布allocate3d的主体,因此无法进行验证)

I'm not sure you need a temp array there. 我不确定那里是否需要临时数组。 Since you're already passing in the array itself, and in c you can modify arrays directly with other functions. 由于您已经传递了数组本身和c语言,因此您可以直接使用其他函数来修改数组。 Maybe... 也许...

void fun(double **array, int XDIM, int YDIM)
{
    for(int j = 0; j < YDIM; j++)
    {
        for(int i = 0; i < XDIM; i++)
        {
            array[j][i] = 2.5;
        }
    }
}

Let me know how it goes! 让我知道事情的后续!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM