Download presentation
Presentation is loading. Please wait.
1
CISC/CMPE320 - Prof. McLeod
Winter 2013 CISC/CMPE320 2/22/2019 CISC/CMPE320 RAD marking is underway, still. You will get feedback in an Issue. Some information on the SDD on the next few slides. “Due” next Friday evening – Nov. 2 in Confluence. Fall 2018 CISC/CMPE320 - Prof. McLeod Prof. Alan McLeod
2
SDD – “System Design Document”
The RAD is usually a one-shot deal, viewed and approved of by the client. The SDD is for the development team only – it is not shown to the client. It is technical – more diagrams than words. It should be a dynamic document and is really just a framework that evolves or is filled in as the design of your project matures. Well suited to the Confluence style environment because it is dynamic and everyone can view and contribute. Fall 2018 CISC/CMPE320 - Prof. McLeod
3
CISC/CMPE320 - Prof. McLeod
SDD as a Deliverable See the description on the course web site. You do not have to use all categories on the next two slides, but need to have put what analysis results you have in the right places. The SDD will keep changing – even after the 2nd! Fall 2018 CISC/CMPE320 - Prof. McLeod
4
SDD - Traditional Contents
Create something like this table of contents as a framework to hold your design information: Introduction Purpose of system Design goals Definition, acronyms and abbreviations References Overview Current software architecture (not needed for us) Fall 2018 CISC/CMPE320 - Prof. McLeod
5
CISC/CMPE320 - Prof. McLeod
SDD Contents, Cont. Proposed software architecture Overview Subsystem decomposition Hardware/software mapping Persistent data management Access control and security Global flow control Boundary conditions Subsystem services Glossary Fall 2018 CISC/CMPE320 - Prof. McLeod
6
CISC/CMPE320 - Prof. McLeod
Software Analysis We will need to discuss what some of these terms mean. Software Analysis provides the fodder for the SDD. The SDD provides a framework, so you know where to store the results as they evolve. Important that everyone has access to, and an understanding of, the system-wide architecture. An important communication tool for the architect. Fall 2018 CISC/CMPE320 - Prof. McLeod
7
CISC/CMPE320 - Prof. McLeod
Software Analysis Some information comes straight from the RAD, but most comes from an Analysis of the RAD. To build your project you ultimately need to know what all of your classes are going to be: Their attributes and methods. Their public interfaces. How they interact along a time line. How they interact with other systems and users. The result of Analysis is expressed by a Model: Fall 2018 CISC/CMPE320 - Prof. McLeod
8
Overview as a UML Class Diagram
Use Case Diagram Class Diagram StateChart Diagram Sequence Diagram Functional Model Object Model Dynamic Model Analysis Model Fall 2018 CISC/CMPE320 - Prof. McLeod
9
CISC/CMPE320 - Prof. McLeod
Winter 2013 CISC/CMPE320 2/22/2019 SDD Contents, Cont. See if you can answer these questions in the SDD: Have you decided on the libraries you will use? Have you tested them for compatibility? Do you know how your system will work along a timeline? One actor or more? You will need to identify the classes you need along with their members. Eventually decide on which team members are coding which classes. Who is responsible for integration testing? Art? Research? Back story? Diagrams? Help system? Each coder is responsible for testing their own code. Fall 2018 CISC/CMPE320 - Prof. McLeod Prof. Alan McLeod
10
Jira Product Backlog and More Sprints
As your design becomes more concrete, you will be creating many more issues. Or, hopefully, you will have written user stories and will be breaking them up into many sub-tasks, each of which: Will be assigned to a team member. With a description of the task. Containing an initial time estimate and a priority. Eventually moved from the Backlog into a Sprint. Regularly updated by the member carrying out the task so that progress can be monitored once the sprint is underway. Eventually containing links to code in your repository. Fall 2018 CISC/CMPE320 - Prof. McLeod
11
CISC/CMPE320 - Prof. McLeod
Today Using the Heap Properly. Not Using the Heap Properly – Common Memory Errors. Fall 2018 CISC/CMPE320 - Prof. McLeod
12
CISC/CMPE320 - Prof. McLeod
Heap Memory You don’t always know until run-time how big an object will need to be. It might be huge! You may not be able to use (or may not want to use) a global, but need to have a variable that persists beyond a function’s completion. This memory is allocated when you use the new operator. The new operator provides dynamic memory allocation – a “memory allocator” process supplies the address of a heap storage location. You must use a pointer to refer to something on the heap. The pointer will be on the stack. Fall 2018 CISC/CMPE320 - Prof. McLeod
13
CISC/CMPE320 - Prof. McLeod
Returning a Pointer If you create a local object on the heap in a function then you can return a pointer to that object and it will persist. Much better than returning a reference to a local object created on the stack! A local stack object may be stable for a while, but eventually the memory it occupies will get totally or partially overwritten by another function call. Fall 2018 CISC/CMPE320 - Prof. McLeod
14
CISC/CMPE320 - Prof. McLeod
Heap Memory To return memory to the heap, you must use the delete operator. To free up an array, use delete[]. Supply a pointer to the operator. When you use new to create an array the size does not have to be known until run time: double* arr = new double[arraySize]; The pointer arr will reside on the run-time stack, but the array structure will be on the heap. Fall 2018 CISC/CMPE320 - Prof. McLeod
15
Notes on Using new and new[]
These operators always provide a pointer. The operators new or new[] could fail if the memory cannot be allocated by your OS. Normally you can check a pointer using something like this: if (arr) // pointer is not null else // pointer is null – was not assigned properly Fall 2018 CISC/CMPE320 - Prof. McLeod
16
Notes on Using new and new[], Cont.
Fortunately, new and new[] do not operate this way. If allocation fails, these operators will throw a std::bad_alloc exception instead of providing a null pointer. In this case the pointer arr would be undefined and most likely not nullptr. So, the conditional test of the pointer will always be true and it will not be able to check for successful allocation. If you are concerned, use new and new[] in a try/catch. Fall 2018 CISC/CMPE320 - Prof. McLeod
17
Notes on Using new[] and delete[]
A pointer of an array type must be deleted with delete[]. Unfortunately, the compiler cannot see any difference between a regular pointer and a pointer to an array. An array pointer is just a pointer to the first element in the array. You will not get a compiler error if you use delete instead of delete[] by mistake. Fall 2018 CISC/CMPE320 - Prof. McLeod
18
Notes on Using new[] and delete[], Cont.
Suppose you do something like: string** arr = new string*[100]; for(int i=0; i < 100; i++) arr[i] = new string(); Will this be enough to delete this structure completely?: delete[] arr; Fall 2018 CISC/CMPE320 - Prof. McLeod
19
Notes on Using new[] and delete[], Cont.
Similarly: vector<string*> *varr = new vector<string*>(); for (int i = 0; i < 100; i++) varr->push_back(new string()); Will this be enough to delete this structure completely?: delete varr; Fall 2018 CISC/CMPE320 - Prof. McLeod
20
Destructor Member Function
Distinguished from a constructor using the ~ before the class name. Responsible for deleting all heap variables (using delete and delete[]). If you use the heap in your class you must have a destructor. You never invoke the destructor directly – the system will invoke it instead. One of “The Big Three”: Fall 2018 CISC/CMPE320 - Prof. McLeod
21
“The Big Three” – Summary
Destructor: Frees all heap memory used by the object. Copy constructor: Initializes the object as a copy of the same type object supplied as a parameter. If you are using heap memory you will need to allocate and initialize each value. Assignment operator: Check to make sure that you are not assigning yourself. If so, do nothing. Free up the heap memory that is no longer needed. Copy the value of the argument supplied. Return *this. Fall 2018 CISC/CMPE320 - Prof. McLeod
22
CISC/CMPE320 - Prof. McLeod
Aside - “The Big Three” Any class that uses the heap must have these three functions. More on these guys later. We will build them in a demo program. For now, let’s summarize some common errors created by the improper use of memory in C++. Using the heap makes it much easier for the coder to create errors! Fall 2018 CISC/CMPE320 - Prof. McLeod
23
CISC/CMPE320 - Prof. McLeod
Common Memory Errors Using a variable that has not been initialized. Using a pointer to reference a memory location that is no longer valid. Forgetting to delete something on the heap. Deleting a memory value that was never allocated. Deleting something on the heap more than once. Look at each of these, in turn: Fall 2018 CISC/CMPE320 - Prof. McLeod
24
Initialization Memory Errors
We’ve seen this one before. A variable or pointer is created, but not initialized. Includes creating an array on the heap, but not initializing its elements. You don’t get an error, you just get some random value – whatever happens to be in that memory location. Fall 2018 CISC/CMPE320 - Prof. McLeod
25
Lifetime Memory Errors
When an activation record is popped off the run-time stack it takes all its local variables with it – they are no longer valid. If you have returned a pointer or a reference to a local variable, that location will only be valid until the next function invocation occurs. It is likely that this location will be overwritten. Eventually. Fall 2018 CISC/CMPE320 - Prof. McLeod
26
Lifetime Memory Errors, Cont.
For example: char* readALine() { char buffer[200]; cin >> buffer; return buffer; } char* p = readALine(); p is called a “dangling pointer” as it is pointing to memory that is no longer valid after readALine() is finished. Fall 2018 CISC/CMPE320 - Prof. McLeod
27
Aside – Buffer Overflow Error
Consider that readALine() function again: Suppose the user entered more than 200 characters: In this case, the extra characters would end up being written to memory beyond the dimensions of the array. This used to be a popular way for virus writers to inject their own function into an application. If you can overwrite the return address of the activation frame to the address of your function… Fall 2018 CISC/CMPE320 - Prof. McLeod
28
Lifetime Memory Errors, Cont.
You can cause similar behaviour if you attempt to return a reference to a local variable. For example: Fraction& operator+(const Fraction& left, const Fraction& right) { Fraction result(left.numer() * right.denominator() + right.numer() * left.denominator(), left.denominator() * right.denominator()); return result; } Get rid of this Fall 2018 CISC/CMPE320 - Prof. McLeod
29
CISC/CMPE320 - Prof. McLeod
Memory Leaks When you forget to delete something on the heap. More likely to cause a problem in a long-running program. Suppose you have successive re-assignments to the same pointer variable inside a loop. If you don’t carry out a delete in-between assignments each object remains on the heap. Eventually the heap manager will be unable to service any new allocation requests. Fall 2018 CISC/CMPE320 - Prof. McLeod
30
Invalid Memory References
What happens when you are too eager to delete a value from the heap. If you have more than one pointer aliased to that value, then the second pointer becomes invalid. For example, deleting the nodes of a linked list in a for loop: for (Node* p = ptr; p != NULL; p = p->next) delete p; } p->next is no longer accessible after deletion. Fall 2018 CISC/CMPE320 - Prof. McLeod
31
CISC/CMPE320 - Prof. McLeod
Aside – Using nullptr You can assign a pointer to be nullptr. (It does not happen by default as in Java!). Deleting a null pointer has no effect. So, consider setting a pointer to nullptr after you have deleted it: This will prevent you trying to invoke any functions from the pointer (you will get a null pointer error). And if you do try to delete the pointer again, nothing bad will happen. Fall 2018 CISC/CMPE320 - Prof. McLeod
32
Deleting an Un-Initialized Pointer
Fraction* frac; delete frac; Bad news. You are trying to delete garbage. Better to set to nullptr if you cannot initialize right away: Fraction* frac = nullptr: Fall 2018 CISC/CMPE320 - Prof. McLeod
33
CISC/CMPE320 - Prof. McLeod
Object Slicing Another memory problem that is particular to C++. For example: Child* c = new Child(arguments…); Parent* p = c; // OK Parent pp = *c; // NOT OK This is legal, compiles and runs fine, but what is the problem with the last line? Fall 2018 CISC/CMPE320 - Prof. McLeod
34
CISC/CMPE320 - Prof. McLeod
Object Slicing, Cont. The portion of the Child object that will not fit into an object of type Parent is “sliced” away – it is lost, when the pointer, c, is de-referenced. This is one of the reasons why it is best to access objects through pointers or references only. Fall 2018 CISC/CMPE320 - Prof. McLeod
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.