CS3505 Lecture2
CS3505 Lecture2
Lecture 2:
Separation of Concerns
Scope,
Pointers, References
Course Announcements
• First assignment out today
– Due next Thursday
• Help hours start tomorrow – see page
under Course Resources
• See instructions on installing Docker and
VSCode
– This is a great activity for a lively discussion
on Piazza
– Doing the steps yourself will help you
understand how this is set up
Some Design Discussion
• I have declared a function
10 1004
int var = 5;
int count = 10;
• Additional variables
grow the stack
– an area of memory for
each process
– Usually a stack is
shown growing up
Function Calling
value address
• Function calls need to 5 1000
store
10 1004
– Where to return when
done with the function
– Parameters
– Local variables
• Added as a stack
frame
Function Calling
value address
int A(int param1) { 1000
1
int local1 = 5;
return local1; 0x1F0B 1004
} 1 1008
5 100C
int main() {
int mVar = 1;
mVar = A(mVar);
return 0;
}
Function Calling
value address
int A(int param1) { 1000
1
int local1 = 5;
return local1; 0x1F0B 1004
} 1 1008
5 100C
int main() {
int mVar = 1;
mVar = A(mVar);
return 0;
}
Leaving a Function
int A(int param1) { value address
int local1 = 5; 1 1000
return local1;
0x1F0B 1004
}
1 1008
• Value in local1 is copied to
a temporary return int 5 100C
– often the compiler gets rid of
this overhead
• Destructor is called on local
objects in frame
• Stack frame is ‘popped’
• Execution returns to calling
address
Function Calling
int A(int param1) { • Functions in C++ use
int local1 = 5; a pass-by-value
return local1; mechanism by default
} • Changes to value
stored in param1 in
int main() { A() are not
int mVar = 1; propagated back to
mVar = A(mVar); the argument mVar
return 0;
}
Break
Looking at Addresses
• Ampersand is overloaded as a unary operator
– the address-of operator
&localvar
• Returns the address of localvar
int mLocal = 1;
cout << "mLocal address is: " <<
&mLocal << endl;
Storing Addresses
• Store addresses in a pointer
type variable int var = 5;
• Type is base type with an * int* varA = &var;
– Like int* p
– Also see it as int *p
• A pointer holds a memory
address
– That is all you need to know to
be successful with pointers
• Sketch out compiler table and
memory diagrams
Sketch
int var = 5;
int* varA = &var;
One Use
• Pointers can be used to void A(int* param1)
make arguments {
mutable. …
– the pointer is constant, }
the thing it points to can
change
int mLocal = 1;
A(&mLocal);
Dereference a Pointer
• Can access the thing the pointer is pointing at
with *
– Dereference operator
int mLocal = 1;
A(&mLocal);
C++ References
References are a safer way to int& ref = val;
“point” at another object
– Use in parameter list
– Don’t change calling syntax int A(int& v) {
v++;
Reference type mechanism }
can depend on compiler
– Reference variable might only
exist during compilation int main() {
– Might be made as a pointer
int x = 4;
A(x);
}
Uses of & and *
Character\Use Operator Type Modifier
* dereference (*val) pointer (int*)
& address-of (&val) reference (int&)