PROGRAMMING 1 CHECKPOINT ISSUES ACKNOWLEDGEMENT THE SLIDES ARE

  • Slides: 8
Download presentation
PROGRAMMING 1 – CHECKPOINT ISSUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY

PROGRAMMING 1 – CHECKPOINT ISSUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1

DELETE ON ARRAY VS. DELETE OBJECT Deleting an array is not similar to deleting

DELETE ON ARRAY VS. DELETE OBJECT Deleting an array is not similar to deleting a single element/object. Ex. T* t = new T[cap]; delete t; X Wrong Syntax to delete Use the right delete syntax. Ex. delete [] t; //deletes an array Ex. delete n; //deletes an object pointer array 2

MEMORY CLEANUP BEFORE NEW ALLOCATION Not deleting the previous memory, while allocating new memory

MEMORY CLEANUP BEFORE NEW ALLOCATION Not deleting the previous memory, while allocating new memory for the same object will result in memory leaks. Free the memory before deleting the reference to it. Ex. T* t = new T[100]; Ex: T * t = new T[100]; … …. delete []t ; t = new T[cap]; X Wrong 3

EMPTY VECTOR CHECK Not checking whether the array is empty, before reducing the size

EMPTY VECTOR CHECK Not checking whether the array is empty, before reducing the size or deleting elements from it. Ex: in- pop_back{ sz--; X Not sufficient } Always have an empty check before deleting or reducing the size. Ex: pop_back{ if(!empty()) sz--; } 4

SETTING CAP VARIABLE IN RESERVE When allocating a new array of different Not changing

SETTING CAP VARIABLE IN RESERVE When allocating a new array of different Not changing the capacity to new capacity in reserve function. capacity, always change the value of ’cap’ variable. Ex: cap = c; 5

CODE DUPLICATION Try to reuse functions anywhere possible. Replication of code in resize, push_back,

CODE DUPLICATION Try to reuse functions anywhere possible. Replication of code in resize, push_back, pop_back. For ex: in push_back, allocate memory using reserve function instead of copying the code from reserve. 6

ISSUE SUMMARY # ERROR/ISSUES RIGHT WAY 1) Deleting an array is not similar to

ISSUE SUMMARY # ERROR/ISSUES RIGHT WAY 1) Deleting an array is not similar to deleting a single element/object. For ex. T* t = new T[cap]; delete t; X Wrong Use the right Delete operator. Ex. delete [] t; 2) Not deleting the previous memory, while allocating new memory for the same object will result in memory leaks. Ex: T * t = new T[100]; t = new T[cap]; X Wrong Free the memory before deleting the reference to it. Ex. T* t = new T[100]; delete []t ; t = new T[cap]; 3) Not checking whether the array is empty, before reducing the size or deleting elements from it. Ex: in- pop_back{ sz--; X Wrong } 3) Always have an empty check before deleting or reducing the size. Ex: pop_back{ if(!empty()) sz--; } 4) Not changing the capacity to new capacity in reserve function. When allocating a new array of different capacity, always change the value of ’cap’ variable. For Ex: cap = c; 5) Replication of code in resize, push_back, pop_back. Try to reuse functions anywhere possible. For ex: in push_back, allocate memory using reserve function instead of copying the code from reserve.

THINGS TO DO Read the comments in the code Sketch out the algorithm by

THINGS TO DO Read the comments in the code Sketch out the algorithm by writing comments Then code! Use web search for syntax help 8