在c中使用四重指针将矩阵分割成块

时间:2017-12-11 18:51:45

标签: c malloc

我想使用四重指针来表示分成块的矩阵。我认为这可能是一个坏主意。它看起来像这样:

double ****A=malloc(NBlocks*sizeof(double***));

for(i=0;i<NBlocks;i++)
  {
    A[i]=malloc(NBlocks*sizeof(double**));
    for(j=0;j<NBlocks;j++)
      {
        A[i][j]=malloc(Blocksize*sizeof(double*));
        for(k=0;k<Blocksize;k++)
          {
              A[i][j][k]=malloc(Blocksize*sizeof(double));
          }

      }

  }  

这是一个很好的方法吗?

注意我假设一个方形矩阵,我使块方块。

2 个答案:

答案 0 :(得分:2)

这不是一个坏主意,只是不方便,已经讨论过3D矩阵动态分配:Malloc a 3-Dimensional array in C?

答案 1 :(得分:2)

我为此道歉,因为它不是一个答案,但我无法抗拒:

Sung to "Jukebox Hero":

Lookin' at some code, with his head hung low
Couldn't get the memory, it was a sold out show.
Heard the roar of the crowd, he could picture the scene
He could call to the heap, and and keep it all lean.

He saw the pointer, just blew him away
He saw stars in his eyes, and the very next day
Installed a beat up compiler from a secondhand store
Didn't know how to code yet, but he knew for sure

That four star pointer, felt good in his hands,
Didn't take long, to understand.
Just a four star pointer, slung way down low,
Was a one way ticket, only one way to go.

So he started codin'
Ain't never gonna stop
Gotta keep codin'
Someday he's gonna make it to the top

He's a four star hero, got stars in his eyes
He's a four star hero...