Beruflich Dokumente
Kultur Dokumente
04/10/2016
Recap slide
Data types:
Primitive Data Types
- Bool, Int, float etc.
User-Defined Data Types (UDTs)
- Aggregate data types e.g., structures, array-of-integers etc.
Abstract Data Types (ADTs)
- Does not specify how the data type is implemented
- In an object-oriented language such as C++, an ADT and its
implementation together make up a class
M. Shahzad: Data Structures & Algorithms Lecture 2: Pointers and dynamic memory allocation 2
Todays lecture
Referencing vs pointers
Arrays & dynamic memory allocation
Stack vs heap
The new operator & memory leaks
Concept of shalow/deep copying
Void pointer
M. Shahzad: Data Structures & Algorithms Lecture 2: Pointers and dynamic memory allocation 3
Differences between references & pointers
cout << n << ' ' << *p << ' ' << r << endl;
*p = 9;?
Output: 5 5 5 r = 10;?
M. Shahzad: Data Structures & Algorithms Lecture 2: Pointers and dynamic memory allocation 4
Differences between references & pointers
M. Shahzad: Data Structures & Algorithms Lecture 2: Pointers and dynamic memory allocation 5
Constant pointer vs pointer constant
#include <iostream>
void main() {
const SIZE = 5
M. Shahzad: Data Structures & Algorithms Lecture 2: Pointers and dynamic memory allocation 7
Dynamic memory allocation
M. Shahzad: Data Structures & Algorithms Lecture 2: Pointers and dynamic memory allocation 8
Stack vs Heap
M. Shahzad: Data Structures & Algorithms Lecture 2: Pointers and dynamic memory allocation 9
Stack vs Heap
Stack:
Special region of computer's memory that stores temporary
variables created by each function, including the main()
Stack variables only exist in the block of code in which they
were declared (unless declared static)
Advantages:
Memory managed automatically
CPU organizes stack memory therefore reading from and
writing to stack variables is very fast and efficient
Disdvantages:
Size limits (depending on the OS)
Large chunks of memory, such as very large arrays, should
not be allocated on the stack to avoid overfilling the stack
memory (known as stack overflow)
M. Shahzad: Data Structures & Algorithms Lecture 2: Pointers and dynamic memory allocation 10
Stack vs Heap
Heap:
Region of your computer's memory that is NOT managed
automatically
Advantages:
More free-floating region of memory i.e., only limited by the
size of virtual memory (i.e., RAM and swap space)
May be accessed outside of the block in which it was
allocated
Disadvantage:
Needs to be explicitly freed by the program Memory leaks
M. Shahzad: Data Structures & Algorithms Lecture 2: Pointers and dynamic memory allocation 11
Heap vs Stack
Stack Heap
local variables are put on the stack global variables are on the heap
-unless they are also declared as
static local variables are on the heap
'static
(this is how they keep their value
function parameters are allocated between function calls)
on the stack
memory allocated by new, malloc
local variables that are declared on and calloc are on the heap
the stack are not automatically
The heap segment provides more
initialized by the system so they
stable storage of data for a program;
usually have garbage in them until
you set them memory allocated in the heap
remains in existence for the duration
variables on the stack disappear
of a program.
when the function exits (thus, if a
function is called multiple times, it's The memory allocated in the heap
local variables and parameters are area, if initialized to zero at program
recreated and destroyed each time start, remains zero until the program
the function is called and exited). makes use of it. Thus, the heap area
need not contain garbage.
M. Shahzad: Data Structures & Algorithms Lecture 2: Pointers and dynamic memory allocation 12
Dynamic memory allocation
M. Shahzad: Data Structures & Algorithms Lecture 2: Pointers and dynamic memory allocation 13
The new operator
int x = 10;
int *ptr;
M. Shahzad: Data Structures & Algorithms Lecture 2: Pointers and dynamic memory allocation 14
Memory leaks delete operator
M. Shahzad: Data Structures & Algorithms Lecture 2: Pointers and dynamic memory allocation 15
Case of 1D array
int *arr;
arr = new int[SIZE]; // allocation
M. Shahzad: Data Structures & Algorithms Lecture 2: Pointers and dynamic memory allocation 16
Case of 2D arrays
int **arr2D;
arr2D = new int* [numRows]; // allocation
delete [] arr2D;
(individual elements can be accessed using
indices!! (e.g., arr2D[0][2]=10;))
M. Shahzad: Data Structures & Algorithms Lecture 2: Pointers and dynamic memory allocation 17
Shallow / Deep Copy
struct Node{
char *name; Output:
int age; => Wendy 20 Wendy 30
Node(char *n = , int a=0) {
name = strdup(n); The reason for this is that the
age = a; definition of Node does not
} provide a copy constructor
};
Node(const Node&);
Node node1(Roger, 20);
Node node2(node1); // node2 = node1;
The intention of these declarations is to create object node1, assign values to
the two data members in node1, and then create object node2 and initialize
its data members to the same values as in node1
strcpy(node2.name, Wendy);
node2.age = 30;
cout<<node2.name<< <<node2.age<< <<node1.name<< <<node1.age;
M. Shahzad: Data Structures & Algorithms Lecture 2: Pointers and dynamic memory allocation
Shallow / Deep Copy
M. Shahzad: Data Structures & Algorithms Lecture 2: Pointers and dynamic memory allocation 19
Shallow / Deep Copy
Node node1(Roger, 20);
struct Node{ Node node2(node1); // or node2 = node1;
char *name;
int age;
Node(char * n=, int a=0) {
name = strdup(n);
strcpy(name, n);
age = a;
}
Node(const Node& n){ strcpy(node2.name, Wendy);
name = strdup(n.name); node2.age = 30;
age = n.age;
}
};
M. Shahzad: Data Structures & Algorithms Lecture 2: Pointers and dynamic memory allocation
Void pointer
M. Shahzad: Data Structures & Algorithms Lecture 2: Pointers and dynamic memory allocation 21
Next lecture topic:
Linked lists
M. Shahzad: Data Structures & Algorithms Lecture 2: Pointers and dynamic memory allocation 22