SlideShare a Scribd company logo
Namespaces
Mohamed Saied
Namespace
// A program to demonstrate need of namespace
int main()
{
int value;
value = 0;
double value; // Error here
value = 0.0;
}
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;
}
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;
}
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
intro_to_cpp_namespace_robotics_corner.pdf
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" << ' ';
Memory Management
• The Course consists of the followingtopics:
• Memory Layout
• Stack
• Call Stack
• Data Segment
• Heap
• Rodata segment
Outline
9
• The Course consists of the followingtopics:
• Memory Layout
• Stack
• Call Stack
• Data Segment
• Heap
• Rodata segment
Outline
10
Memory Layout
Memory Layout diagram courtesy of bogotobogo.com
• The Course consists of the followingtopics:
• Memory Layout
• Stack
• Call Stack
• Data Segment
• Heap
• Rodata segment
Outline
12
• The Course consists of the followingtopics:
• Memory Layout
• Stack
• Call Stack
• Data Segment
• Heap
• Rodata segment
Outline
13
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.
• The Course consists of the followingtopics:
• Memory Layout
• Stack
• Call Stack
• Data Segment
• Heap
• Rodata segment
Outline
15
• The Course consists of the followingtopics:
• Memory Layout
• Stack
• Call Stack
• Data Segment
• Heap
• Rodata segment
Outline
16
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
...
}
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
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
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
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
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
...
}
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
...
}
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
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
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
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
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
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.
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.
Pointers
• A variable that holds the address of another variable

More Related Content

PPTX
Program activation records
PDF
09 implementing+subprograms
PDF
Buffer overflow attack
PPTX
How Functions Work
PDF
computer notes - Reference variables
PPTX
stack.pptx
PPTX
2.0 Stacks.pptx
PDF
220 runtime environments
Program activation records
09 implementing+subprograms
Buffer overflow attack
How Functions Work
computer notes - Reference variables
stack.pptx
2.0 Stacks.pptx
220 runtime environments

Similar to intro_to_cpp_namespace_robotics_corner.pdf (20)

PDF
(8) cpp stack automatic_memory_and_static_memory
PPTX
C++ Memory Management
PDF
e computer notes - Reference variables
DOCX
Memory management in c++
PDF
Function in C++
PPT
computer notes - Data Structures - 17
PDF
11 2. variable-scope rule,-storage_class
PPTX
#OOP_D_ITS - 2nd - C++ Getting Started
PDF
The Stack Frame
PDF
127 Ch 2: Stack overflows on Linux
PDF
CNIT 127: Ch 2: Stack overflows on Linux
PDF
C++_notes.pdf
PPT
General Talk on Pointers
PPTX
Learn c++ (functions) with nauman ur rehman
PPTX
Getting started cpp full
PPTX
Introduction to functions in C programming language
PPTX
C MEMORY MODEL​.pptx
PPTX
C MEMORY MODEL​.pptx
PPTX
ppl unit 3.pptx ppl unit 3 usefull can understood
(8) cpp stack automatic_memory_and_static_memory
C++ Memory Management
e computer notes - Reference variables
Memory management in c++
Function in C++
computer notes - Data Structures - 17
11 2. variable-scope rule,-storage_class
#OOP_D_ITS - 2nd - C++ Getting Started
The Stack Frame
127 Ch 2: Stack overflows on Linux
CNIT 127: Ch 2: Stack overflows on Linux
C++_notes.pdf
General Talk on Pointers
Learn c++ (functions) with nauman ur rehman
Getting started cpp full
Introduction to functions in C programming language
C MEMORY MODEL​.pptx
C MEMORY MODEL​.pptx
ppl unit 3.pptx ppl unit 3 usefull can understood
Ad

Recently uploaded (20)

PDF
System and Network Administraation Chapter 3
PPTX
Transform Your Business with a Software ERP System
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PPTX
CHAPTER 2 - PM Management and IT Context
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
top salesforce developer skills in 2025.pdf
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
Nekopoi APK 2025 free lastest update
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
medical staffing services at VALiNTRY
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
System and Network Administration Chapter 2
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
System and Network Administraation Chapter 3
Transform Your Business with a Software ERP System
2025 Textile ERP Trends: SAP, Odoo & Oracle
CHAPTER 2 - PM Management and IT Context
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
VVF-Customer-Presentation2025-Ver1.9.pptx
top salesforce developer skills in 2025.pdf
Design an Analysis of Algorithms II-SECS-1021-03
Odoo POS Development Services by CandidRoot Solutions
Nekopoi APK 2025 free lastest update
Internet Downloader Manager (IDM) Crack 6.42 Build 41
medical staffing services at VALiNTRY
How to Migrate SBCGlobal Email to Yahoo Easily
System and Network Administration Chapter 2
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
Adobe Illustrator 28.6 Crack My Vision of Vector Design
wealthsignaloriginal-com-DS-text-... (1).pdf
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Ad

intro_to_cpp_namespace_robotics_corner.pdf

  • 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" << ' ';
  • 9. • The Course consists of the followingtopics: • Memory Layout • Stack • Call Stack • Data Segment • Heap • Rodata segment Outline 9
  • 10. • The Course consists of the followingtopics: • Memory Layout • Stack • Call Stack • Data Segment • Heap • Rodata segment Outline 10
  • 11. Memory Layout Memory Layout diagram courtesy of bogotobogo.com
  • 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.
  • 31. Pointers • A variable that holds the address of another variable