CSC 230 - Lecture 03-Memory
CSC 230 - Lecture 03-Memory
CSC230
Memory Management
Von Neumann Architecture
2
Memory
Variables and static allocation
5
a = area(wid,len);
}
int area(int w, int l)
{
int ans = w * l;
print(ans);
return ans;
}
void print(int
area)
{
cout << “Area is
“ << area;
Local variables
7
a = area(wid,len);
}
int area(int w, int l)
{
int ans = w * l;
print(ans);
return ans;
}
0xbf0 wid
0xbf4 void print(int
main len
area)
0xbf8 a {
00400120 Return
cout << “Area is
0xbfc link “ << area;
Local variables
8
a = area(wid,len);
}
int area(int w, int l)
{
int ans = w * l;
print(ans);
return ans;
}
0xbf0 8 wid
0xbf4 5 void print(int
main len
area)
0xbf8 a {
00400120 Return
cout << “Area is
0xbfc link “ << area;
Local variables
9
a = area(wid,len);
}
0xbe0 ans
8 int area(int w, int l)
area 0xbe4 w {
5 int ans = w * l;
0xbe8 l print(ans);
004000ca0 return ans;
0xbec Return
link
}
0xbf0 8
wid
0xbf4 5 void print(int
main len
area)
0xbf8 a {
00400120 Return
cout << “Area is
0xbfc link “ << area;
Local variables
10
a = area(wid,len);
40 }
0xbe0 ans
8 int area(int w, int l)
area 0xbe4 w {
5 int ans = w * l;
0xbe8 l print(ans);
004000ca0 return ans;
0xbec Return
link
}
0xbf0 8
wid
0xbf4 5 void print(int
main len
area)
0xbf8 a {
00400120 Return
cout << “Area is
0xbfc link “ << area;
Local variables
14
a = area(wid,len);
}
int area(int w, int l)
{
int ans = w * l;
print(ans);
return ans;
}
0xbf0 8
wid
0xbf4 5 void print(int
main len
area)
40
0xbf8 a {
00400120 Return
cout << “Area is
0xbfc link “ << area;
Scope example
15
Addres
#include <iostream> s
using namespace std; 0 Code
…
int x = 5;
Global
x=5
int main() {
…
int m, n = 3, x = 2;
Pointer
• Memory address of a variable
• &obj returns the address of obj
• *ptr returns the object at address given by ptr
• *(&obj) returns obj
NULL
• Pointer value points nowhere
• Is 0. So, we can have
• int * p = NULL;
• if(p)
• Defined in <cstdlib>
nullptr
• The NULL pointer defined in C++11
• To use nullptr compatible with C++11
• g++ -std=c++11 -g -o helloPilot helloPilot.cpp
Pointer & Reference
17
Reference
• Is an alias to an existing variable
• Must be initialized when it is declared
• Logically, reference does not consume memory, it is just another name (alias ) of
some variable.
• Physically, it may be implemented by pointer.
Use pointers correctly
18 14
One function can use pointer to modify the int area(int, int, int*);
variable in a different function.
int main()
{
int wid = 5, len = 4, a;
area(wid,len,&a);
Stack Area of }
RAM
void area(int w, int l,
5 int* p)
0xbe0 w {
0xbe4 4 *p = w * l;
area l }
0xbe8 0xbf8
p
0xbec 004000ca0 Return
link
0xbf0 5 wid
main 0xbf4 4 len
20
0xbf8 a
Return
0xbfc 00400120 link
Misuse of pointers
19
5
0xbf0 wid
main 0xbf4 4 len
0xbf8 a
0xbfc 00400120 Return
link
Use Reference, as a variable
20
int x = 10; x: x == r
10
int &r = x; r: &x == &r
Use Reference, as a parameter
21
5
0xbf0 wid
main 0xbf4 4 len
20
0xbf8 a
0xbfc 00400120 Return
link
Parameters
22
int a=10; …
0x101e300e0 Global
void foo(int x, int y){ a = 10
cout << x << "\t" << y << endl;
} …
#include <iostream>
using namespace std;
int a=10;
#include <iostream>
using namespace std;
int a=10;
int main(){
int c = 10, d =20;
foo(c, d);
bar(&c, a);
}
Parameters
25
#include <iostream>
using namespace std;
int a=10;
int main(){
int c = 10, d =20;
foo(c, d);
bar(&c, a);
}
When to use pass by value, pass by pointers, pass by
reference?
26
Pass by value:
• Do not want to modify the parameter.
• It is easy to copy (int, double, std::string, std::vector, etc.)
Pass by pointer:
• expensive to copy the data
• NULL can be the address value
Pass by reference:
• expensive to copy the data
• NULL cannot be the address value
Memory
Static vs. dynamic
28
Deallocated (die) when they go out of Valid until explicitly deallocated by the
the scope program (via ‘delete’)
In general, the size is constant and must Size can be determined at run-time
be known at compile time
int count;
int student[24]; cin >> count;
int *student = new int[count];
Dynamic memory in C
29
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *ptr;
*ptr = 25;
Both gcc and g++
free(ptr);
return 0; can compile this
} code
Dynamic memory in C++
30
new
• Allocates memory from heap
• Returns a pointer to the memory
• double *ptr = new double;
• int *array = new int[24];
delete
• Returns the memory to heap
• followed by the pointer to the data that you want to deallocate
• delete ptr;
• delete [] ptr; // ptr is a pointer to an array
Dynamic memory allocation
31
0 Code
…
#include <iostream> Globals
using namespace std; …
Heap new
allocates:
int main(int argc, char *argv[]){ 20bc0 00 salary
int count; 20bc4 00 [0]
cin >> count; 20bc8 00 salary[1]
int *salary=new int[count]; 20bcc 00 salary[2]
20bd0 00
delete [] salary; salary[3]
… salary[4]
return 0;
}
stack
fffffffc
Mem
ory
What to fill?
32
Dynamic Allocation
int area(int, int);
• Stored in heap
• Pointer accesses it int main()
• Exists until user ‘delete’ it {
int wid = 5, len = 4, a;
• If it is not deleted, the data exists in heap even a = area(wid,len);
pointer dies }
0xbf0 5
wid
0xbf4 4
main len
0xbf8 a
00400120
0xbfc Return
link
Dynamic allocation
34
Dynamic Allocation
int area(int, int);
• Stored in heap
• Pointer accesses it int main()
• Exists until user ‘delete’ it {
int wid = 5, len = 4, a;
• If it is not deleted, the data exists in heap even a = area(wid,len);
pointer dies }
0xbf0 5
wid
0xbf4 4 Memory Leak
main len
0xbf8 a No pointer to it.
0xbfc 00400120 Return Memory space is
link
wasted.
Dynamic allocation
35
Dynamic Allocation
int area(int, int);
• Stored in heap
• Pointer accesses it int main()
• Exists until user ‘delete’ it {
• If it is not deleted, the data exists in heap even pointer int wid = 5, len =
a = area(wid,len);
4, a;
dies }
0xbf0 5
wid
0xbf4 4
main len
0xbf8 a
00400120
0xbfc Return
link
Dynamic allocation
36
Dynamic Allocation
int area(int, int);
• Stored in heap
• Pointer accesses it int main()
• Exists until user ‘delete’ it {
• If it is not deleted, the data exists in heap even pointer int wid = 5, len =
a = area(wid,len);
4, a;
dies }
0xbf0 5
wid
0xbf4 4 Memory Leak
main len
0xbf8 a No pointer to it.
0xbfc 00400120 Return Memory space is
link
wasted.
Object assignment
37
Assigning one struct or class object to another will cause an element by element copy
of the source data.
#include<iostream>
using namespace std;
enum {CS, MATH, BIO};
struct student { s1 s2
char name[80];
int id; Bill Bill
int major; 5 5
};
int main(int argc, char *argv[]) CS CS
{
student s1;
strncpy(s1.name,”Bill”,80);
s1.id = 5;
s1.major = CS;
student s2 = s1;
return 0;
}
Objects in C++
Objects can be created as local variables just like any basic data types in C++.
C++:
ComplexType num1;
Basic data types and classes are treated the same way in C++, unlike Java.
int numbers[];
numbers = new int[5];