C++ Review Flashcards

1
Q

Memory

A

C++ program has two key areas: Stack and Heap

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

Stack

A
  • Manages function calls and control flow, stores local variables, function parameters as local variables, and return addresses
  • Each function call creates a stack frame
  • Limited in Size
  • LIFO (Last-In-First-Out)
  • Automatically managed by system
  • Everything is destroyed when goes out of scope
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Heap

A
  • Enables dynamic allocation; a.k.a., allocating memory on heap during runtime
  • Larger size
  • new keyword to perform allocation
  • delete keyword to free memory/prevent leaks
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Pointers

A
  • Store memory addresses
  • Dereferencing to access data at the address
  • Ensure type safety
  • Pointer Arithmetic to navigate arrays
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Dynamic allocation

A
  • Allows memory allocation at runtime
  • Helpful for large or unknown data sizes
  • e.g. size_t array_size; std::cout << "array size: "; std::cin >> array_size; int* array_ptr = new int [array_size] {10}; delete[] array_ptr;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Memory Errors

A

Crucial to for avoid program crashes, data corruption and security vulnerabilities

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

Stack overflow

A

Exceeding stack size, often by recursion or large arrays

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

Index out of bounds

A

Accessing an area in memory outside an array’s scope

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

Memory leak

A

Deleting the reference to your data on the heap so the data is no longer accessible; leading to wasted memory resources and unexpected behavior

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

Dangling pointers

A

Pointer that points to an address that was previously de-allocated. To avoid this, always set the pointer to nullptr after freeing the memory using delete;
~~~
delete age_ptr;
age_ptr = nullptr;
~~~

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

Use After Free

A

Trying to access memory after it’s been de-allocated

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

Double Free

A

double deletion on the same pointer; aka trying to free memory twice

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

Uninitialized memory access / Segementation Fault

A
int * pointer;
std::cout << *pointer << std::endl;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Operator Overloading

A

Enabling user-defined objects to work with standard operators; e.g., ==, =, >, <, «,&raquo_space;

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

Constructors

A

Initialize objects. If overloaded, you can create objects under different scenarios based on the parameters

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

“Big 3”

A

For any class, you should have the Copy Constructor, Destructor, and Copy Assignment Operator

17
Q

Template Functions

A
  • Function blueprint with generic data types
  • Compiler generates the actual functions for needed data types based on how the functions are called at compile time!
  • Must specify data type in angle brackets
  • Following example works for chars, doubles, ints
  • `template <typename>
    T max(T a, T b)
    {
    return (a>b) ? a : b;
    }</typename>in main:int x = 5;
    int y = 2;max <int> (x,y);
    `</int>
18
Q

Stack Frame

A
  • Created and pushed onto stack during function call
    *
19
Q

Template Classes

A

template <typename></typename>