What is a 'scope'

In general what is a scope in a programming language? For example, in C/C++ each function is a scope. The compiler uses a stack in processor for all variables in a function. Different stacks for different functions. So are scopes maintained on different stacks too?
https://en.wikipedia.org/wiki/What_happens_on_tour,_stays_on_tour

Variables declared within scope are visible only within the scope and vanish at the end of the scope.

A scope can be inside another scope. Being within it can see the variables of the outer scope(s).

How compiler implements scopes is a lesser concern (except for compiler developers).


Another language. \textit and \textbf are used there as nested scopes:
http://tex.stackexchange.com/questions/41681/correct-way-to-bold-italicize-text

A scope is defined by an opening and closing brace:

1
2
3
4
5
6
7
8
9
10
void function1()
{  
    // outer scope begins
    // ...
   {
       // inner scope begins : both scopes applicable
       //
   }  // inner scope ends
   // outer scope applicable
}  // outer scope ends 


Steven
The scopes are probably maintained by some kind of node tree, any variables created would be created in the "current scope" node. When trying to reference any variables, if the referenced variable is not found in the "current scope", it wouid traverse backward through the node tree trying to find that variable until it gets to the first tree, say node[0]. If it isn't found there, I imagine you would get a compilation error.
From an application programmer's point of view, the mechanism about how a scope is implemented, is not very important. It may / may not be on a stack; the underlying mechanism is irrelevant to an app programmer.

What _is_ important is whether a variable is visible or not ie: whether it is in scope or not, and that is as has been explained.

Stacks come into the picture when a function is being invoked, because the arguments are passed to the function on a stack. Therefore, each function call involves a different stack. Note that the stack really comes into the picture (for the app programmer) when the function is _called_ rather when it is _defined_.
Nonetheless, all program variables that are not objects are defined on the stack.

Objects (instances of classes) are defined on the heap.

Steven
SSteven wrote:
Objects (instances of classes) are defined on the heap.
No, local variables are stored on the stack.

By the way: There is only one stack. If variables (or an infinite/to deeply nested recursion) needs too much stack space you will corrupt the stack.
No, local variables are stored on the stack.


Local variables are stored on the stack provided they _not_ objects.
Egs: An int is not an object. It is a fundamental type.

Therefore, an int will be stored on the stack. This is true for for all ints, whether local or global vars.

However if you have a class and then instantiate it. Eg:

Class1 o = Class1();

Now o is an object; not a fundamental datatype. It will be created on the heap.
@SSteven

I guess that you are thinking of another language (C#).

In C++ there is no difference between 'fundamental' and other types. Hence int is an object [type] as well.

object are stored on the heap when using new otherwise they are stored on the stack.

This

Class1 o = Class1();

creates an object o on the stack and the default constructor is called ( = Class1() can be omitted)


Global objects are not created on the stack. They are stored in their own global memory segment.
Local variables are stored on the stack provided they _not_ objects.


Just to repeat coder777, as he says; simply not true. You must be thinking of some other language.
Topic archived. No new replies allowed.