Global Vs Local Variables

This is a discussion on Global Vs Local Variables within the C++ Programming forums, part of the General Programming Boards category; I wouldnt be too confident from just using a few simple examples Remeber that the compiler can optimize and make ...

  1. #16
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    I wouldnt be too confident from just using a few simple examples

    Remeber that the compiler can optimize and make all the difference in simple cases, but if your task becomes complex and more involved, then that optimisation might not be applied....

    Interestingly......I tried FYB's code on VC++6 & Codewarrior 5...

    For VC++6 debug, they were roughly the same with local winning by a little bit,

    For Codewarrior debug, the local took about the same as the local in VC++, but the global was optimised to a count of 0.

    For both compilers, when put into default release mode, they both optimised both to zero.....

  2. #17
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    hmmm

    i suspect in the case i was referring to globals are better simply for ease of use, it's i nthe case of students learning the language. The teacher isn't helping much so I say the hell with passing locals to functions if she won't explain it, i feel sorry for my friend, so I'm still commited to globals in that respect.
    You can always optimize one way or the other later (whichever the case may be in a true speed test)
    PHP and XML
    Let's talk about SAX

  3. #18
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    One of my textbooks says that static variables will be faster
    than stack variables.
    But I've read this in a manual to another compiler and from msdn http://msdn.microsoft.com/library/de...mizingcode.asp
    I think I'm going with msdn on this one

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Craps Program with Local Variables
    By tigrfire in forum C Programming
    Replies: 12
    Last Post: 11-09-2005, 08:01 AM
  2. local and global variables???
    By geo_c in forum C Programming
    Replies: 5
    Last Post: 08-23-2004, 03:02 PM
  3. Replies: 6
    Last Post: 01-02-2004, 12:01 PM
  4. Global variables.. how bad?
    By punkrockguy318 in forum C++ Programming
    Replies: 19
    Last Post: 11-30-2003, 09:53 PM
  5. global and local variables
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-02-2001, 01:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21