Global vs Local gives different results.
Riddle me this...
Using gcc I have a setup routine that I'm building up. I'm calculating a 3D array of doubles, an array of matrices if you will. If I put the arrays on the stack the program goes fubar, but if I make them globals all is OK. What gives? I was being lazy & because I know the dimensions I was avoiding the malloc().
Snippet sanitized for your convience.
Code:
#include <stdlib.h>
#include <math.h>
/* If these are moved into the function then fubar */
double Mat[512][20][20];
double Mat1[20][20] = { /* Constant values go here */ };
void Init( void );
int main( int argc, char** argv ) {
Init();
}
void Init( void ) {
int i, j, k, m;
// Can't put these here?! But I want to because they're temp arrays
//double Mat[512][20][20];
//double Mat1[20][20] = { /* Constant values go here */ };
/* Init matrices */
for ( i = 0; i < 32; ++i )
for ( j = 0; j < 32; ++j )
Mat1[i][j] = Mat[1][i][j] = Mat1[i][j] / 10000;
/* Now multiply matrices out */
for ( m = 2; m < 512; ++m )
for ( i = 0; i < 20; ++i )
for ( j = 0; j < 20; ++j )
for ( k = 0; k < 20; ++k )
Mat[m][i][j] += Mat1[i][k] * Mat[m-1][k][j];
/* Testing */
for ( i = 0; i < 20; ++i ) {
for ( j = 0; j < 20; ++j ) printf( "%6.4f ", Mat1[i][j] );
printf( "\n" );
for ( j = 0; j < 20; ++j ) printf( "%6.4f ", Mat[511][i][j] );
printf( "\n\n" );
}
}