DoomMaster, I don't know what you mean by mainline. Explain?
DoomMaster, I don't know what you mean by mainline. Explain?
[email protected]
Microsoft Visual Studio .NET 2003 Enterprise Architect
Windows XP Pro
Code Tags
Programming FAQ
Tutorials
55 replies already? And you are getting any information out of all this nonsense Now you probably got bubba started with assembly stuff
Hehe. Welcome to the fray Jawib. Feel free to jump in.
so is "stack" the L1 or L2 cache on the processor? lol...Originally posted by Lurker
The stack is (hmm how to explain ) basically a list that can have items dynamically added / removed from it. The local variables are placed on the stack until the function returns, where the locals are removed from the stack.
and could someone explain the difference between static and dynamic in terms of storing information? I thought I knew, but apparently I don't.
--MrDoomMaster
The kind of DooM that makes the MooD
If you mean inside the main() function, then no...It all has to do with scoping. Just because it hasn't been destroyed doesn't mean you can treat it as global. You have to pass values to the function so the function knows what the variable's value was, and if you need to change the variable...well...thats where pointers come inBut of course, declaring a variable in the mainline would make it global also, right?
"Think not but that I know these things; or think
I know them not: not therefore am I short
Of knowing what I ought."
-John Milton, Paradise Regained (1671)
"Work hard and it might happen."
-XSquared
I believe he means int main().Originally posted by bennyandthejets
DoomMaster, I don't know what you mean by mainline. Explain?
If so, then no, variables created in main are also, in essence, local. They cannot be used in other funtions. Global variables would be the bold ones below:
EDIT: ARGH stop posting all at once everyone .Code:#include <iostream> using namespace std; int GLOBAL = 49; int main() { cout << "Global variable GLOBAL = " << GLOBAL << endl; return 0; }
Last edited by Lurker; 11-01-2003 at 05:53 PM.
Do not make direct eye contact with me.
Mainline is, the main function that contains all other functions... I guess in C++, it would be outside of everything... for example:Originally posted by bennyandthejets
DoomMaster, I don't know what you mean by mainline. Explain?
Code://headers go here int globalvariable; //this would initialize a global variable, because it has initialized it outside of all functions, so therefore any function can refer to it int main() { //code } int function1() { //more code }
kinda get what I'm saying now? I'm not sure if I'm correct or not, but I think I'm close at least!
--MrDoomMaster
The kind of DooM that makes the MooD
Your code is correct, but the term mainline is not proper terminology. I would say global instead.
[email protected]
Microsoft Visual Studio .NET 2003 Enterprise Architect
Windows XP Pro
Code Tags
Programming FAQ
Tutorials
Yes, by your terminology, variables in the mainline (usually called global) can be used by all the functions.
Do not make direct eye contact with me.
Think we need to explain variable scope or variable lifetime.
Here x has file scope. Every function can access x. It is global to the file. However, other file's cannot access x. It is only known in this file.Code:int x; int main(void) { return(0); } void FooBar(void) { }
This x has function scope. It is only known inside of FooBar. When you get back to main() x does not exist and cannot be accessed.Code:int main(void) { FooBar(); return(0); } void FooBar(void) { int x; }
This x has block scope. It is only known inside of the {} block.Code:int main(void) { if (condition) { int x=0; } return(0); }
well actually mainline is the global area. That's what I've been taught anyway. Mainline is usually the main function or module in other programming languages, but in C++ I guess it would be the area that isn't inside of ANY function, usually at the top or bottom. This is what I believe, but sorry. I should use words everyone else understandsOriginally posted by bennyandthejets
Your code is correct, but the term mainline is not proper terminology. I would say global instead.
--MrDoomMaster
The kind of DooM that makes the MooD
What about 'extern' Bubba . I think we should ignore extern variables for now.However, other file's cannot access x. It is only known in this file.
[email protected]
Microsoft Visual Studio .NET 2003 Enterprise Architect
Windows XP Pro
Code Tags
Programming FAQ
Tutorials
Don't confuse him! He's starting to understand .
Do not make direct eye contact with me.
Code:What about 'extern' Bubba . I think we should ignore extern variables for now.
Yeah I thought about that as soon as I wrote it...but yeah think we'll leave the extern discussion out right now. LOL
Hmm... you're using things in your code I don't quite seem familiar with. what exactly is "(void)"?Originally posted by Bubba
Think we need to explain variable scope or variable lifetime.
Here x has file scope. Every function can access x. It is global to the file. However, other file's cannot access x. It is only known in this file.Code:int x; int main(void) { return(0); } void FooBar(void) { }
This x has function scope. It is only known inside of FooBar. When you get back to main() x does not exist and cannot be accessed.Code:int main(void) { FooBar(); return(0); } void FooBar(void) { int x; }
This x has block scope. It is only known inside of the {} block.Code:int main(void) { if (condition) { int x=0; } return(0); }
--MrDoomMaster
The kind of DooM that makes the MooD
Maybe some code will clear things up:
Code:#include <iostream> using namespace std; int Global=0; int Func1() { int a=1; int b=2; return a+b; } //a,b now gone int Func2() { return Global++; //can be changed and accessed since it is global } int Func3(int val) { Global=val; return Global; } int Func4(int& val) { val=10; return val; } int main() { int MainVar=25; int MainVar2=20; cout<<Func1()<<endl;//outputs 3 cout<<Func2()<<endl;//outputs 0, Global now 1 cout<<Global<<endl;//outputs 1; cout<<Func3(MainVar)<<endl;//outputs 25, Global now 25 cout<<Global<<endl;//outputs 25 cout<<Func4(MainVar)<<endl;//outputs 10, MainVar now 10 cout<<MainVar<<endl;//outputs 10 //note MainVar could not be changed without passing it as //a reference, since its scope is within main() }
"Think not but that I know these things; or think
I know them not: not therefore am I short
Of knowing what I ought."
-John Milton, Paradise Regained (1671)
"Work hard and it might happen."
-XSquared