CSCE 121 Exam 3 Review

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

What is RAII? What problem does it address?

("Resource Acquisition Is Initialization") a basic technique for resource management based on scopes.

What is a predicate?

A predicate is a function that returns true or false

How does a multimap differ from a map?

Allow the keys to not necessarily be unique

What operations does an iterator for a list provide?

Go to next element: ++ Get value: * Check if two iterators point to same element: ==

What is a function object?

a "function object," that is, an object that can behave like a function. overloads function call operator( )

What is the definition of a "memory leak"? What C++ programming methodology is highly recommended for avoiding memory leaks?

a failure in a program to release discarded memory, causing impaired performance or failure. RAII

What is a key feature of generic programming?

a style of programming focused on the design and efficient implementation of algorithms. A generic algorithm will work for all argument types that meet its requirements. In C++, generic programming typically uses templates.

What are some ideals for the way we store our data?

e want uniform access to the data independent of how it is stored independent of its type

How much space per element does a vector take up?

just data item

What are two benefits of the twos-complement representation of an integer over the onescomplement representation?

larger range of numbers arithmetic is easier - addition and subtraction can both be done

What is a link (in a linked list)?

link holds: a data item and a pointer to the next object

What is a resource leak?

losing access to a resource

Give one example of an associative container in the STL.

map (key value pair) set (key) multimap (key can occur multiple times) multiset (keyc can occur multiple times) + unordered versions of each

Why is it often infeasible to use the free store in an embedded system?

memory fragmentation and new may not take the same amount of time

What does sort(b,e) use as its sorting criterion?

numeric order or alphabetical (sorts using less than < )

How does an STL algorithm take a container as an input argument?

takes iterator to object....?

How does an STL algorithm take a container as an output argument?

takes iterator to object....?

What is the purpose of coding standards? List reasons for having them.

A coding standard is a set of rules for what code should look like, typically specifies naming and indentation rules specifies a subset of the language (e..g, don't use new or throw) specifies rules for commenting requires the use of certain libraries Organizations often try to manage complexity through coding standards

What is the potential problem with exceptions in the context of embedded systems?

adds space to code, can mess with the predictability of code time duration

In which ways does a function object differ from a function?

an object is passed instead of a function pointer

How do you know if a sequence is empty?

begin and end point to the same address

What kinds of iterators can you move to the previous element?

bidirectional iterator, random-access iterator

What is a linked list? How does it fundamentally differ from a vector?

collection of nodes where eac node holds data item and pointer to next object size is easily adjusted while program runs avoids over-allocating space although space is needed for next pointers inserting can be done in constant time once you know where you want to insert

How does a set differ from a map?

A set is a container that stores unique elements that are ordered. The value of an element also identifies it (i.e., the value is the key) Each value must be unique Values cannot be modified once in the container, but they can be inserted and removed Like maps, sets are typically implemented as binary search trees

Why would anyone use an unordered_map when an (ordered) map is available?

Advantage: search, insert and remove are faster Disadvantage: cannot access a sorted subrange of the elements

How does an unsigned int differ from a signed int?

Unsigned integers are trivial to represent in memory: bit0 means 1, bit1 means 2, bit2 means 4, and so on. However, signed integers pose a problem: how do we distinguish between positive and negative numbers? C++ gives the hardware designers some freedom of choice, but almost all implementations use the two's complement representation. The leftmost (most significant bit) is taken as the "sign bit":

What (roughly) does it mean for a tree to be balanced?

each sub-tree has approximately as many nodes as every other sub-tree that's equally far from the root

What are examples of useful STL algorithms?

find, find_if, count, count_if, sort, copy, unique_copy, merge, equal, equal_range, accumulate, inner_product

What are two pitfalls when using the built-in array language feature?

fixed size don't know their own size

How do you iterate over a container using the STL?

for( auto it = v.begin(); it != v.end(); ++it)

What is the difference between a protected class member and a private class member?

• Private: If a member is private, its name can be used only by members of the class in which it is declared. • Protected: If a member is protected, its name can be used only by members of the class in which it is declared and members of classes derived from that.

From a C++ language point of view, what are the three kinds of storage?

• Static memory: allocated by the linker and persisting as long as the program runs • Stack (automatic) memory: allocated when we call a function and freed when we return from the function • Dynamic (heap) memory: allocated by new and freed for possible reuse by delete

What is one advantage of an unordered map over a map, and vice versa?

• Use map if you need to look up based on a value (and if your key type has a reasonable and efficient less-than operation). • Use unordered_map if you need to do a lot of lookup in a large map and you don't need an ordered traversal (and if you can find a good hash function for your key type).

How do you move an iterator to the next element?

++

How do you move an iterator to the previous element?

--

When would you like to use the free store?

..You don't know how much memory you need at compile time. For instance, when reading a text file into a string, you usually don't know what size the file has, so you can't decide how much memory to allocate until you run the program. You want to allocate memory which will persist after leaving the current block. For instance, you may want to write a function string readfile(string path) that returns the contents of a file. In this case, even if the stack could hold the entire file contents, you could not return from a function and keep the allocated memory block..

List some properties of a good coding standard.

A good coding standard is prescriptive as well as restrictive give advice as well as rules A good coding standard gives rationales and examples for its rules A good coding standard is better than no standard

What is an STL iterator? What operations does it support?

An iterator is a type that supports the "iterator operations": Go to next element: ++ Get value: * Check if two iterators point to same element: ==

Why do we prefer higher levels of abstraction to low-level code?

As ever, our ideal is to work at the highest level of abstraction that is feasible given the constraints on our problem. Don't get reduced to writing glorified assembler code! As ever, represent your ideas as directly in code as you can (given all constraints). As ever, try hard to write the clearest, cleanest, most maintainable code.

What is the definition of the Kleene star operation on regular expressions?

Ax* matches an A followed by zero or more xs, such as Click here to view code image A Ax Axx Axxxxxxxxxxxxxxxxxxxxxxxxxxxxx

What basic operations can we do to a collection of data items?

Collect multiple data items into a container Organize the data according to some rule Retrieve data items by index: e.g., get the i-th item by value: e.g., get the first item with the value "Chocolate" by properties: e.g., get the first item with age < 64 Add new data items Remove existing data items Sorting and searching Simple numeric operations (e.g., add them all up)

Why can it be a poor idea to optimize a system for performance?

Don't optimize until you have to. Performance (in time or space) is often essential for an embedded system, but trying to squeeze performance out of every little piece of code is misguided. Also, for many embedded systems the key is to be correct and fast enough; beyond "fast enough" the system simply idles until another action is needed.

Define predictability in the context of embedded systems.

Each C++ operation executes in constant, measureable time e.g., every add operation takes the same amount of time, every virtual function call takes the same amount of time, etc. (on a specific machine)

What is memory fragmentation?

Fragmentation if you have a free chunk of size N and you allocate M of it, where M < N, then you have a fragment of size N-M after a while, such fragments constitute much of the memory

How can a coding standard do harm?

Inappropriate coding standards, causing extra work or prohibiting the best solution to some classes of problems, thus becoming a source of the kind of problems that the standards were introduced to solve

How much space per element does a map take up?

Maps are associative containers that store elements that are pairs first element of the pair is the key; value used to sort and uniquely identify the element second element of the pair is the value; holds the content associated with this key

Is list an associative container? Why not?

No

What is a pool?

Pre-allocate a fixed amount of global memory Divide it into fixed-size objects Can allocate and deallocate in any order (but chunks are always same size) No fragmentation Constant-time operations

What is a stack?

Pre-allocate a fixed amount of global memory Use "stack discipline" to access this memory grow (allocate) and shrink (deallocate) only at the top analogous to how the run-time stack for function calls operates No fragmentation Constant-time operations

How can we design a system to survive failure?

Replicate in emergency, use a spare Self-check/monitor know when the program (or hardware) is misbehaving Have a quick way out of misbehaving code make systems modular have some other part of the system responsible for serious errors ultimately perhaps a manual override

. Why is it useful to separate data from algorithms?

Separation of concerns Algorithms manipulate data, but don't know about containers Containers store data, but don't know about algorithms Algorithms and containers interact through iterators Each container has its own iterator types

In C++, each name (e.g., that of a variable) is declared in a scope and is valid (can be used) from its point of declaration until the end of the scope in which it was declared. What is the main purpose of restricting the validity of names in this way?

The main purpose of a scope is to keep names local, so that they won't interfere with names declared elsewhere.

What are two differences between a pointer and a reference?

Think of a reference as an automatically dereferenced pointer you do not use the * (dereference) operator to get to the value thus it is an alternative name for an object Differences from pointer: reference must be initialized value of a reference cannot be changed after initialization (reference always refers to the same object)

What is a "copy constructor" and why do we sometimes want to write our own for a class instead of relying on the default?

This constructor will be called when we try to initialize one object with another. If we want a deep copy (not just copying pointers but the value that they point to)

What are transient errors? Why do we particularly fear them?

Transient errors are usually the hardest to find. A transient error is one that happens "sometimes" but not every time a program is run. For example, we have heard of a processor that misbehaved only when the temperature exceeded 130°F (54°C). It was never supposed to get that hot; however, it did when the system was (unintentionally and occasionally) covered up on the factory floor, never in the lab while being tested.

What are virtual functions used for?

Virtual functions: the ability to define a function in a base class and have a function of the same name and type in a derived class called when a user calls the base class function.

What are the main considerations when deciding whether a function should be included as a member of a class definition or whether it should be a free-standing function?

When we design our interfaces to be minimal (though complete), we have to leave out lots of operations that are merely useful. A function that can be simply, elegantly, and efficiently implemented as a freestanding function (that is, as a nonmember function) should be implemented outside the class. That way, a bug in that function cannot directly corrupt the data in a class object. Note also that if the representation changes, only the functions that directly access the representation need to be rewritten.

What is a container?

container an object that holds elements (other objects).

What is the basic ordering property of binary tree?

each node contains one key (also known as data) the keys in the left subtree are less then the key in its parent node, in short L < P; the keys in the right subtree are greater the key in its parent node, in short P < R; duplicate keys are not allowed.

Why can't we have a universal coding standard?

every good coding standard is designed for a particular application area and for a particular set of programmers

What is one useful application of regular expressions?

he basic idea of a regular expression is that it defines a pattern that we can look for in a text.

What is an iterator category? What kinds of iterators does the STL offer?

input iterator, output iterator, forward itereator, bidrectional iterator, random-access iterator

What does insert() do? What does erase() do?

inserts node before (points new node to next node, changes node before to point to new node) erases node (point node before to node after, delete node)

Why can't we easily move objects from one place in memory to another?

must be aware of pointers pointing to the correct memory address when you move

Recall that one way to approximate dynamic memory allocation in embedded systems programming is to use either the "stack" approach or the "pool" approach. List one advantage of "stack" over "pool" and one advantage of "pool" over "stack".

pool - Can allocate and deallocate in any order stack - can have different sized objects

What operations are provided by a random-access iterator, but not a bidirectional iterator?

random-access can use subscript[ ] and can add or subtract iterators random-access is what vector uses

What is special about embedded systems? Give five concerns that are common.

reliability is critical resources are limited real time response is essential predictability is key system must function uninterrupted for years correctness is more important (check for hardware malfunction) hands-on maintenance is infeasible or very rare

How does an STL algorithm usually indicate "not found" or "failure"?

returning pointer to end

When would you prefer an unsigned int to a signed int?

saves space • Use signed integers (e.g., int) for numbers. • Use unsigned integers (e.g., unsigned int) for sets of bits

What is an STL sequence?

sequence: elements that can be visited in a linear order. From the STL point of view, a collection of data is a sequence. A sequence has a beginning and an end. We can traverse a sequence from its beginning to its end, optionally reading or writing the value of each element. We identify the beginning and the end of a sequence by a pair of iterators. An iterator is an object that identifies an element of a sequence.

Why doesn't the use of stacks and pools lead to memory fragmentation?

stack - always deallocates from the top pool - each object is the same size

What is the STL?

standard template library

Why use a copy() algorithm when we could "just write a simple loop"?

stl algorithms are fastest implementation

What is a memory leak? Why can it be a problem?

takes up memory you can never delete (can't access it)

What is the main C++ mechanism that supports generic programming?

template a template is a mechanism that allows a programmer to use types as parameters for a class or a function.

What should begin() and end() do for a container?

the beginning (points to first element, if any) the end (points to the one-beyond-the-last element)

Why is the use of dynamic memory allocation (e.g., new) often forbidden in embedded systems programming?

time for finding enough memory is unpredictable memory fragmentation can occur

What happens if you try to move an iterator past the end of a sequence?

undefined behavior.. accesses unknown memory location

Suppose 32 bits are used to store an int. How many different values can be stored in an int using the twos-complement representation? Using the signed magnitude representation?

unsigned [0, 2^n -1] = 2^n signed magnitue [-2^(n-1) - 1, 2^(n-1) - 1] = 2^n - 1 2's complement [-2^(n-1), 2^(n-1)-1] = 2^n

What is an associative container? Give at least three examples.

uses key map, set

What containers does the STL provide?

vector, list ,map ,unordered map ,set, unordered set, array

What are a few different ways of storing data?

vector, string, list, map, matrices

What is the potential problem with std::vector in the context of embedded systems?

vectors indirectly uses new C++ code refers directly to memory locations once allocated, an object typically cannot be moved Allocation delays the effort (time) to find a new free chunk of memory depends on what has already been allocated, and thus is unpredictable

What is a garbage collector (in the context of programming)?

writing program to squish all memory together (compact) and change pointers when moving

When would you use a list rather than a vector?

• Elem[]: Doesn't know its own size. Doesn't have begin(), end(), or any of the other useful container member functions. Can't be systematically range checked. Can be passed to functions written in C and C-style functions. The elements are allocated contiguously in memory. The size of the array is fixed at compile time. Comparison (== and !=) and output (<<) use the pointer to the first element of the array, not the elements. • vector<Elem>: Can do just about everything, including insert() and erase(). Provides subscripting. List operations, such as insert() and erase(), typically involve moving elements (that can be inefficient for large elements and large numbers of elements). Can be range checked. The elements are allocated contiguously in memory. A vector is expandable (e.g., use push_back()). Elements of a vector are stored (contiguously) in an array. Comparison operators (==, !=, <, <=, >, and >=) compare elements. • string: Provides all the common and useful operations plus specific text manipulation operations, such as concatenation (+ and +=). The elements are guaranteed to be contiguous in memory. A string is expandable. Comparison operators (==, !=, <, <=, >, and >=) compare elements. • list<Elem>: Provides all the common and usual operations, except subscripting. We can insert() and erase() without moving other elements. Needs two words extra (for link pointers) for each element. A list is expandable. Comparison operators (==, !=, <, <=, >, and >=) compare elements.

When would you use a string rather than a vector?

• Elem[]: Doesn't know its own size. Doesn't have begin(), end(), or any of the other useful container member functions. Can't be systematically range checked. Can be passed to functions written in C and C-style functions. The elements are allocated contiguously in memory. The size of the array is fixed at compile time. Comparison (== and !=) and output (<<) use the pointer to the first element of the array, not the elements. • vector<Elem>: Can do just about everything, including insert() and erase(). Provides subscripting. List operations, such as insert() and erase(), typically involve moving elements (that can be inefficient for large elements and large numbers of elements). Can be range checked. The elements are allocated contiguously in memory. A vector is expandable (e.g., use push_back()). Elements of a vector are stored (contiguously) in an array. Comparison operators (==, !=, <, <=, >, and >=) compare elements. • string: Provides all the common and useful operations plus specific text manipulation operations, such as concatenation (+ and +=). The elements are guaranteed to be contiguous in memory. A string is expandable. Comparison operators (==, !=, <, <=, >, and >=) compare elements. • list<Elem>: Provides all the common and usual operations, except subscripting. We can insert() and erase() without moving other elements. Needs two words extra (for link pointers) for each element. A list is expandable. Comparison operators (==, !=, <, <=, >, and >=) compare elements.


Set pelajaran terkait

BIBL 104-Quiz: The Old Testament Books of Prophecy

View Set

Prep U: Chapter 24: Assessing Musculoskeletal System

View Set

High-Risk Intrapartum (test #3) Nov 12

View Set

Government 2305 Constitution Scavenger Hunt

View Set

CH 14 Overview of Shock and Sepsis

View Set