Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Global array question [modified]

Global array question [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
helpdata-structuresquestion
11 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Cristoff

    I've declared an array like that in a header (the code should be a pure C code): int someMap[MAPSIZE_X][MAPSIZE_Y]; and I want to use it as a global variable. The problem is if I include that header in another file I get a linker error that this variable was already defined and I'm really stuck here and I have absolutely no clue what's going on and how I'm missing the point. If I use static it is fine but it isn't global any more. "extern int gameMap[MAPSIZE_X][MAPSIZE_Y];" doesn't work as I'm expecting. I get the linker error even if I don't use the array in any files.

    modified on Thursday, April 7, 2011 6:34 AM

    _ Offline
    _ Offline
    _Superman_
    wrote on last edited by
    #2

    In the header file declare it as extern int someMap[MAPSIZE_X][MAPSIZE_Y]; Give the declaration int someMap[MAPSIZE_X][MAPSIZE_Y]; in any one of the .cpp files.

    «_Superman_»  _I love work. It gives me something to do between weekends.

    _Microsoft MVP (Visual C++)

    Polymorphism in C

    C 1 Reply Last reply
    0
    • _ _Superman_

      In the header file declare it as extern int someMap[MAPSIZE_X][MAPSIZE_Y]; Give the declaration int someMap[MAPSIZE_X][MAPSIZE_Y]; in any one of the .cpp files.

      «_Superman_»  _I love work. It gives me something to do between weekends.

      _Microsoft MVP (Visual C++)

      Polymorphism in C

      C Offline
      C Offline
      Cristoff
      wrote on last edited by
      #3

      Thank you for your reply. Well, my code is supposed to be a C code.

      _ 1 Reply Last reply
      0
      • C Cristoff

        Thank you for your reply. Well, my code is supposed to be a C code.

        _ Offline
        _ Offline
        _Superman_
        wrote on last edited by
        #4

        Just replace CPP in my reply with C.

        «_Superman_»  _I love work. It gives me something to do between weekends.

        _Microsoft MVP (Visual C++)

        Polymorphism in C

        C 1 Reply Last reply
        0
        • _ _Superman_

          Just replace CPP in my reply with C.

          «_Superman_»  _I love work. It gives me something to do between weekends.

          _Microsoft MVP (Visual C++)

          Polymorphism in C

          C Offline
          C Offline
          Cristoff
          wrote on last edited by
          #5

          Thank you! It was a lame problem. BTW Very interesting article.

          1 Reply Last reply
          0
          • C Cristoff

            I've declared an array like that in a header (the code should be a pure C code): int someMap[MAPSIZE_X][MAPSIZE_Y]; and I want to use it as a global variable. The problem is if I include that header in another file I get a linker error that this variable was already defined and I'm really stuck here and I have absolutely no clue what's going on and how I'm missing the point. If I use static it is fine but it isn't global any more. "extern int gameMap[MAPSIZE_X][MAPSIZE_Y];" doesn't work as I'm expecting. I get the linker error even if I don't use the array in any files.

            modified on Thursday, April 7, 2011 6:34 AM

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #6

            int someMap[MAPSIZE_X][MAPSIZE_Y]; allocates memory for an array; if you include that statement in a header file, then include that header file in N different C files, you end up allocating N arrays, all with the same name, and the linker will complain as it can't have data blocks with global scope and conflicting names. extern int someMap[MAPSIZE_X][MAPSIZE_Y]; says: "there is an array somewhere, but this by itself does not allocate it"; so inserting that in the header file tells your C files they get access to the array, without allocating memory at all, and without creating linker problems. Of course you still need a single int someMap[MAPSIZE_X][MAPSIZE_Y]; in one of the C files to actually allocate the memory; and that C file better also include the header file with the extern statement. [ADDED] One elegant way of dealing with all this is described in Stefan63's message below. [/ADDED] :)

            Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

            Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

            modified on Thursday, April 7, 2011 9:02 AM

            C 1 Reply Last reply
            0
            • C Cristoff

              I've declared an array like that in a header (the code should be a pure C code): int someMap[MAPSIZE_X][MAPSIZE_Y]; and I want to use it as a global variable. The problem is if I include that header in another file I get a linker error that this variable was already defined and I'm really stuck here and I have absolutely no clue what's going on and how I'm missing the point. If I use static it is fine but it isn't global any more. "extern int gameMap[MAPSIZE_X][MAPSIZE_Y];" doesn't work as I'm expecting. I get the linker error even if I don't use the array in any files.

              modified on Thursday, April 7, 2011 6:34 AM

              S Offline
              S Offline
              Stefan_Lang
              wrote on last edited by
              #7

              If you have plenty of such variables or declarations you can use a macro to automatically use the declaration in your header file as a definition exactly once:

              // globals.h
              #ifndef GLOBALS_H
              #define GLOBALS_H

              #ifdef GLOBALS_C

              define MYGLOBAL_API

              #else

              define MYGLOBAL_API extern

              #endif

              MYGLOBAL_API int someMap[MAPSIZE_X][MAPSIZE_Y];

              #undef MYGLOBAL_API // clean up - we won't need that symbol outside this header
              #endif

              // globals.c
              #define GLOBALS_C // MYGLOBAL_API will be resolved to empty string
              #include "globals.h" // defines the globals

              // every_other_soucre_file.c
              #include "globals.h" // declares the globals, but doesn't define them
              //...

              Using this technique, adding another global variable just requires adding it to globals.h, you never have to touch globals.c (or any other file) for that purpose! That said, it is always a good idea to keep the number of globals to a minimum, or, if possible, not use any at all. It's always very difficult to track an error that leaves a global in a bad state, because the problem may be caused anywhere in your program!

              A C 2 Replies Last reply
              0
              • S Stefan_Lang

                If you have plenty of such variables or declarations you can use a macro to automatically use the declaration in your header file as a definition exactly once:

                // globals.h
                #ifndef GLOBALS_H
                #define GLOBALS_H

                #ifdef GLOBALS_C

                define MYGLOBAL_API

                #else

                define MYGLOBAL_API extern

                #endif

                MYGLOBAL_API int someMap[MAPSIZE_X][MAPSIZE_Y];

                #undef MYGLOBAL_API // clean up - we won't need that symbol outside this header
                #endif

                // globals.c
                #define GLOBALS_C // MYGLOBAL_API will be resolved to empty string
                #include "globals.h" // defines the globals

                // every_other_soucre_file.c
                #include "globals.h" // declares the globals, but doesn't define them
                //...

                Using this technique, adding another global variable just requires adding it to globals.h, you never have to touch globals.c (or any other file) for that purpose! That said, it is always a good idea to keep the number of globals to a minimum, or, if possible, not use any at all. It's always very difficult to track an error that leaves a global in a bad state, because the problem may be caused anywhere in your program!

                A Offline
                A Offline
                Albert Holguin
                wrote on last edited by
                #8

                macros are wonderful when you know how to use them :)

                1 Reply Last reply
                0
                • L Luc Pattyn

                  int someMap[MAPSIZE_X][MAPSIZE_Y]; allocates memory for an array; if you include that statement in a header file, then include that header file in N different C files, you end up allocating N arrays, all with the same name, and the linker will complain as it can't have data blocks with global scope and conflicting names. extern int someMap[MAPSIZE_X][MAPSIZE_Y]; says: "there is an array somewhere, but this by itself does not allocate it"; so inserting that in the header file tells your C files they get access to the array, without allocating memory at all, and without creating linker problems. Of course you still need a single int someMap[MAPSIZE_X][MAPSIZE_Y]; in one of the C files to actually allocate the memory; and that C file better also include the header file with the extern statement. [ADDED] One elegant way of dealing with all this is described in Stefan63's message below. [/ADDED] :)

                  Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                  Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                  modified on Thursday, April 7, 2011 9:02 AM

                  C Offline
                  C Offline
                  Cristoff
                  wrote on last edited by
                  #9

                  Wow! Amazing explanation! Thank you so much! :)

                  L 1 Reply Last reply
                  0
                  • S Stefan_Lang

                    If you have plenty of such variables or declarations you can use a macro to automatically use the declaration in your header file as a definition exactly once:

                    // globals.h
                    #ifndef GLOBALS_H
                    #define GLOBALS_H

                    #ifdef GLOBALS_C

                    define MYGLOBAL_API

                    #else

                    define MYGLOBAL_API extern

                    #endif

                    MYGLOBAL_API int someMap[MAPSIZE_X][MAPSIZE_Y];

                    #undef MYGLOBAL_API // clean up - we won't need that symbol outside this header
                    #endif

                    // globals.c
                    #define GLOBALS_C // MYGLOBAL_API will be resolved to empty string
                    #include "globals.h" // defines the globals

                    // every_other_soucre_file.c
                    #include "globals.h" // declares the globals, but doesn't define them
                    //...

                    Using this technique, adding another global variable just requires adding it to globals.h, you never have to touch globals.c (or any other file) for that purpose! That said, it is always a good idea to keep the number of globals to a minimum, or, if possible, not use any at all. It's always very difficult to track an error that leaves a global in a bad state, because the problem may be caused anywhere in your program!

                    C Offline
                    C Offline
                    Cristoff
                    wrote on last edited by
                    #10

                    That's very interesting thank you!

                    1 Reply Last reply
                    0
                    • C Cristoff

                      Wow! Amazing explanation! Thank you so much! :)

                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #11

                      you're welcome. In software development, getting things to work may be the goal, understanding how each part works is a necessary step to get things to work reliably. :)

                      Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                      Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups