Download presentation
Presentation is loading. Please wait.
1
CISC/CMPE320 - Prof. McLeod
Winter 2013 CISC/CMPE320 2/19/2019 CISC/CMPE320 Assignment 2 due today, 7pm. RAD marking is underway. You will get feedback in an Issue. Fall 2018 CISC/CMPE320 - Prof. McLeod Prof. Alan McLeod
2
CISC/CMPE320 - Prof. McLeod
Today Finish Repositories. Namespaces. Introduce the Heap. Fall 2018 CISC/CMPE320 - Prof. McLeod
3
Last Time: Git Repository Terms
Commit Update Version control Index or cache or “staging area” Working directory Local repository Clone Add Snapshot Push Fetch Merge Merge conflict Conflict resolution Fall 2018 CISC/CMPE320 - Prof. McLeod
4
CISC/CMPE320 - Prof. McLeod
Git Branching Features can be developed in an isolated way by creating a branch. Eventually it will merge back to the “master” branch. A branch is a subset of source code files that are needed just for one particular task. Fall 2018 CISC/CMPE320 - Prof. McLeod
5
Repositories in Use, Cont.
You need a server, we have Bitbucket. Or (if you were not in this class) you could use a cloud-based server. And local client software. Such as “GitKraken”, “SourceTree” or “SmartGit”, etc. Many others, or your IDE (like Eclipse or Visual Studio) will have a git client built right into it. Also popular: GitLab and GitHub. Fall 2018 CISC/CMPE320 - Prof. McLeod
6
From Before: Bitbucket
If you are using a git client the URL will be in the form: Where is your project key and “####” is your team name in lower case. For example, for team Aberdeen: You will still need to supply your username and password to access your repository. Fall 2018 CISC/CMPE320 - Prof. McLeod
7
CISC/CMPE320 - Prof. McLeod
Conflicts What happens when two people commit the same file? Fall 2018 CISC/CMPE320 - Prof. McLeod
8
CISC/CMPE320 - Prof. McLeod
Resolving Conflicts How to prevent? Option 1: only allow one person to have a writeable copy at any time (lock the file). This is called pessimistic concurrency or lock, modify, unlock. Used in Microsoft Visual SourceSafe. Option 2: let people edit, and resolve conflicts afterward by merging files. Called optimistic concurrency or copy, modify, merge. “It's easier to get forgiveness than permission”. Most modern systems (including Subversion) do this. Fall 2018 CISC/CMPE320 - Prof. McLeod
9
Resolving Conflicts, Cont.
Using Git “terminology”: Hermione attempts a push, but Ron has done a push after Hermione has fetched (or cloned). Hermione’s push will be rejected, even if she has not changed the same files that Ron has changed. Hermione must do a fetch and merge to get Ron’s changes before she can push. Ron’s changes will be applied and then Hermione’s changes will be re-applied. Fall 2018 CISC/CMPE320 - Prof. McLeod
10
Resolving Conflicts, Cont.
It may be that changes do not overlap, so a merge will work automatically, but if there is a conflict: The resolution process allows one person to view a merged file, with the differences marked. This person decides which revisions to keep/discard. Only works for text files, not binary files. If more than two people submit the same file – then the project needs better management… Fall 2018 CISC/CMPE320 - Prof. McLeod
11
Conflict Resolution, Cont.
Fall 2018 CISC/CMPE320 - Prof. McLeod
12
CISC/CMPE320 - Prof. McLeod
Repositories, Cont. Note that not only are file changes tracked and recorded but directory structure changes are also monitored. Always add a comment when you commit noting your changes in a general way and why you made those changes. Was this a response to a bug fix, for example? Did you run your unit tests on your code before you committed? Fall 2018 CISC/CMPE320 - Prof. McLeod
13
CISC/CMPE320 - Prof. McLeod
How to Use Git, Cont. Fall 2018 CISC/CMPE320 - Prof. McLeod
14
CISC/CMPE320 - Prof. McLeod
Fall 2018 CISC/CMPE320 - Prof. McLeod
15
CISC/CMPE320 - Prof. McLeod
Namespaces We’ve been using namespace std; for a while now. Without it, you would need to write: std::cout << something << std::endl; A simple mechanism to avoid naming conflicts. A class can have the same name as long as it is in a different namespace. Fall 2018 CISC/CMPE320 - Prof. McLeod
16
CISC/CMPE320 - Prof. McLeod
Namespaces, Cont. To place your class definitions in a namespace, simply name it: namespace alpha { class dingdong { … }; } Now dingdong can be defined in another namespace (like std) and there will not be a conflict. Fall 2018 CISC/CMPE320 - Prof. McLeod
17
CISC/CMPE320 - Prof. McLeod
Namespaces, Cont. Particularly useful when you are building a library of classes. You will want to make sure your namespace’s name is unique. You can use an alias to make this easier: namespace alpha = Alpha_Software_Company; Now the user of your library, just needs to type: alpha::someThing(); Fall 2018 CISC/CMPE320 - Prof. McLeod
18
CISC/CMPE320 - Prof. McLeod
Namespaces, Cont. The only namespace that you should identify with the using keyword is std. All other namespaces should be named in the statement, like alpha::. Then you won’t conflict with anything in the std namespace. Fall 2018 CISC/CMPE320 - Prof. McLeod
19
CISC/CMPE320 - Prof. McLeod
Nested Namespaces C++17 Now, you can declare a namespace inside another namespace: namespace A { namespace B { namespace C { //… } Or you could just do: namespace A::B::C { Fall 2018 CISC/CMPE320 - Prof. McLeod
20
CISC/CMPE320 - Prof. McLeod
Memory Categories Code: Machine instructions for all member and non-member functions. Code is read from memory and executed. Static Data: All global variables and static local variables and static class data members. Run-time Stack: Used by most variables. All local, non-static variables. Free Store or Heap: Used when variables are created using the new or new[] keywords. Fall 2018 CISC/CMPE320 - Prof. McLeod
21
Memory Categories, Cont.
Fall 2018 CISC/CMPE320 - Prof. McLeod
22
CISC/CMPE320 - Prof. McLeod
Stack vs. Heap Memory The stack is a fixed size amount of RAM consisting of sequential addresses. (1 to 100MB) Every time a function is invoked a frame is pushed on top of the stack. The frame contains: Function arguments. Local variables. The code memory location for the instruction that invoked the function. Another code instruction counter. A memory location waiting to accept other return values. Fall 2018 CISC/CMPE320 - Prof. McLeod
23
Aside – Changing Stack Size
Default is about 2MB. Lots – unless you create large arrays on the stack (why?) or you are doing a lot of recursion. To increase add the following to the “miscellaneous” linker command line options: -Wl,--stack,<size> <size> is in bytes. This assumes gcc/mingw on Windows. (I have not tried this…) Fall 2018 CISC/CMPE320 - Prof. McLeod
24
Aside – Changing Stack Size, Cont.
A larger stack is not always a good idea: You might have many threads running, each with their own large stack. A stack must be contiguous. A larger stack will take longer to demonstrate memory problems such as infinite recursion. Using a larger stack probably means you are not using the heap to your advantage. Fall 2018 CISC/CMPE320 - Prof. McLeod
25
Stack vs. Heap Memory, Cont.
When a function completes, the frame is popped off the stack (effectively, a stack pointer which contains the address of the top frame on the stack is moved to the frame below the frame that finished). The next frame, resulting from another function call, overwrites some or all of the memory from the old frame (and the stack pointer is moved up to the new frame). Fall 2018 CISC/CMPE320 - Prof. McLeod
26
Stack vs. Heap Memory, Cont.
This does not happen with heap memory. Objects in the heap persist and you must delete them! Heap memory is only limited by the amount of RAM that can be obtained by your OS. Addresses are not necessarily sequential. If you run out of RAM in your heap, then you may start using a swap file (yuk!). Stack overflow is common – when you run out of stack memory. But the heap is huge!! No overflow problems. But you might have a memory leak problem instead! Fall 2018 CISC/CMPE320 - Prof. McLeod
27
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
28
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! A local 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
29
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.