Thread: global variable

  1. #1
    Registered User stautze's Avatar
    Join Date
    Apr 2002
    Posts
    195

    global variable

    i need to make a dynamic global 2d array....

    will this work....

    Code:
    //inside of header file, but outside of  function
    int p**
    
    //function to create dynamic array
    dynamicArray(int num_rows, int num_cols)
    {
      p = new int*[num_rows];
    for (int i = 0 ; i < num_rows; i++ ) 
      p[i] = new int[num_cols];
    }
    'During my service in the United States Congress, I took the initiative in creating the Internet.' - Al Gore, March 9, 1999: On CNN's Late Edition

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Don't declare your pointer-pointer in the header, do it in the actual cpp file instead.

    Make sure the memory is deleted afterwards too, to prevent memory leaks. delete the pointers in the array first before deleteing the pointer-pointer (is there another name for this?).

    Also, test if your memory allocations succeeds before attempting to access them.

    Other than that, it should work fine.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User stautze's Avatar
    Join Date
    Apr 2002
    Posts
    195
    what if i have functions in several different cpp files that all need the global array? Is this possible?
    'During my service in the United States Congress, I took the initiative in creating the Internet.' - Al Gore, March 9, 1999: On CNN's Late Edition

  4. #4
    Registered User stautze's Avatar
    Join Date
    Apr 2002
    Posts
    195
    sorry, one more question. I know how to delete, but how do i test to make sure the memory is allocated.
    'During my service in the United States Congress, I took the initiative in creating the Internet.' - Al Gore, March 9, 1999: On CNN's Late Edition

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    You mean when linking your files? It is possible to share one global variable, if you declare it in every cpp file that uses it. You must declare all of them but one as extern.
    Code:
    extern int MyVar;
    To check if a memory allocation was successful, check if the pointer differs from NULL. If it equals to NULL, it failed.
    Code:
    int* MyVar;
    MyVar = new int;
    if(MyVar != NULL)
    {
       //Do stuff
    
       delete MyVar;
    }
    else
    {
       cout << "Failed!";
    }
    
    
    int* MyArray;
    MyArray = new int[1024];
    if(MyArray != NULL)
    {
       //Do stuff
    
       delete[] MyArray;
    }
    else
    {
       cout << "Failed!";
    }
    (notice the [] on delete when deleting an array)
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Magos is correct, and this is something that I see too often. The main reason you don't want to do this is when mulitple files use the header with the pointer. What you should do is this.

    Code:
    //inside header
    extern int **ptr;
    
    //inside ONE of your files
    int **ptr;
    
    //in one of your files
    void dynamicArray(int num_rows, int num_cols)
    {
      p = new int*[num_rows];
    for (int i = 0 ; i < num_rows; i++ ) 
      p[i] = new int[num_cols];
    }
    Okay, there are a couple of ways to make sure that memory is allocated.
    Code:
    void dynamicArray(int num_rows, int num_cols)
    {
      using namespace std;
      try {
        p = new int*[num_rows];
      } catch(bad_alloc e) {
        cerr << e.what();
        return;
      }
      for (int i = 0 ; i < num_rows; i++ ) {
        try {
          p[i] = new int[num_cols];
        } catch(bad_alloc e) {
          cerr << e.what();
          return;
        }
      }
    }
    
    //alternatively
    
    void dynamicArray(int num_rows, int num_cols)
    {
      using namespace std;
      p = new int*[num_rows];
      if(!p) {
        cerr << "not enough memory" << endl;
        return;
      }
      for (int i = 0 ; i < num_rows; i++ )  {
        p[i] = new int[num_cols];
        if(!p) {
          cerr << "not enough memory" << endl;
          return;
        }
      }
    }

  7. #7
    Registered User stautze's Avatar
    Join Date
    Apr 2002
    Posts
    195
    thanks for the replies, i understand every thing except how to declare the global variable. What i really have is an array of structs.

    Code:
    typedef struct tile {
      bool furniture;
      bool cat;
      bool vacuum;
      int dirt;
    } Tile;
    
    Tile p**;
    So as I understand, I have two choices:

    choice 1: declare the struct and the pointer in all of the cpp files and not in any header files. Also making one of the declarations an extern.

    choice 2: declare the struct and the pointer in a header as an extern and declare the struct and pointer in only one of the cpp files.

    are both ways correct, or have i misunderstood?
    'During my service in the United States Congress, I took the initiative in creating the Internet.' - Al Gore, March 9, 1999: On CNN's Late Edition

  8. #8
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    I've made several tests on extern not long ago. Though it may be informative for other readers to declare the variable as extern in the header file, this is not neccessary (but doable).
    It works fine without it.

    Just declare the variable in all of your cpp files. One normally, the rest as extern. Which file you place the non-extern variable in doesn't matter.

    Code:
    Main.cpp
    
    Tile** p;
    Code:
    OtherFile.cpp
    
    extern Tile** p;
    Code:
    AnotherFile.cpp
    
    extern Tile** p;
    Code:
    YourHeader.h
    
    //Notice, no declaration is needed
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  9. #9
    Registered User stautze's Avatar
    Join Date
    Apr 2002
    Posts
    195
    thanks for the good answer. I still have another question though.

    All i need to do with the struct defintion then is to put it in the header file that is included in every cpp file?

    Code:
    Main.cpp
    #include "header.h"
    Tile** p;
    Code:
    OtherFile.cpp
    #include "header.h"
    extern Tile** p;
    Code:
    AnotherFile.cpp
    #include "header.h"
    extern Tile** p;
    Code:
    YourHeader.h
    typedef struct tile {
      bool furniture;
      bool cat;
      bool vacuum;
      int dirt;
    } Tile;
    
    //Notice, no declaration is needed
    [/B][/QUOTE]
    'During my service in the United States Congress, I took the initiative in creating the Internet.' - Al Gore, March 9, 1999: On CNN's Late Edition

  10. #10
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Yep
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  11. #11
    Registered User stautze's Avatar
    Join Date
    Apr 2002
    Posts
    195
    thanks
    'During my service in the United States Congress, I took the initiative in creating the Internet.' - Al Gore, March 9, 1999: On CNN's Late Edition

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. global variable and prototype
    By Lincoln_Poh in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2008, 03:25 AM
  2. static class variable vs. global variable
    By nadamson6 in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2005, 03:31 PM
  3. global variable turns zero
    By Roaring_Tiger in forum C Programming
    Replies: 5
    Last Post: 04-07-2003, 12:52 PM
  4. Global variable???
    By loopy in forum C Programming
    Replies: 13
    Last Post: 06-09-2002, 03:08 PM