Question

What are the issues that may arise if variables are declared globally, rather than being declared...

What are the issues that may arise if variables are declared globally, rather than being declared locally? State and discuss a C++ code example.
Are there any rules that should be followed in determining the scope of a variable? If so, what are they?​

Homework Answers

Answer #1

Issues with global variables:

Issues With Global Variables:

  • Global variables are memory locations that are directly visible to an entire software system.
  • The problem associated with global variables is that different parts of the software are coupled in ways that increase complexity and lead to subtle bugs.
  • Globals have system-wide visibility and are not same as static variables. Globals also increase the risk of data concurrency problems. Pointers to globals are the most evil things.
  • In its plainest form , a global variable is accessed directly by name rather than being passed as a parameter.

Following is the c++ example:

#include <iostream>

// variables declared outside of a block are global variables

int x; // global variable x

const int y; //global variable y

void seeThem()

{

   // global variables can be seen and used everywhere in program

   x =3;

   std::cout << y << endl;

}

int main()

{

   seeThem();

   // global variables can be seen and used everywhere in    program

   x = 5;

   std::cout << y << endl;

  

   return 0;

}

Rules that should be followed in determining the scope of a variable:

Rule 1:

The scope of an entity is the program or function

in which it is declared.

There is a direct consequence of Scope Rule 1. Since an entity declared in a function has a scope of that function, this entity cannot be seen from outside of the function.

Rule 2:

A global entity is visible to all contained functions,

including the function in which that entity is declared.

Rule 3:

An entity declared in the scope of another entity is always

a different entity even if their names are identical.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
GAAP revenue recognition standards are based on broad principles rather than bright-line rules. This creates a...
GAAP revenue recognition standards are based on broad principles rather than bright-line rules. This creates a certain amount of latitude in determining when revenue is earned. Assume a company that normally required acceptance by its customers prior to recording revenue as earned, delivers a product to a customer near the end of the quarter. The company believes customer acceptance is assured but cannot obtain it prior to quarter-end. Recording the revenue would assure “making its numbers” for the quarter. Although...
Note: Do not use classes or any variables of type string to complete this assignment Write...
Note: Do not use classes or any variables of type string to complete this assignment Write a program that reads in a sequence of characters entered by the user and terminated by a period ('.'). Your program should allow the user to enter multiple lines of input by pressing the enter key at the end of each line. The program should print out a frequency table, sorted in decreasing order by number of occurences, listing each letter that ocurred along...
Leave comments on code describing what does what Objectives: 1. To introduce pointer variables and their...
Leave comments on code describing what does what Objectives: 1. To introduce pointer variables and their relationship with arrays 2. To introduce the dereferencing operator 3. To introduce the concept of dynamic memory allocation A distinction must always be made between a memory location’s address and the data stored at that location. In this lab, we will look at addresses of variables and at special variables, called pointers, which hold these addresses. The address of a variable is given by...
1. In C++, programmers can use a class to create a large number of instance variables...
1. In C++, programmers can use a class to create a large number of instance variables of the class's type. Group of answer choices True False 2. When a C++ programmer declares a function, they are required to state all of the following except Group of answer choices {} The type of data, if any, returned by the function The function name The type of data, if any, sent to the function 3. When a C++ programmer calls a function,...
1. Pick a minimum of 20 observations on any subject. This will include a dependent variable...
1. Pick a minimum of 20 observations on any subject. This will include a dependent variable plus two independent variables that you may think are either negatively or positively correlated with the dependent variable. List the observed data (include the source). Then do the following: a. State before doing any calculations whether you think they are positively or negatively correlated. What is your rationale? Example: I test for a correlation between the quantity of coffee that people buy (Y) with...
Problem Description Many recipes tend to be rather small, producing the fewest number of servings that...
Problem Description Many recipes tend to be rather small, producing the fewest number of servings that are really possible with the included ingredients. Sometimes one will want to be able to scale those recipes upwards for serving larger groups. This program's task is to determine how much of each ingredient in a recipe will be required for a target party size. The first inputs to the program will be the recipe itself. Here is an example recipe that comes from...
This programming task will be a bit different. It will be more like what you would...
This programming task will be a bit different. It will be more like what you would receive if you were a maintenance engineer working in a corporate information systems or technology department. In other words, you will have some prebuilt source code provided for you that you must alter so it will meet the given programming specification.. Build a program from what you have been given as a starting-point. Rename the “starter code”. Do not add any modules; just, use...
**[70 pts]** You will be writing a (rather primitive) online store simulator. It will have these...
**[70 pts]** You will be writing a (rather primitive) online store simulator. It will have these classes: Product, Customer, and Store. All data members of each class should be marked as **private** (a leading underscore in the name). Since they're private, if you need to access them from outside the class, you should do so via get or set methods. Any get or set methods should be named per the usual convention ("get_" or "set_" followed by the name of...
What are the biggest challenges Costco will experience in trying to expand globally? Costco Wholesale Corporation...
What are the biggest challenges Costco will experience in trying to expand globally? Costco Wholesale Corporation is an American membership-only warehouse club selling a large range of products. The company opened its first warehouse in 1983 and currently is the second largest retailer in the U.S.—Walmart is number one. Costco, however, is the largest membership warehouse club chain in the U.S. Costco operates about 700 warehouses across the U.S. and abroad. As of November 2015, the company had international locations...
GUNS A well-regulated militia being necessary to the security of a free state, the right of...
GUNS A well-regulated militia being necessary to the security of a free state, the right of the people to keep and bear arms shall not be infringed. That is the full text of the 2nd amendment to the U.S. Constitution. If anyone tells you they KNOW! definitively what it means, they are actually giving you their opinion rather than an objective fact. They can’t possibly know, because it’s inherently ambiguous, as much as some people would like to believe otherwise....
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT