CS 2 Flashcards

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play

1/35

flashcard set

Earn XP

Description and Tags

CS2 ultra cards

36 Terms

1
New cards

What does &x mean, and what does p mean?

&x gets x's address; p accesses the value at p's address.

2
New cards

What is the relationship between arr[i] and pointer arithmetic?

arr[i] is equivalent to *(arr + i).

3
New cards

Which index accesses the fourth element of an array?

Index 3. C++ arrays start at index 0.

4
New cards

Can a stack pointer point to a heap object?

Yes. The pointer and the object it points to can be in different memory regions.

5
New cards

How do you dynamically allocate an integer array of size n?

int* arr = new int[n];

6
New cards

How do you release an array allocated with new[]?

delete[] arr;

7
New cards

How do you dynamically allocate and release one integer?

int* p = new int; then delete p;

8
New cards

What is a memory leak?

Allocated memory becomes unreachable without being released.

9
New cards

What is a dangling pointer?

A pointer that refers to memory that is no longer valid.

10
New cards

When is a local object's destructor automatically called?

When the object goes out of scope.

11
New cards

When is a dynamically allocated object's destructor called?

When its memory is released using delete.

12
New cards

What is a shallow copy of an object containing an owning pointer?

It copies the pointer address, not the dynamically allocated data.

13
New cards

What are three risks of shallow copying owned memory?

Shared modifications, dangling pointers, and double deletion.

14
New cards

What is a deep copy?

A copy that allocates separate memory and duplicates the original data.

15
New cards

What must a deep-copy constructor do before copying array elements?

Allocate a separate destination array of the required size.

16
New cards

What are the essential steps of deep-copy assignment?

Check self-assignment, release old memory, allocate new memory, copy elements, return *this.

17
New cards

Why check this != &objToCopy in operator=?

To prevent self-assignment from destroying the object's own data.

18
New cards

What are the three functions in the Rule of Three?

Destructor, copy constructor, and copy assignment operator.

19
New cards

What does unique_ptr provide, and what does std::move do?

Exclusive ownership; std::move transfers ownership to another unique_ptr.

20
New cards

When is a shared_ptr-managed object destroyed?

When its last shared_ptr owner releases it.

21
New cards

What header is required for file input/output?

include <fstream>



22
New cards

Declare an input file stream named inFS.

ifstream inFS;

23
New cards

Open data.txt for reading using inFS.

inFS.open("data.txt");

24
New cards

How do you check whether inFS successfully opened a file?

inFS.is_open()

25
New cards

How do you close an input file stream?

inFS.close();

26
New cards

Declare an output file stream named outFS.

ofstream outFS;

27
New cards

Write the integer total to an output file stream.

outFS << total;

28
New cards

What does eof() return?

True if the previous stream operation reached end-of-file.

29
New cards

What does fail() return?

True if the previous stream operation failed.

30
New cards

What does good() return?

True if no stream errors have occurred.

31
New cards

How do you reset an istringstream's error flags?

inSS.clear();

32
New cards

How do you replace an istringstream's contents with line?

inSS.str(line);

33
New cards

What does while (inSS >> value) check?

Whether extraction succeeded; stops at EOF or invalid input.

34
New cards

Declare an output string stream named outSS.

ostringstream outSS;

35
New cards

Append a name and age to an output string stream.

outSS << name << " " << age;

36
New cards

Retrieve the string stored in outSS.

string result = outSS.str();