Wrox Programmer Forums
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old May 30th, 2005, 12:22 PM
Registered User
 
Join Date: Apr 2005
Posts: 6
Thanks: 0
Thanked 1 Time in 1 Post
Default Why avoid global variabel?

Many books on programming often avoid using global variable because it is considered harmful. Why is that? Does global variable use lesser instruction because it is treated as static by compiler, so it have more performance than locals ( which is treated as stack dynamic ).



Precision is not truth!
Reply With Quote
The Following User Says Thank You to fuehrer For This Useful Post:
  #2 (permalink)  
Old May 31st, 2005, 02:57 AM
Authorized User
 
Join Date: Mar 2005
Posts: 58
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to Alan-LB Send a message via Yahoo to Alan-LB
Default

A Global Variable is accessible by all functions in the program. This means that it may be changed anywhere in the program by mistake. If you have a large program (especially one that several porgrammers are working on) global variables can cause a lot of problems.

Local variables are visible only within the function in which they are created and cease to exist once the program returns from the function.

It is much safer to pass variables into functions as parameters. The parameters passed in have the same scope as local variables within the function. This way a much tighter control over the use of variables exists and mistakes are fewer. It is also much easier to debug a program if you have a tight control over which functions use any particular variable.

Alan


Reply With Quote
  #3 (permalink)  
Old June 14th, 2005, 09:02 PM
Registered User
 
Join Date: Jun 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Is'nt there a slight performance gain as well, using local variables? More effecient use of memory or something like that.

Reply With Quote
  #4 (permalink)  
Old June 15th, 2005, 03:14 AM
Authorized User
 
Join Date: Mar 2005
Posts: 58
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to Alan-LB Send a message via Yahoo to Alan-LB
Default

Local variables are allocated on stack memory (the stack frame for the function)only when the function starts and the memory is automatically released when the function returns. This means that there are not large chunks of memory allocated that may not be used at the time.

Global variables are allocated memory in the Data Segment when the program loads and stay allocated for the whole life of the program. This may be OK if you have variables that are used by many functions and if it makes sense to have them as Globals. In general, global variables should be avoided if it is reasonable to do so.

You may also allocate Global (outside the main() function)Dynamic Variables on the Heap memory using the "new" function and the memory must be released by using the "delete" function when the program terminates. Dynamic Variables may also be allocated from within any function but they must be released before leaving the function otherwise they memory stays allocated but you may not access it because it is no longer in scope. This is a "memory leak".

Alan


Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to declare the global variable in global.asax? calyn_gately ASP.NET 3.5 Basics 0 August 6th, 2008 08:06 PM
how to avoid logon justin_min Crystal Reports 0 November 30th, 2004 02:05 PM
Trying to avoid duplicacy aspadda Excel VBA 0 April 23rd, 2004 12:59 PM
How do I Avoid a ROLLBACK when... wmhhodson SQL Server 2000 1 October 31st, 2003 01:07 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.