English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

C programming

2007-01-09 00:54:03 · 5 answers · asked by Anonymous in Computers & Internet Programming & Design

5 answers

nothing

2007-01-09 01:07:28 · answer #1 · answered by Anonymous · 0 3

Lets suppose you want to make a program to sell fruits and vegetables at a stand. You would need a variable to store price, right, perhaps another to store description. Customer buys an orange, $.50, apple, $1.00. Oh look we already have a problem because our program only has room for 1 price and 1 description.

It is easy to solve this problem we can create a global array to store both price and description. Lets make it big enough to hold 100 items, that should be enough. What happens to the customer that buys 101 items?. More important what happens to your computer memory. Several cashiers may work in your store, say 10. Just to be safe we should make a global array for 20 cashiers. Your use of RAM memory must take into consideration all possible extremes. Perhaps you sell avocados and grapefruit. Lets make that array large enough for all extremes.

I have a better idea. If you buy an orange the purchase method can allocate memory room for the description and price and then store the value in a local (dynamic) variable. If the customer decides to purchase again, no problem call the purchase method, allocate more memory, on the fly, dynamic, just enough for the job.

The digital world is much like our own. The ownership of private property is important, sacred. I own property, a house and have money. If you invade my home without invitation and take my money you will have a problem, I might just take advantage of the situation and take your money, your life your wife. This is not the rule of law.

With computers the operating system is the rule of law. If you have need to save something, just ask. What assets does a computer and its operating system have to offer? Well memory for one, CPU time, disk storage, access to external devices, printers, the display, internet connection. The worst thing that can happen is you program will be denied.

Global variables are only there once. If you change their value, and someone else changes their value, what have we learned?

2007-01-09 09:19:52 · answer #2 · answered by Anonymous · 0 0

They are usually considered bad practice precisely because of their nonlocality: a global variable can potentially be modified from anywhere, and any part of the program may depend on it. A global variable therefore has an unlimited potential for creating mutual dependencies, and adding mutual dependencies increases complexity. See Action at a distance. However, in a few cases, global variables can be suitable for use. They can be used to avoid having to pass frequently-used variables continuously throughout several functions, for example.

Global variables are used extensively to pass informations between sections of code that don't share a caller/callee relation like concurrent threads and signal handlers. Languages where each file defines an implicit namespace eliminate most of the problems seen with languages with a global namespace though some problems may persist without proper encapsulation. Without proper locking (such as with a mutex), code using global variables will not be thread-safe.

2007-01-09 01:01:57 · answer #3 · answered by Bala 2 · 2 1

1) They comsumes memory space throughout program life cycles.
2) They sometime mixup concept with local variables having same name.
3) Variable values can be altered from anywhere in the programs.
e.g. Game Trainers works changes the global variables of the game.NFS Carbon, NFS U2, etc

2007-01-09 01:07:56 · answer #4 · answered by Talha 4 · 0 0

any function can access the global variable so changes can be made by different functions.

2007-01-09 01:19:33 · answer #5 · answered by sunny 2 · 1 1

fedest.com, questions and answers