Hi can anyone tell me if global variables are not liked to be used and why, and whether local variables are a preferred or not and why??

thanks if you can help

Basically, why use global variables when they aren't necessary. You have tighter control over local variables. You decide what sees them.

Some situations I find global variables are useful, like in a program that has mutliple files when passing parameters to functions isn't practical. That's my opinion.

Global variables should be avoided, because imagine u have 4000 lines of code, in which u have define many functions to perform different tasks, and also u have a global variable which is used nearly by evey function, and everything is working fine. Now if you want to define a new function to peform some task in which u have to change the value of that variable (or u acidently changed its value). Then u can imagine how difficult it would be to check the dependies of that global variable in every function. Best way to avoid problems, is to use global variable as constant (Use uppercase to name global variables). And one thing more they occupy memory till the program teminates.

It is good to use Local variable because, fristly they donot effect other part of the program. And it dies as soon as the block ends.

For example u have a long array. And u need that array in one function only. then it is better to define that long array local to that function. Imagine if u make that array global, it would be waste of precious memory that would be hold by that long array for whole life of the program, even if it is required in one function only.


Better to move to OOPS

global variables are evil.

most beginners don't realize this, because in short little school programming assignments , no one cares.

it becomes a major problem is "the real world" when people start relying on global variables as "duct tape" for poor program specification and implementation.

because in the real world you have tens- or hundreds of thousands of lines of code that are often written by several people, and have to be maintained by many more people and very soon, no one knows WTF all those global variables are for.

>can anyone tell me if global variables are not liked
Experienced programmers tend to cringe when they see global variables.

>and why
Global variables are almost always an indication of poor design. They're such a small and innocent seeming thing, but the repercussions can be truly frightening if you know what to look for.

A good analogy for global variables is throwing a stone into a pool of water. You throw the stone in the pool and it makes a little splash. Then there are ripples all the way to the edges of the pool. Then the stone wedges itself in a submerged gas vent that builds up pressure for several days and explodes spectacularly, causing untold pain and chaos. ;)

commented: LOL :) +2

So local variables are only used by a specific function and global variables affect the whole program possibly in a bad way?

18 years ago I got this nice new job and I had to take over maintenance of a C program that was written by someone else long gone. The program was written for MS-DOS 3.1 operating system and had about 20 *.c files with lots of global variables scattered around all those files. Maintenance of those files quickly became a nightmare because of all those globals so I consolidated them all into one *.c file. Globals were used to help speed up the program -- function calls in a computer that only ran 10 Mz or so is very very expensive so globals was one way to improve performance.

If that same program were written today I would have strangled the original programmer and just trashed the program.

commented: "strangled the original programmer and just trashed the program" ... i dont know how many times ive had that exact thought. +2

I'm actually working with a program for a Fujitsu uController at the moment. Written by (with all respect) an old-school programmer. static this, extern that... bleh..
I'm more of a 'throw pointers around' kind of guy :)

Hi Ancient Dragon,
The time u are talking about (18 years back), Speed as well as memory was also important.

^ i dont know how that fact informs the discussion here. It certainly doesnt change the fact that global variables/pointers are generally, if not always, evidence of sloppy programming.

perhaps sometimes, in some contexts, when sparingly used, they might be useful... but only when they are specified from the beginning and clearly limited to a specific and well-defined functionality.

but not as an afterthought, not as a quick-fix, and not because they are convenient.

Well, I tend to use them when it saves me adding numerous parameters to some functions. I think you can use global variables, but you should at least limit most of them to one source file with the static modifier (or without, since it's the default). When you write a throw away program, it's okay to use them, but globals aren't built to last.

if you ever work on any big project you're going to be in trouble.

because if you have to work with any coworkers who develop code the right way, they're going to make you look bad ...

then people will curse your name after you've left (or been asked to leave) your job and your code is handed off to future developers/maintainers.

here's a hint: "struct"


.

When I consolidated all those 50+ globals into one *.cpp file I found several errors that had gone undetected, such as an integer with the same name had been declared globally in about three different files and god only knows what the compiler did to resolve that problem.

This program was written before function prototypes became popular. After taking the time to make a header file that contained all the function prototypes the compiler was able to identify several places in which function calls had either the wrong parameters or missing parameters. Function prototypes was another very major improvement.

as far as i feel that we need both Variables. but depend on the Situation u can use global variables. but mostly i prefered to go with local variables. but if u feel that u badly want one ore two global.. thats fine but not much

um, okay... thanks?

I'm writing this windows app and I can't figure out how to let the user manipulate data (in a function called by WndProc) without using globals. Do you think it's better to use one global array/struct than lots of tiny globals? That'd make sense.

why don't you just pass a pointer to a struct into the function?

So, declare a static struct in WndProc and pass a pointer... That makes sense. Thanks.

no, i would declare a normal struct in the program that *calls* WndProc (most likely, your "main" routine) and, from there, pass a pointer to the struct as an argument to WndProc


.

WndProc is a callback function. I don't call it.

oh, well that changes things. you're talkign about the Windows API that handles windows events.

In that case you should not be processing any data in WndProc, itself. Ideally this function will only be used to report what actions the user has performed on the window. specific data processing should be handled by other (non-callback) functions.

in any event, this thread was a general discussion about *why* globals should not be used, not *how* globals should not be used. Perhaps your problem would be better served in its own specific thread.


.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.