KBD

Keith Devens .com

Friday, August 29, 2008 Flag waving
I have the strength of a bear... that has the strength of two bears! – Sealab 2021
← More winds of change... this time, in Egypt!Music thing: The hamster powered MIDI sequencer →

Daily link icon Saturday, February 26, 2005

Dynamically allocated multi-dimensional arrays in C

In C, a multi-dimensional array for which you know the size in advance can be iterated through how we'd expect (i.e. using double subscript notation m[i][j]):

void printMatrix(float m[SIZE][SIZE]){
    int i,j;
    for(i=0; i<SIZE; i++){
        for(j=0; j<SIZE; j++)
            printf("%f ", m[i][j]);
        printf("\n");
    }
    printf("\n");
}

(I'm using all square matrices for simplicity). But the signature for printMatrix has to specify the size of the matrix (though, the first dimension can be left out). That's because when you have a multi-dimensional array it's actually just an ordinary array with each row one after another. When the compiler compiles m[i][j] it actually translates it into something like m[i*n+j], and you have to specify the size in advance so it knows what n is.

Now, if you have a dynamically allocated multi-dimensional array (or if you simply want to write a function that doesn't have to know the size in advance), you essentially have to do some of that compilation yourself. printMatrix becomes:

void printMatrix(float *m, int n){
    int i,j;
    for(i=0; i<n; i++){
        for(j=0; j<n; j++)
            printf("%f ", m[i*n+j]);
        printf("\n");
    }
    printf("\n");
}

And you can allocate m with:

float *m = (float*) calloc(d*d, sizeof(float));
← More winds of change... this time, in Egypt!Music thing: The hamster powered MIDI sequencer →

Comments XML gif

Peter wrote:

I agree this is a good method when the array size is not known in advance.

But, for dynamically allocated multi-dimensional arrays--whose size is known in advance--there is a better way.

Instead of pointing to a matrix with a pointer of type (float *), you can define a more exotic pointer like this

float (*p)[SIZE];

and then you allocate storage for p, using malloc or whatever you like. Now the first version of printMatrix will work just fine when you call printMatrix(p).

I prefer this method because it helps detect data-type mismatches. For example, let's say someone accidentally uses printMatrix to print an array v[] of floats, where v[] represents a vector, instead of a matrix. The second version of printMatrix would unwittingly allow

printMatrix(v,SIZE);

and this would probably cause a hard-to-track-down memory access violation at run-time.

But, in the first version of printMatrix, trying to call printMatrix(v) would cause an easy-to-spot compliation error because v does not have type float[][SIZE]

∴ Peter | 10-Apr-2005 6:30am est | #7388

Feel free to post a comment below. Please see my comment policy.

Formatting Rules (No HTML):

  • **bold**, *italic*, _underlined_, --strikeout--
  • "text"="url" creates a link, and URLs are auto-highlighted
  • Blockquote: Like e-mail, begin paragraph with > (greater-than sign)
  • Lists: begin paragraph with *,-, or + (unordered), or # (ordered)
  • Code block: ?!code:language=perl|php|sql|javascript|etc.{\n}...{\n}?!/code

:
(will be your IP address if blank)
: (optional)
(Will not be shown on site)

: (optional)
:

August 2008
SunMonTueWedThuFriSat
 12
3456789
10111213141516
17181920212223
24252627282930
31 



RSS feed RSS feed for Keith's Weblog
Atom feed Atom feed for Keith's Weblog
Weblog archive
Recent comments
  on 3 posts

Recent comments XML

new⇒WoW Forums -> Tanking in Wrath of the Lich King

 cheap wow gold in www.wow4s.com or​www.agamegold.com.and cheap​powerleveli...

74.53.254.179: Aug 29, 11:19pm

new⇒Johnny Walker Blue Label

Wow, thanks for the scotch review​:D

Lagavulin and Laphroaig are​some of...

Keith: Aug 29, 3:35pm

Girls, please don't get breast implants

Wow, After all this time, the​comments on this page continue to​grow. It wa...

Ajeet: Aug 25, 2:36am

Generated in about 0.141s.

(Used 8 db queries)

mobile phone