CS Week 6- Pointer and Memory Management Flashcards

1
Q

new

A

allocates memory for the given type and returns a pointer to the allocated memory

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

look at notes and lab notes for the coding stuff

A

:)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

different ways to use new

A
  1. just one object
    int* point = new int;

2.one object with a specific value
int* point = new int(10);

  1. a class
    int* point = new Class;
  2. a class with its specific values
    int* point = new Class(77,88);
  3. an array of objects
    int* point = new int[10];
  4. an array of classes
    int* point = new Class[10];
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

member access operator

A

->
allows the pointer to access member function
new only one class
not an array of classes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

delete

A

deallocates (or frees) a block of memory that was allocated with the new operator
After delete, no attempt to dereference because the value does not exist anymore

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

when to use delete and delete[]

A

delete when there is only one thing/object allocated

delete[] when there is an array of objects

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

code

A

region where the program instructions are stored

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Static memory-

A

region where global variables (variables declared outside any function) as well as static local variables (variables declared inside functions starting with “static”) are allocated.
-Static variables are allocated once and stay in the same memory location for the duration of a program’s execution

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

the stack

A

region where a function’s local variables are allocated during a function call
-A function calls adds them (to the top of the stack) and a return removes them
-Think of the picture tyler drew with the stack and how function call are on top of main and return a value down and cannot be access again until another call
-Also called automatic memory - this memory is automatically allocated and unallocated

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

the heap

A

region where the “new” operator allocates memory and where “delete” operator deallocates memory
Also called free store

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

memory leak

A

occurs when a program that allocates memory loses the ability to access the allocated memory, typically due to failure to properly destroy/free dynamically allocated memory

free stuff that is no longer used with delete or delete[]
free stuff as private pointers (sub-object) with delete or delete[] in deconstructors

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

garbage collection

A

automatic process of fencing and freeing unreachable allocated memory locations

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

inline member function

A

a member function’s definition may appear within the class definition
Use to yield more compact code, keeping longer definitions outside the class

except with declaring variables before use

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Constructor initializer list

A

an alternative approach for initializing data member in constructor, coming after a colon and consisting of a comma-separated list of variableName(initValue) items
-Have them in the order they were declared in class definition
-Avoid creating and then modifying, all one step

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Standard template library (STL)

A

defines classes for common Abstract Data Types(ADT)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

vector

A

is an ADT of an ordered, indexable list of items
The vector ADT is implemented as a class

17
Q

at, size, empty, clear, insert, erase

A

.at(n) - returns the value at the index n

.size() - returns the size

.empty() - returns 1 or T if size ==0

.clear() - vector still exists but its values do not

.insert(name.begin() + n, m) - takes in position argument indicating where m should be inserted

.erase(name.begin() + n) - takes in a position argument where it should be deleted

18
Q

inserting elements in sorted order

A

Common use of insert() is to insert a new item in sorted order

  1. 1st number is added to the vector
  2. If the next number is greater than the 1st, it is added to the end of the vector
  3. If the next number is less than both the previous one, it is added to the front of the vector
  4. Find the 1st number it is less than
  5. And so on