Difference so str 0 would set str back to pointing

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ramatic use is for arrays. Suppose we want 20 bytes to store strings in. Suppose We can say We or char* str; char* str str = new char[20]; and str would now point at 20 bytes, accessible and str would in the usual way. in Object Programming W hat happens to the memory given to us by new new if we lose the pointer to it? It is still allocated to us, but we can't use it. This is a very bad thing. This very strcpy(str,"message"); str[0] = 'a'; jjust like a declared array – remembering the real ust remembering difference between a pointer and an array. difference So str = 0; would set str back to pointing nowhere. would str Object Programming Here's our month name function again: Here's char* MonthName(int m) char* MonthName(int { char name[12][4] ={"Jan","Feb","Mar","Apr", "May","Jun","Jul","Aug", "Sep","Oct","Nov","Dec"}; char* n; n = new char[4]; strcpy(n,name[m]); return n; This is called a memory leak. This memory } We'll see soon how to avoid this. Object Programming Object Programming Here's our month name function again: Here's char* MonthName(int m) char* MonthName(int { const char name[12][4] ={"Jan","Feb","Mar","Apr", "May","Jun","Jul","Aug", "Sep","Oct","Nov","Dec"}; char* n; n = new char[4]; strcpy(n,name[m]); return n; } Note the use of strcpy iinstead of just Note strcpy nstead n = name[m]; name[m Object Programming Now we can go back to our database problem. Now struct Video { char Title[MAXTITLE+1]; long BarCode; long BarCode short RunTime; short RunTime char Category[MAXCAT+1]; short NumCopies; short NumCopies }; Video* DBase; int NumRecords; Now we can go back to our database problem. Now struct Video { char Title[MAXTITLE+1]; long BarCode; long BarCode short RunTime; short RunTime char Category[MAXCAT+1]; short NumCopies; short NumCopies }; Video DBase[MAXVIDEO]; Video DBase[MAXVIDEO int NumRecords; Object Programming cout << "Size of existing database? "; cout cin >> NumRecords; >> NumRecords MAXVIDEO = NumRecords + 10; MAXVIDEO NumRecords // this allows for 10 additions DBase = new Video[MAXVIDEO]; DBase Video[MAXVIDEO if (DBase == 0) { cout << "No heap space for DBase\n"; return 1; } ... ... Note that MAXVIDEO iis no longer a const. Note MAXVIDEO s const Object Programming So, in summary, to get memory for an individual datatype value datatype DataType* var; var var = new DataType; new DataType (we can say DataType* var = new DataType;) var new DataType For an array, we specify the number of entries Object Programming One of the nice features of memory allocated by new iis that, once its usefulness is over, it can be new s returned to the pool of available memory, ready for other demands. for The operator delete returns the piece of The delete returns dynamic memory referred to by the value of pointer ptr by ptr delete ptr; delete...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online