-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path013_VarScopes_and_Namespace.cpp
77 lines (67 loc) · 2.78 KB
/
013_VarScopes_and_Namespace.cpp
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/// Description: namesapce, using, stackoverflow, variable storage in memory
/// Author: Pritesh Gohil - priteshbgohil [at] gmail [dot] com
#include<iostream>
using namespace std;
// Memory Segments
// https://ozh.github.io/ascii-tables/
// +--------+------------------------------------------+
// | FFFFh | |
// | | |
// | Static | Local variables |
// | | Parsed arguments to function |
// | | Recursive function call |
// +--------+------------------------------------------+
// | | |
// | Heap | |
// | | Dynamically allocated variables/memory |
// +--------+------------------------------------------+
// | | Initialized variable Local/Global |
// | | |
// | |------------------------------------------+
// | Data | |
// | | .bss (uninitialized variables) |
// | | global, static, extern variables |
// | | Note: not occupying but only in .obj file|
// +--------+------------------------------------------+
// | | |
// | | |
// | Text | compiled program |
// | | Machine code |
// | 0000h | Crt.o (startup routine |
// +--------+------------------------------------------+
namespace MyNamesapce {
int a, b;
long int lnNum = 10;
int sum(int num1, int num2){
return num1+num2;
}
}
// variable scopes
int x = 55; //global scope
void increment (int num){
cout << "Increment " << num << " by 1 = " << ++num << endl;
// increment(++num); // stack overflow, cz function pointers and local
// variables are stored in stack
}
int main(int argc, char const *argv[]) {
cout << "using only \"cout\" instead of \"std::cout\"" << '\n';
cout << "MyNamesapce::lnNum = " << MyNamesapce::lnNum << endl;
// access namespace items
using namespace MyNamesapce;
cout << "lnNum = " << MyNamesapce::lnNum << endl; //scope resolution operator not req.
cout << "sum of 5 + 7 = " << sum(5,7)<< '\n';
// namespace aliasing
namespace NewName = MyNamesapce;
cout << "sum of 5 + 7 = " << NewName::sum(5,7)<< '\n';
// Scope of variables
cout << "x = " << x << '\n';
int x = 5; // local scope
{
int x = 10; // inner scope
cout << "x = " << x << '\n';
}
cout << "x = " << x << '\n';
// stackoverflow error
increment(1);
return 0;
}