Argh! My brain!

July 30, 2007 at 11:12 PM | Fixed-it | View Comments

For the summer I'm working on adding features to, cleaning up, and making algorithms more generic in a academic research tool. Overall the algorithms and data structures are well planned out and have been generally quite friendly to improvement. As part of my work, I'm integrating derivative versions created by people in their research so it isn't surprising when certain things are hacked together just to get them working, but sometimes it is a little overkill.

The fun example I've found that convinced me to write up this post was using a FIVE dimensional dynamic array to store some information. While I can see some reasons for it, there was quite a shock when i noticed the typecast to (short*****). Here is the code if you dare! (I replaced the variable name with a much shorter one)

  1. foo = (short *****) my_malloc ((nx+1)*sizeof(short ****));
  2. for (i = 0; i < nx+1; i++){
  3.   foo[i] = (short ****) my_malloc ((ny+1)*sizeof(short ***));
  4.   for (j = 0; j < ny+1; j++){
  5.     sblock_pattern_init_mux_lookup[i][j] = (short ***) my_malloc (4*sizeof(short **));
  6.     for (from_side = 0; from_side < 4; from_side++){
  7.       foo[i][j][from_side] = (short **) my_malloc (4*sizeof(short *));
  8.       for (to_side = 0; to_side < 4; to_side++){
  9.         foo[i][j][from_side][to_side] = (short *) my_malloc (nodes_per_chan*sizeof(short));
  10.         for (itrack = 0; itrack < nodes_per_chan; itrack++){
  11.           foo[i][j][from_side][to_side][itrack] = UN_SET;
  12.         }
  13.       }
  14.     }
  15.   }
  16. }

I really don't believe this was the best answer and I can hopefully cut this out.

EDIT: Apparently there were so many *'s it broke the blog software...