Vectors and Memory Flashcards

1
Q

What is the purpose of std::vector in C++?

A

To provide a dynamic array that allows for random access and automatic expansion of elements

Unlike C-style arrays which have a fixed size, std::vector can grow as needed.

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

What are the four components of the C++ Standard Library?

A
  • Algorithms
  • Containers
  • Iterators
  • Functions
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a container in the context of the Standard Library?

A

A data abstraction that stores a sequence of elements and iterators.

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

How does a C-style array store elements in memory?

A

Elements are stored contiguously in memory, allowing random access in O(1) time.

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

What is one limitation of C-style arrays?

A

Fixed size; they do not allow for dynamic expansion at runtime.

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

What is an example of a real-world application that requires dynamic storage?

A
  • Database query responses
  • Bluetooth scanning applications
  • Chat applications
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How can vectors be initialized in C++?

A
  • std::vector<int> v;</int>
  • std::vector<int> v(50);</int>
  • std::vector<int> v{1,2,3};</int>
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What method is used to add an element to the end of a vector?

A

push_back()

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

What does the size() method of a vector return?

A

The number of elements in the vector.

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

Fill in the blank: The stack memory is organized in a ______ fashion.

A

LIFO

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

What type of memory is used for dynamic memory allocation in C++?

A

Heap memory

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

What happens if a vector’s capacity is exceeded?

A

Reallocation to a new memory space is needed.

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

What does the capacity() function of a vector return?

A

The total amount of allocated memory for the vector.

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

True or False: The size of a vector is always equal to its capacity after adding an element.

A

False

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

What does the pop_back() method do in a vector?

A

Deletes the last element of the vector.

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

What is the maximum number of elements a vector can hold determined by?

A

Its capacity.

17
Q

What does the term ‘automatic expansion’ refer to in the context of std::vector?

A

The ability of a vector to increase its size dynamically as more elements are added.

18
Q

What is stored in the Code (or Text) segment of memory?

A

Where the compiled program sits in memory, read-only.

19
Q

What does the sizeof operator do in C++?

A

Gives the direct size in bytes of a data type or variable.

20
Q

What is the typical size of an int data type on a 64-bit CPU?

21
Q

What is the role of iterators in the Standard Template Library?

A

Allow programs to traverse data structures or ranges of elements.

22
Q

What happens when a vector is initialized with a specified size and default value?

A

The vector contains that many elements, all initialized to the specified value.

23
Q

What is a potential consequence of not properly deallocating memory from the heap?

A

Memory leak errors.

24
Q

What is the initial capacity of a vector when it is created without any elements?

25
What is the difference between accessing an element using at() and [] in a vector?
at() throws an exception for out-of-bounds access; [] returns garbage.