3

I want to use a quadruple pointer to represent a matrix split up into blocks. I think this could be a bad idea. It would look something like this:

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));
          }

      }

  }  

Is this a good way to go about this?

Note I assume a square matrix and I make the blocks square.

John Meighan
  • 69
  • 1
  • 10
  • It's not entirely clear what you mean by "blocks". But it's almost certainly worth abstracting this - e.g. using `typedef`s. – Oliver Charlesworth Dec 11 '17 at 18:54
  • 2
    Being called a [three star programmer](http://wiki.c2.com/?ThreeStarProgrammer) is *not* a compliment. Four? Then you will soon be ready for the psych ward. – Some programmer dude Dec 11 '17 at 18:54
  • The pointers notwithstanding, the hard coded dimensions give pause as to whether any of this should be using dynamic allocation more than once, much less looped four levels deep. – WhozCraig Dec 11 '17 at 18:56
  • 1
    On a more serious note, *why* do you need this? What is the *actual* problem you want to solve this way? I suggest you [read about the XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) and think about how it might relate to this question. And of course take a closer look at your requirements, analysis and design. Are you *sure* you really need this? – Some programmer dude Dec 11 '17 at 18:56
  • @WhozCraig I updated that. – John Meighan Dec 11 '17 at 18:57
  • @Someprogrammerdude It's using a block multiplication method to multiply matrices. – John Meighan Dec 11 '17 at 18:58
  • He's a 4. Star. Hero. He's got stars in his eyes! I feel the entire song could be applied here with just a little work and break the internets. – Michael Dorgan Dec 11 '17 at 19:00
  • Well if you really *need* to do something like this, then I suggest you first of all try to hide it behind some kind of abstraction using a set of functions that hides all these details as much as possible (including using [opaque data structures](https://en.wikipedia.org/wiki/Opaque_data_type). Then test out that API and the functionality it provides to make sure it can handle every case. Then use that API only. And of course document it all very well. Once you have done that, it's no longer that bad. – Some programmer dude Dec 11 '17 at 19:02
  • This gives me a headache, and I've been know to do some crazy stuff with pointers. At any rate, I'd most certainly allocate what I need without the loops and then somehow link it all together, either using loops and obscure maths or hard coding it. – Zacariaz Dec 11 '17 at 20:39

2 Answers2

4

I apologize for this as it isn't an answer, but I couldn't resist:

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...
Michael Dorgan
  • 12,453
  • 3
  • 31
  • 61
3

It is not a bad idea, just inconvenient, and already discussed for 3D matrices dynamic allocation here: Malloc a 3-Dimensional array in C?.

VladP
  • 529
  • 3
  • 15