Thread: Global vs Local gives different results.

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    46

    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" );
        }
    }
    Last edited by rafe; 11-14-2002 at 11:29 AM.

  2. #2
    Registered User
    Join Date
    Oct 2002
    Posts
    46
    More experiments. Curiouser...

    I can move this inside the routine & it works
    Code:
    double Mat1[20][20] = { /* Constant values go here */ };
    But not...
    Code:
    double Mat[512][20][20];
    Which means that the malloc() is entirely approprate here. But required?!

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    By my calculations, the array you are using occupies close to 1.5 Megs of space. I'm not sure of the limits but I think this is too much to be placed onto the stack within a function; having it as a global is fine. You could use a pointer variable and dynamically allocate this space within the function instead and it should work fine then.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    46

    Smile

    Got it. Thanks to both. Yea, it's big struct... so much for being lazy.

    [edit]FYI "ulimit -s" gives "unlimited". But your explainations seem more on target than this bash nonsense.[/edit]
    Last edited by rafe; 11-14-2002 at 12:20 PM.

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    By gcc I assume you are using Linux. If not I assume you are using Cygwin. In either case I am positive that I've seen a option that allows you to do what you are trying to do without changing any of your original code. Perhaps you could even use the one that allows large objects. But I guess I'm too late anyway, it looks like this issue has been resolved.

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    46
    Well, I'm always looking for a way to be a lazier programer (-; So if you have a particular suggestion I'll give it a whorl.

    BTW: Linux/gcc and the command line is:
    gcc -lm -o calc calc.c && ./calc

    I had a scan thru the man pages for every option about stacks and the -fno-stack-limit seemed to be the logical choice. But sad to say it didn't work, it still gives k'flooey numbers. [edit]I was going to try the -mcode-model=large & the -mlarge-data switches but no need. I introduced a bug in my test code AND it DOES work! 100 times...
    I will check my work before I post.
    I will check my work before I post.
    I will...[/edit]
    Last edited by rafe; 11-15-2002 at 11:49 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 12:54 AM
  2. Static Local Variable vs. Global Variable
    By arpsmack in forum C Programming
    Replies: 7
    Last Post: 08-21-2008, 03:35 AM
  3. Global Variables, include files and classes
    By sharpstones in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2005, 10:06 AM
  4. Global coordinates in OpenGL?
    By IcyDeath in forum C++ Programming
    Replies: 1
    Last Post: 11-25-2004, 06:29 PM
  5. global and local variables
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-02-2001, 01:17 PM