Jump to content

Best way to use global variables

- - - - -

  • Please log in to reply
5 replies to this topic

#1
rabintech

rabintech

    Newbie

  • Members
  • Pip
  • 2 posts
please tell me the best way to declare and define global variables?

#2
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
As far as I know, it's best to avoid global variables. But if you must and you're coding in C++ then use const instead of C macros as they are type safe. Also, I suggest you read a bit on heap and stack <here>.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#3
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
You want to be careful trying to use global variables in C/C++. The file scoping rules could end up biting you when you start writing multi-file programs. The best thing to do is start by thinking about why you even want a global variable. It's often a sign of poor code design.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#4
Groogy

Groogy

    Programmer

  • Members
  • PipPipPipPip
  • 183 posts
If it's C++ I would recommend that you use class variables instead and implement interface functions to access and work with the variable. I believe that's safer and gives it a more efficient way to work with the global data.
My Code Blog - My Github - Ascension Project - Madness Script Project - Simple-Garbage-Collector Project
There is bound to be something useful somewhere.

#5
outsid3r

outsid3r

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 623 posts
You might want to read some article about singleton classes.

Singleton pattern - Wikipedia, the free encyclopedia

#6
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts
A long long time ago (about 1987) I took over a fairly large C program that made extensive use of global variables. There were 20 or so *.c files an globals were declared in most of the, and used in many of the others. Trying to find out what was declared where was absolutely horrifying. I finally got sick of it and declded to consolidte all those global declarations into one *.c file and declared them all (usng extern keyword) in a header file. What I found was that a few globals were actually declared in more than one *.c file and the C compiler (Lattice C) just silently created duplicate data objects the same name as if they were declared with the static keyword. And that was the cause of several bugs that I was having difficulty finding/correcting.
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users