Presentation is loading. Please wait.

Presentation is loading. Please wait.

Class 2 CSI2172

Similar presentations


Presentation on theme: "Class 2 CSI2172"— Presentation transcript:

1 Class 2 CSI2172 http://www.site.uottawa.ca/~fbinard/Teaching/CSI2172_summer

2 Into C++: Data Structure: Class (start) Syntax: class { … }; class TwoTurtledoves{ public: method ( ){ method body } };

3 example 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).

4 example problem review Solving 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; };

5 example problem review Solving 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; };

6 example problem review Solving 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; };

7 example problem review Solving 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; };

8 pointeurs  A variable that contains as its value the address of another variable 0X0A2BB 0X0A2B9 0X0A2BA 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

9 0X0A2BB 0X0A2B9 0X0A2BA 0X0A2B8 0X0A2BC 0X0A2BD 0X0A2BE … 0X0A2BD 123 ptr a &a ptr &ptr *ptr a == 0x0A2BD == 0x0A2B8 == 123

10 Pointeurs 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 */

11 Allocating memory: Memory: 3 Static: Memory is allocated by the linker at the beginning of the program’s execution. The memory is deallocated at execution’s end. 3 Automatic: The memory is automatically allocated, managed and deallocated during the program’s execution. Function arguments and local variables get automatic memory. 3 Dynamic: Memory is requested explicitly by the programmer. The programmer manages and deallocates the memory.

12 Find the variable compile-time program-text i global variables i static variables automatic stack i local variables i function parameters i return values of functions run-time heap i malloc i new

13 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

14 Dynamic memory C Is harder. You have to be careful. This is where your main source of run time errors will be. C What does a run time error look like ? C The heap stores dynamically allocated memory blocks. C There are data structures that thrive on dynamic memory C what’s a tree ? C What’s a list ? C In C++ (unlike java), there is no garbage collector

15 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);

16 Request for memory allocation ( malloc ) † malloc requests the allocation of a memory block of size bytes on the heap. † Syntaxe : #include 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 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); }

17 The C++ equivalent is ( new ) † 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. }

18 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; }


Download ppt "Class 2 CSI2172"

Similar presentations


Ads by Google