Week 2 notes
Popular in Advanced Programming
PHIL 1000 - 01
verified elite notetaker
Popular in Department
This 5 page Class Notes was uploaded by bloodiris0312 on Saturday October 8, 2016. The Class Notes belongs to PIC10C at University of California - Los Angeles taught by Ricardo Salazar in Fall 2016. Since its upload, it has received 3 views.
Reviews for Week 2 notes
Report this Material
What is Karma?
Karma is the currency of StudySoup.
You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!
Date Created: 10/08/16
PIC10C Lecture Notes 03.10 Week 2 Lecture Memory Management Stack like a stack of books. Stack on top and the book under it cannot be accessed. Disadvantage: tiny. Heap have to keep track of where things are placed. Advantage: huge. Disadvantage: other programs can mess with your data. creates memory in heap, pointer is in stack. releases memory, but does not affect the pointer. string and vector classes have new and delete built in to them. Every call to new needs a delete. Memory leaks happen when memory is not manually de-allocated after it is allocated. Memory leaks only happen with heap memory. Four types of memory Code each function definition has a set of instructions that does not change during program. Static data global or static variables. Values are modifiable but do not belong to any local scope. Exists until the end of program. Run-time stack memory that is stacked as the program runs. For example, if program calls f(), run-time stack stacks from bottom parameters, return address, and then local variables of f(). Stack overflow happens when stack runs out of memory; program force exits. 04.10 Section Combining inheritance, RAII, and input/output The modern machine has multiple cores, cores = CPUs. Hyper threading allows double the core amount of processing. Queue type more recommended (first one out, last one in) for a Jobmanager. We will use vectors, however, in case someone’s never learned queue before. First, the constructors and the assignment operator: Now the functions and the destructor. Variance of auto for when you want to interact with a copy of the object, for when object is used but not modified, and for when object will be modified. Abstract class We’ll make Job an abstract class so that it forces functionality onto its derived classes. This will allow Job* pointers, but not Job objects. Also known as an interface. Generally abstract classes don’t have variables, and definitions of functions are also rare. 05.10 Lecture Sometimes when you have multiple pointers pointing to the same object, you can accidentally delete the object twice (error). There's a new library in C++11 that addresses this. Use the corresponding release function towards the type (vector would be instead of ). Simple try‐catch Should be used whenever you try to allocate heap memory. Copies In this case, b is a deep copy since it makes a copy of a, while c is a shallow copy since it refers to a’s memory. 06.10 Section More idioms RAII is an implementation detail of any operator that copies objects, designed to protect us from leaked memory. Code factory is a general class that makes sure memory leaks don’t happen. Some tips No public in the inheritance line will default to private, so remember the public. In practice calling a function, like , twice may be dangerous since rounds of operations are wasted, so it’s better to create a variable and call that instead. NEVER. Write a sort algorithm outside an interview. Just use the default. Don’t do this. It’s bad style even if it’s legal. 07.10 Lecture Ternary operator returns a if the boolean a < b is true, b if false. Summary of memory management Identify errors these are generally uninitialized, shared, or dangling pointers. Memory leaks happen via reassignment pointers and polymorphism. Pointers in destructors need to point to , 0, or somewhere other than the object being deleted. Templates For a lot of code that differ only in type, a template would make it easier to use without copy-pasting everywhere.
Are you sure you want to buy this material for
You're already Subscribed!
Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'