Memory.

I am reading an e-book and i don't really understand what he means with the last 3 lines i wrote below in quotes. Can someone please help me understand this.




The function main() immediately invokes parent(). The first thing that the
processor sees in parent() is the declaration of intParent. At that point,
intParent goes into scope — that is, intParent is defined and available for the
remainder of the function parent().
The second statement in parent() is the call to child(). Once again, the function child() declares a local variable, this time intChild. The scope of the
variable intChild is limited to the function child(). Technically, intParent is not
defined within the scope of child() because child() doesn’t have access to
intParent; however, the variable intParent continues to exist while child() is
executing.
When child() exits, the variable intChild goes out of scope. Not only is intChild
no longer accessible, it no longer exists. (The memory occupied by intChild is
returned to the general pool to be used for other things.)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// the following variable is accessible to
// all functions and defined as long as the
// program is running(global scope)
int intGlobal;
// the following variable intChild is accessible
// only to the function and is defined only
// as long as C++ is executing child() or a 
// function which child() calls (function scope)
void child(void)
{
 int intChild;
}
// the following variable intParent has function
// scope
void parent(void)
{
 int intParent = 0;
 child();
 int intLater = 0;
 intParent = intLater;
}
int main(int nArgs, char* pArgs[])
{
 parent();
}
When child() exits, the local variable intChild is destroyed. This happens to all local variables when control passes out of the block where they are defined. When a variable the contents of the memory that it previously occupied is undefined by the language. In practice, the run time environment of the program may decide to reuse for some other purpose.

I think the text is trying to explain the difference between having a variable go out of scope, and having it destroyed. Inside the call to child(), the local variable intParent is out of scope - child() can't access it by name. But it does still exist. When control returns from child() back to parent, the value of intParent is still there and still accessible.

The great advantage of this scheme is that a function like parent() can call a function like child() and be confident that child() won't somehow change parent's local variables. This helps you keep your data straight.
dhayden explains it good.

Just to simplify things, every time you initialize a variable inside of a specific function, that variable effectively exists ONLY in that function. Let me show you incorrect code:

1
2
3
4
5
6
7
8
9
10
int coolfunction()
{
   int a = 10;
   return a;
}

int main()
{
    cout << a;
}

That is incorrect. int main doesn't know what a equals to because a exists ONLY in coolfunction.

When int main runs, the computer has no memory of what a is until you actually run coolfunction and it reads the code and says "Hey, a is equal to 10!" a literally does not exist until coolfunction is run.

Even if coolfunction does run, int main still doesn't know what a is equal to (because it's not in the scope of main), but what it DOES know is that coolfunction is giving it a number of 10 for some reason. int main also doesn't care what a is equal to, it's just concerned with the number that coolfunction gives it.

Now with that information, you can figure out which variables the computer has stored into memory for any program you run, and which ones it hasn't. Let's say you've declared 2 variables at the start of int main. There will then always be at least 2 variables stored in the computer's memory. Now in another function, you've declared 1 million other variables, but if your main never calls this function with a million variables, the computer will only have 2 variables in its memory. If you happen to call this function, your computer will store 1,000,002 variables in its memory (and this is key) only for as long as this function is running. As soon as the function stops running and returns back to main, it will delete those 1 million variables and go back to only storing 2.

If anything confused you, please let me know.

PS Anyone correct me if I said something wrong.
Okay, but what if i call child() several times in main, doesn't it get destroyed after the first time?
It does get destroyed, but every single time you call child(), it gets created again and remains in memory for as long as the computer is using child(). So if you call child() 10 times, intChild will be created 10 times and it will also be destroyed 10 times.
When it gets created second or third time, does it have the same memory location as the first time it was called?
When it gets created second or third time, does it have the same memory location as the first time it was called?

Maybe. Or maybe not. And even if it has the same location, you can't rely on the value being preserved across calls. The point is that the behavior is undefined. That means the compiler and runtime environment are free to do whatever they want. In particular, even if you test it out and find that the same address is used, you still can't rely on the behavior because another compiler, (or another version of the same compiler, or another set of compiler options) might do something totally different.
Is the reason for the error that, dLocalVariable's function is not called from main yet and then doesn't exist until then? I still don't get what all that memory thing is about.




The following code segment compiles without error but doesn’t work (don’t
you just hate that?)

The problem with this function is that dLocalVariable is defined only within
the scope of the function child(). Thus, by the time the memory address of
dLocalVariable is returned from child(), it refers to a variable that no longer
exists. The memory that dLocalVariable formerly occupied is probably being
used for something else.



1
2
3
4
5
6
7
8
9
10
11
double* child(void)
{
 double dLocalVariable;
 return &dLocalVariable;
}
void parent(void)
{
 double* pdLocal;
 pdLocal = child();
 *pdLocal = 1.0;
}
Last edited on
Topic archived. No new replies allowed.