2. Namespace
// A program to demonstrate need of namespace
int main()
{
int value;
value = 0;
double value; // Error here
value = 0.0;
}
3. Namespace 2
// Here we can see that more than one
variables
// are being used without reporting any error.
// That is because they are declared in the
// different namespaces and scopes.
#include <iostream>
using namespace std;
// Variable created inside namespace
namespace first
{
int val = 500;
}
// Global variable
int val = 100;
int main()
{
// Local variable
int val = 200;
// These variables can be accessed from
// outside the namespace using the scope
// operator ::
cout << first::val << 'n';
return 0;
}
4. Namespace 3
// Creating namespaces
#include <iostream>
using namespace std;
namespace ns1
{
int value() { return 5; }
}
namespace ns2
{
const double x = 100;
double value() { return 2*x; }
}
int main()
{
// Access value function within ns1
cout << ns1::value() << 'n';
// Access value function within ns2
cout << ns2::value() << 'n';
// Access variable x directly
cout << ns2::x << 'n';
return 0;
}
5. For loops
• for (initialization; condition; update) { // body of-loop }
•initialization - initializes variables and is executed only once
•condition - if true, the body of for loop is executed
if false, the for loop is terminated
•update - updates the value of initialized variables and again checks the
condition
7. Range based for loops c++11
// the initializer may be a braced-init-list
for (int n : {0, 1, 2, 3, 4, 5})
std::cout << n << ' ';
std::cout << 'n';
// Iterating over array
int a[] = {0, 1, 2, 3, 4, 5};
for (int n : a)
std::cout << n << ' ';
std::cout << 'n';
// Just running a loop for every array
// element
for (int n : a)
std::cout << "In loop" << ' ';
12. • The Course consists of the followingtopics:
• Memory Layout
• Stack
• Call Stack
• Data Segment
• Heap
• Rodata segment
Outline
12
13. • The Course consists of the followingtopics:
• Memory Layout
• Stack
• Call Stack
• Data Segment
• Heap
• Rodata segment
Outline
13
14. Stack
• Stack contains local variables from functions and
related book-keeping data. LIFO structure.
▫ Function variables are pushed onto stack when
called.
▫ Functions variables are popped off stack when
return.
15. • The Course consists of the followingtopics:
• Memory Layout
• Stack
• Call Stack
• Data Segment
• Heap
• Rodata segment
Outline
15
16. • The Course consists of the followingtopics:
• Memory Layout
• Stack
• Call Stack
• Data Segment
• Heap
• Rodata segment
Outline
16
17. Call Stack
Example: DrawSquare called from main()
void DrawSquare(int i){
int start, end, …. //other local variables
DrawLine(start, end);
}
void DrawLine(int start, int end){
//local variables
...
}
18. Lower address
Call Stack
Example:
void DrawSquare(int i){
int start, end, …. //other local variables
DrawLine(start, end);
}
void DrawLine(int start, int end){
//local variables
...
}
Top of Stack
Higher address
19. int i (DrawSquare arg)
Higher address
Lower address
Call Stack
Example: DrawSquare is called in main
void DrawSquare(int i){
int start, end, ...
DrawLine(start, end);
}
void DrawLine(int start, int end){
//local variables
...
}
Top of Stack
20. main() book-keeping
int i (DrawSquare arg)
Higher address
Lower address
Call Stack
Example:
void DrawSquare(int i){
int start, end, ...
DrawLine(start, end);
}
void DrawLine(int start, int end){
//local variables
...
}
Top of Stack
21. local variables (start, end)
main() book-keeping
int i (DrawSquare arg)
Higher address
Lower address
//local variables
...
}
Call Stack
Example:
void DrawSquare(int i){
int start, end, ...
DrawLine(start, end);
}
void DrawLine(int start, int end)
{
DrawSquare
Stack Frame
Top of Stack
22. start, end (DrawLine args)
local variables (start, end)
main() book-keeping
int i (DrawSquare arg)
Higher address
Lower address
Call Stack
Example:
void DrawSquare(int i){
int start, end, ...
DrawLine(start, end);
}
void DrawLine(int start, int end)
{
DrawSquare
Stack
Frame
Top of Stack
//local variables
...
}
23. DrawSquare book-keeping
start, end (DrawLine args)
local variables (start, end)
main() book-keeping
int i (DrawSquare arg)
Higher address
Lower address
Call Stack
Example:
void DrawSquare(int i){
int start, end, ...
DrawLine(start, end);
}
void DrawLine(int start, int end)
{
DrawSquare
Top of Stack
Stack Frame
//local
variables
...
}
24. Lower address
Top of Stack
Call Stack
Example:
void DrawSquare(int i){
int start, end, ...
DrawLine(start, end);
}
DrawLine
Stack Frame
void DrawLine(int start, int end){
//local variables DrawSquare
Stack Frame
...
}
DrawLine local vars
DrawSquare book-keeping
start, end (DrawLine args)
local variables (start, end)
main() book-keeping
int i (DrawSquare arg)
Higher address
25. Lower address
Top of Stack
Call Stack
Example: DrawLine returns
void DrawSquare(int i){
int start, end, ...
DrawLine(start, end);
} DrawLine
Stack Frame
void DrawLine(int start, int end){
DrawSquare
Stack Frame
//local variables
...
}
DrawLine local vars
DrawSquare book-keeping
start, end (DrawLine args)
local variables (start, end)
main() book-keeping
int i (DrawSquare arg)
Higher address
26. local variables (start, end)
main() book-keeping
int i (DrawSquare arg)
Higher address
Lower address
Call Stack
Example: DrawLine returns
void DrawSquare(int i){
int start, end, ...
DrawLine(start, end);
}
void DrawLine(int start, int end)
{ DrawSquare
Stack Frame
//local variables
...
}
Top of Stack
27. local variables (start, end)
main() book-keeping
int i (DrawSquare arg)
Higher address
Lower address
Call Stack
Example: DrawSquare returns
void DrawSquare(int i){
int start, end, ...
DrawLine(start, end);
}
void DrawLine(int start, int end){
//local variables
... DrawSquare
Stack frame
}
Top of Stack
28. Lower address
Call Stack
Example: DrawSquare returns
void DrawSquare(int i){ int start, end,
...
DrawLine(start, end);
}
void DrawLine(int start, int end){
//local variables
...
}
Top of Stack
Higher address
29. Reference
• A reference variable is an alias, that is, another name for an
already existing variable. Once a reference is initialized with a
variable, either the variable name or the reference name may
be used to refer to the variable.
30. Reference Vs Pointers
• References are often confused with pointers but three major
differences between references and pointers are −
• You cannot have NULL references. You must always be able to
assume that a reference is connected to a legitimate piece of
storage.
• Once a reference is initialized to an object, it cannot be changed
to refer to another object. Pointers can be pointed to another
object at any time.
• A reference must be initialized when it is created. Pointers can
be initialized at any time.