Upload
christopher-heath
View
215
Download
3
Embed Size (px)
Citation preview
Class 2Class 2CSI2172CSI2172
http://www.site.uottawa.ca/~fbinard/Teaching/CSI2172_summerhttp://www.site.uottawa.ca/~fbinard/Teaching/CSI2172_summer
Into C++: Data Structure: Class (start)Into C++: Data Structure: Class (start)
Syntax:class <className>{
…};
class TwoTurtledoves{
public:<returntype> method (<args>){
method body}
};
example reviewexample review
Topcoder Problem (copyright, used in educational context) SRM224 Div 1
I give my true love presents every day. On day 1, I give her one Type 1 present. On day 2, I give her two Type 2 presents, followed by one Type 1 present. On day 3, I give her three Type 3 presents, followed by two Type 2 presents, followed by one Type 1 present.
This pattern continues day after day. Soon I begin to wonder, what type of present will be the 100th present that I give my true love? The 1000th? The 1000000th?
For example, the 10th present that I give my true love is the Type 1 present on day 3.
Write a method that takes an int n and returns the type of the nth present that I give my true love. Note that n is one-based, so n=1 refers to the very first present I give her (the Type 1 present on day 1).
example problem reviewexample problem reviewSolving by questions:
Q1: How many gifts do I give on day d ?
int Q1(int d){GiftNum = 0;for(int i=1; i<=d; i++ ){
GiftNum +=i;} return GiftNum;
};
example problem reviewexample problem reviewSolving by questions:
Q2: How many gifts will I have given by day d (d included)
int Q2(int d){int GiftNum = 0;for(int i=1; i<=d; i++ ){
GiftNum += Q1(i);} return GiftNum;
};
example problem reviewexample problem reviewSolving by questions:
Q3: On which day will I be giving the nth present to my true love ? (d)
int Q3(int n){int Day = 1;while(Q2(Day) < n) Day++;return Day;
};
example problem reviewexample problem reviewSolving by questions:
Q4: If I’m giving gift number g of day d, what is the type of gift number g ?
int Q4(int i, int day){int Type = day;int numGifts = 0;while( numGifts + Type < i){
numGifts += Type;Type--;}
return Type;};
pointeurspointeurs
A variable that contains as its value the address of
another variable
0X0A2BB
0X0A2B90X0A2BA
0X0A2B8
0X0A2BC
0X0A2BD
0X0A2BE…
int* ptr;
Not defined yet (garbage)
int* ptr;
int a;
int* ptr;
int a;
ptr = &a;
0X0A2BD
ptr is now initialized
int* ptr;
int a;
ptr = &a;
a = 123;
123
ptr
a
0X0A2BB
0X0A2B90X0A2BA
0X0A2B8
0X0A2BC
0X0A2BD
0X0A2BE…
0X0A2BD
123
ptr
a
&a
ptr
&ptr
*ptr
a
== 0x0A2BD
== 0x0A2BD
== 0x0A2B8
== 123
== 123
Pointeurs are also typesPointeurs are also types
• Pointeurs are also typed • There is a generic void*
int a; char c; int* pi; char* pc; void* gp;
pi = &a; /* OK */ pi = &c; /* TYPE MISMATCH */ pc = &c; /* OK */ pc = &a; /* TYPE MISMATCH */
gp = &a; /* OK */ gp = &c; /* OK */
Allocating memory:Allocating memory:
Memory:
Static:Memory is allocated by the linker at the beginning of the program’s execution. The memory is deallocated at execution’s end.
Automatic:The memory is automatically allocated, managed and deallocated during the program’s execution. Function arguments and local variables get automatic memory.
Dynamic:Memory is requested explicitly by the programmer. The programmer manages and deallocates the memory.
Find the variableFind the variable
compile-time program-text global variables static variables
automatic stack local variables function parameters return values of functions
run-time heap malloc new
Automatic and static memory are easy
int x; /* global */
int f(int n) {
int x; /* local to f */
if (n > 3) {
int x; /* local to if */
... }
{
/* a local scope *
"out of the blue" */ int x;
}
}
S T A T I C
Constants
Global Variables
Variables declared as: static
A U T O M A T I C
Local Variables
Function parameters
Function return
Dynamic memoryDynamic memory
Is harder. You have to be careful. This is where your main source of run time errors will be. What does a run time error look like ?
The heap stores dynamically allocated memory blocks.
There are data structures that thrive on dynamic memory what’s a tree ? What’s a list ?
In C++ (unlike java), there is no garbage collector
int * x = (int*)malloc(sizeof(int));
int * a = (int*)calloc(10,sizeof(int));
*x = 3;
a[2] = 5;
free(a);
a = 0;
free(x);
x = 0;
(C)
void* malloc(size_t size);
void* calloc(size_t n, size_t size);
void* realloc(void * ptr,size_t size);
void free(void * ptr);
Request for memory allocation Request for memory allocation ((mallocmalloc))
malloc requests the allocation of a memory block of size bytes on the heap.
Syntaxe : #include <stdlib.h> void *malloc(size_t size);
Returned Value : This is a function call. On success, it returns a pointer to the newly allocated memory. On failure (not enough memory or bad argument), malloc returns NULL.
Careful : dynamic memory functions return void* types. You have to cast the return value in order to use it.
#include <stdio.h>
#include <stdlib.h>
main() {
char *ptr;
struct s_fiche {
char nom[30];
int numero;
struct s_fiche *suiv; } *fiche;
ptr = (char *) malloc(80); // 80 bytes request
if ( ptr == NULL) {printf(“didn’t work\n"); exit(1);}
if (fiche = (struct s_fiche *) malloc(sizeof(struct s_fiche)) == NULL) {printf(" didn’t work\n "); exit(1);}
free(fiche); /* dealocate */
free(ptr);
}
The C++ equivalent is (The C++ equivalent is (newnew))
new is not a function call. It is an operator. There are subtle differences. new allocates memory for an object or array of objects and returns a suitably typed, nonzero pointer to the object.
int main(){
//Use new operator to allocate
// an array of 20 characters.
char *AnArray = new char[20];
for( int i = 0; i < 20; ++i ){
// On the first iteration of the loop, allocate
// another array of 20 characters.
if( i == 0 ){
char *AnotherArray = new char[20];
}
}
delete AnotherArray; // Error: pointer out of scope.
delete AnArray; // OK: pointer still in scope.
}
2 dimensions (matrix):2 dimensions (matrix):
double ** alloc_matrix(int n, int m) {
double ** M = new double* [n]; int i; for(i=0; i<n; ++i)
M[i] = new double [m]; return M;
}