Midterm Review Flashcards
What does the new operator do?
Allocates memory for object of type to right of new and assign value to pointer to left of the word new
Roadster = new int;
allocates 4 bytes from heap to store an integer at an address that gets stored in pointer Roadster
What is a static array? example
int stat[10]; static and knows at compile time that space for 10 integers will be allocated
What is a dynamic array? example
cin»arraysize;
int* a;
a=new int[arraysize];
delete [] a; -> at compile and program startup, the amount of space for the array is indeterminant; when the cin runs, the user sets the size; now set for lifetime of program
What is heap?
Heap is a system managed binary tree of available memory.
How does pointer relate to heap?
If you declare a pointer variable and then new it, sufficient memory space is given up from the heap for a variable of the type the pointer points to.
How does delete relate to heap and pointers?
When delete is executed, the memory used that is pointed to by the pointer in the delete is returned to the heap tree.
In what sense are linked lists more dynamic than dynamic arrays?
With linked lists (and trees) memory is allocated/deleted from/back to the heap as needed throughout the program’s life. With dynamic arrays, once the memory is first allocated its size is permanent throughout program life
What does *pointer mean?
- is de-reference; value at variable pointed to by pointer; cout«*pointer; prints out value at location pointed to by pointer.
What does & do with pointers?
p=&number1; sets pointer p to the address of the variable called number1
Explain when you use . and when you use :: with respect to member functions.
use . when invoking function in calling program i.e. classobj.function(); use :: only in implementation file when coding how the function runs;
What does :: have to do with “scope”?
the :: specifies the function name (identifier) will apply only in programs or functions that call it for an object of the class to the left of ::
How is a constructor named? How is it different from other methods? Explain what is meant with overloading constructor def.
constructor method has same name as the class itself. all other methods must have other names; overload constructors creates several versions for different numbers and types of input parameters.
what operations are more efficient with linked lists rather than arrays?
SORTING: to swap 2 records requires only changing a few pointers rather than moving all the data values of 3 records. adding and deleting records also require only pointer changes, not moving large sets of data.
Explain the difference between complexity O[N^2} and complexity O[2^n}. Examples and spaghetti
n^2 is quadratic: quadruples, bubble sort insertion sort; 2^n is exponential doubles in size, towers of hanoi; spaghetti is linear