简体   繁体   English

memcpy c中的3d指针

[英]memcpy a 3d pointer in c

How can I memcpy a 3d pointer to another 3d? 我怎样才能将3d指针指向另一个3d? I already tried the following: 我已经尝试了以下方法:

void somefunction(ArrayBlock **** b1, int noOfBlocks, ArrayBlock **** b2){  
   memcpy((&b2), (&b1), noOfBlocks*sizeof(ArrayBlock));
}

This gives me the following error in gdb: 这给了我gdb中的以下错误:

[Inferior 1 (process 8528) exited with code 030000000005]

both b1 and b2 were allocated memory in the main function like this: 在主要功能中,b1和b2都被分配了内存,如下所示:

ArrayBlock ***b2;
b2 = malloc(max_x * sizeof(ArrayBlock **));
for (i = 0; i < max_x; ++i)                             
{
    b2[i] = malloc(max_y * sizeof(ArrayBlock *));
    for (j = 0; j < max_y; ++j)
    {
        b2[i][j] = malloc(max_z* sizeof(ArrayBlock));
    }
}


Solution: 解:

I figured that because the initialization of my 3D pointer (array) was non-contiguous, one memcpy will not be enough. 我发现,由于3D指针(数组)的初始化是不连续的,因此仅凭memcpy还是不够的。

so this is what I did: 所以这就是我所做的:

int i2, j2;
for(i2 = 0; i2 < max_x; i2++){
  for(j2 = 0; j2 < max_y; j2++){
    memcpy(&(*b2)[i2][j2][0], &(*b1)[i2][j2][0], max_z*sizeof(ArrayBlock));
  }
}

I think this time it works for real, thanks to those who helped me. 我认为这一次确实可行,这要感谢那些帮助过我的人。

That depends how your 3D array was allocated, but in the most common case (array of arrays of arrays) the answer is no. 这取决于3D数组的分配方式,但是在最常见的情况(数组数组)中,答案是否定的。

There is a way to allocate a 3D array in such way that it's possible, however: 一种方法来分配这样的方式,它是可能的。然而,一个三维数组:

  • Allocate a single CX*CY*CZ array of Stuffs ( Stuff* pXyz ) 分配单个Stuff* pXyz CX * CY * CZ数组( Stuff* pXyz
  • Allocate a single CX*CY array of pointers to Stuff ( Stuff** ppXy ) 分配单个CX * CY指针数组到Stuff( Stuff** ppXy
  • Initialize it with pointers to various areas of pXyz ( ppXy[x*CY + y] = &pXyz[x*CY*CZ + y*CZ + 0]; ) 用指向pXyz各个区域的指针初始化它( ppXy[x*CY + y] = &pXyz[x*CY*CZ + y*CZ + 0];
  • Allocate a single CX array of pointers to pointers to Stuff ( Stuff*** pppX ) 分配单个CX指针数组以指向Stuff的指针( Stuff*** pppX
  • Initialize it with pointers to various areas of ppXy ( pppX[x] = &ppXy[x*CY + 0]; ) 使用指向ppXy各个区域的指针对其进行ppXypppX[x] = &ppXy[x*CY + 0];
  • return pppX;

I hope I didn't get these wrong, but it's the base idea: One dimension, one malloc. 我希望我没有弄错这些,但这是基本思想:一维,一个malloc。

Stuff*** Alloc3DArray(size_t cx, size_t cy, size_t cz) {
    Stuff*** ret = NULL;

    /*Allocate a single CX*CY*CZ array of Stuffs*/
    Stuff* pXyz = malloc(cx * cy * cz * sizeof(*pXyz));
    if(pXyz!=NULL) {
        /*Allocate a single CX*CY array of pointers to Stuff*/
        Stuff** ppXy = malloc(cx * cy * sizeof(*ppXy));
        if(ppXy!=NULL) {
            /*Allocate a single CX array of pointers to pointers to Stuff*/
            Stuff*** pppX = malloc(cx * sizeof(*pppX));
            if(pppX!=NULL) {

                /*Initialize ppXy with pointers to various areas of pXyz*/
                size_t x, y;
                for(x=0 ; x<cx ; x++){
                    for(y=0 ; y<cy ; y++){
                        ppXy[x*cy + y] = &pXyz[x*cy*cz + y*cz + 0];
                    }
                }

                /*Initialize pppX with pointers to various areas of ppXy*/
                for(x=0 ; x<cx ; x++){
                    pppX[x] = &ppXy[x*cy + 0];
                }

                /*Success!*/
                ret = pppX;
            } else {
                /*Allocating third level failed: free first and second levels*/
                free(ppXy), ppXy=NULL;
                free(pXyz), pXyz=NULL;
            }
        } else {
            /*Allocating second level failed: free first level*/
            free(pXyz), pXyz=NULL;
        }
    }
    return ret;
}

With this, you can memcpy this: 有了这个,您可以执行以下操作:

memcpy(&pppDest[0][0][0], &pppSource[0][0][0], CX*CY*CZ*sizeof(***pppDest));

Added: And when you're done with the array, if you've done it right it can be freed thusly: 补充:完成数组操作后,如果正确完成操作,则可以释放它:

void Free3DArray(Stuff ***pppArr) {
    if(pppArr != NULL) {
        free(**pppArr); /*Free third level*/
        free( *pppArr); /*Free second level*/
        free(  pppArr); /*Free first level*/
    }
}

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

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