0

I'm trying to program tensorial algebra, for which I need some arrays whose dimension will be unknown when compiling (please, recall that text within square brackets is NOT actual code):

     float [undetermined number of *'s] element; //elements of the tensor

     unsigned int co_index;

     unsigned int contra_index;

     unsigned int N;

Where "element" variable has as many "*" symbols as (co_index + contra_index); is there a way to do this, define the "dimension" of "element" variable at execution time?

An example of what I mean: if (co_index + contra_index = 3), the memory allocation for "element" should look like:

 elemento = (float***)malloc(sizeof(float**) * N);

  for(i=0;i<N;i++)
     elemento[i]=(float**)malloc(sizeof(float*) * N);

  for(i=0;i<N;i++)
  {
     for(j=0;j<N;j++)
        elemento[i][j]=(float*)malloc(sizeof(float) * N);
  }

Can this "floating dimension" array be defined in general at execution time?

gudise
  • 239
  • 1
  • 9
  • it is completely unclear for me what you want to archive. There is no correlation between your struct and the mallocs. Explain it better. Even your typedef will not compile. Put some effort and be less careless when you post the questions - respect our time and effort. – 0___________ Jun 01 '18 at 10:39
  • 2
    The point is, if I knew how to write it clearer... I'd probably have this issue fixed. I'm aware that it's pretty unclear, I'l try to rewritte it better – gudise Jun 01 '18 at 10:40
  • If you do not know what you want to archive no one can help you – 0___________ Jun 01 '18 at 10:41
  • Are you saying that if `co_index + contra_index` were 5 then you'd end up with `elemento[a][b][c][d][e]` ? – Chris Turner Jun 01 '18 at 10:48
  • 1
    @Chris Turner yes! That's right! Is it nonsense? – gudise Jun 01 '18 at 10:49
  • @PeterJ_01 Is it maybe clearer now? – gudise Jun 01 '18 at 10:50
  • 1
    Yes, in general, this is nonsense. – purec Jun 01 '18 at 11:17
  • @purec why? what troubles could it bring? – gudise Jun 01 '18 at 11:22
  • 3
    I think rather than trying to shoehorn this into some kind of array with a variable number of dimensions, you'd find it much easier to treat this as a tree structure – Chris Turner Jun 01 '18 at 11:22
  • @Guillermo. D. S, I cannot even imagine how to implement this. But even if you can imagine, you will not be able to implement. – purec Jun 01 '18 at 11:27
  • @Chris Turner that seems to be what I was looking for – gudise Jun 01 '18 at 11:57
  • 1
    The dupe does not answer this question. The dupe is about fixed dimension arrays and how to get optimal memory layout. This question ask for runtime variable dimension. I'll vote to reopen. – Support Ukraine Jun 04 '18 at 13:32

0 Answers0