Click here to Skip to main content
15,887,214 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Global array question Pin
«_Superman_»7-Apr-11 0:18
professional«_Superman_»7-Apr-11 0:18 
GeneralRe: Global array question Pin
Cristoff7-Apr-11 0:30
Cristoff7-Apr-11 0:30 
GeneralRe: Global array question Pin
«_Superman_»7-Apr-11 0:34
professional«_Superman_»7-Apr-11 0:34 
GeneralRe: Global array question Pin
Cristoff7-Apr-11 0:59
Cristoff7-Apr-11 0:59 
AnswerRe: Global array question [modified] Pin
Luc Pattyn7-Apr-11 1:48
sitebuilderLuc Pattyn7-Apr-11 1:48 
GeneralRe: Global array question Pin
Cristoff8-Apr-11 0:14
Cristoff8-Apr-11 0:14 
GeneralRe: Global array question Pin
Luc Pattyn8-Apr-11 0:19
sitebuilderLuc Pattyn8-Apr-11 0:19 
AnswerRe: Global array question PinPopular
Stefan_Lang7-Apr-11 2:18
Stefan_Lang7-Apr-11 2:18 
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!
GeneralRe: Global array question Pin
Albert Holguin7-Apr-11 3:51
professionalAlbert Holguin7-Apr-11 3:51 
GeneralRe: Global array question Pin
Cristoff8-Apr-11 0:16
Cristoff8-Apr-11 0:16 
Questionmemory leak problem Pin
shashankacharya6-Apr-11 18:35
shashankacharya6-Apr-11 18:35 
AnswerRe: memory leak problem PinPopular
Rajesh R Subramanian6-Apr-11 22:02
professionalRajesh R Subramanian6-Apr-11 22:02 
Questionupdate structure in a vector of structures Pin
csrss6-Apr-11 8:53
csrss6-Apr-11 8:53 
AnswerRe: update structure in a vector of structures Pin
csrss6-Apr-11 9:19
csrss6-Apr-11 9:19 
AnswerRe: update structure in a vector of structures Pin
Niklas L6-Apr-11 20:55
Niklas L6-Apr-11 20:55 
GeneralRe: update structure in a vector of structures Pin
csrss7-Apr-11 2:14
csrss7-Apr-11 2:14 
AnswerRe: update structure in a vector of structures Pin
Parthi_Appu6-Apr-11 23:32
Parthi_Appu6-Apr-11 23:32 
GeneralRe: update structure in a vector of structures Pin
csrss7-Apr-11 2:18
csrss7-Apr-11 2:18 
AnswerRe: update structure in a vector of structures [modified] Pin
Stefan_Lang7-Apr-11 4:01
Stefan_Lang7-Apr-11 4:01 
GeneralRe: update structure in a vector of structures Pin
csrss7-Apr-11 7:11
csrss7-Apr-11 7:11 
GeneralRe: update structure in a vector of structures Pin
Stefan_Lang7-Apr-11 20:44
Stefan_Lang7-Apr-11 20:44 
QuestionHow to create setup file of an EXE in Visual Studio 6.0 ? Pin
pjhelp6-Apr-11 4:10
pjhelp6-Apr-11 4:10 
AnswerRe: How to create setup file of an EXE in Visual Studio 6.0 ? Pin
Hans Dietrich6-Apr-11 4:13
mentorHans Dietrich6-Apr-11 4:13 
AnswerRe: How to create setup file of an EXE in Visual Studio 6.0 ? Pin
DanYELL6-Apr-11 16:16
DanYELL6-Apr-11 16:16 
GeneralRe: How to create setup file of an EXE in Visual Studio 6.0 ? Pin
Albert Holguin6-Apr-11 17:05
professionalAlbert Holguin6-Apr-11 17:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.