C / C++ Flashcards

1
Q

What does the Indirection Operator do, and which symbol is it?

A

Dereferences a pointer, *

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

What kind of variable is this: “int &bruhh = bruh;”

A

Reference Variable (for the “bruh” int variable)

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

What kind of variable is this: “int *bruh;”

A

Pointer Variable (to any int variable)

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

How do you get the value of what a Pointer Variable is pointing to

A

Use * again after initialized, like:
C: cout < < *bruh

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

Which can be reassigned: pointers or references

A

Pointers

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

How do you get the address for a variable?

A

Use the & operator, like:
C: cout < < &bruh;
T: 0x4a00

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

What is the key word for the 0 address

A

nullptr

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

What is “nullptr”?

A

The 0 address

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

What is the * operator called to dereference a pointer

A

Indirection Operator

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

How do you get the first value of an array without using []

A

Use the * operator, like:
C: int bruh[] = {10,20,30};
C: cout < < *bruh;
T: “10”

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

How do you create an alias for an array?

A

Use a Pointer Variable

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

How do you easily print a newline?

A

Use the endl keyword, like:
C: cout &laquo_space;“bruh” &laquo_space;endl

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

Using the * operator for an array pointer, how do you reference an element that is not at index 0

A

Need to wrap with parenthesis after * and add to the pointer (or array), like:
C: int bruh[] = {10,20,30}, *p;
C: p = bruh;
C: cout < < *(p+1) < < endl < < *(bruh+2);
T: “20”
T: “30”

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

How can you change a pointer without reassigning it, and why is this useful?

A

Since it represents an address, you can increment/decrement with the “++” and “–” operators, which is useful if it is pointing to an array, like:
C: int bruh[] = {10,20,30}, *p;
C: cout < < *p < < endl < < *(++p);
T: 10
T: 20

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

How do you initialize a pointer for a variable efficiently

A

You can do it on the same line as the variable initialization, like:
C: int bruh, *b = &bruh;

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

What is an important difference of pointer assignment between Arrays and non-aggregate variable types

A

Since using Arrays in the code is treated as its address, you can assign the Array to the pointer directly without extra operators, whereas non-aggregate variables you must refer to their address with the & operator, like:
C: int var, array[], *pointer;
C: pointer = &var; // valid
C: pointer = array; // valid

17
Q

Can you assign a pointer to any kind of variable?

A

No, you initialize pointers with the type

18
Q

How do you test if a pointer is invalid?

A

This question is slightly misleading, because invalid can either mean it is not assigned yet, or it is assigned the null pointer. If it is not assigned yet, you cannot test this in the code because you really cannot do any operations on uninitialized variables without throwing an error. If it is assigned to address 0 (nullptr) then you can simply use the variable with or without the “!” operator directly in an “if” condition because it is treating the condition as if it was an int variable, like:
C: int var = 0, p;
C: if (p) {}; // this will throw errors
C: p = &var;
C: if (p) {}; // this will execute
C: if (
p) {}; // this will NOT execute, because the variable is 0
C: p = nullptr;
C: if (!p) {}; // this will execute

19
Q

What is the * operator called and what does it do

A

Indirection Operator, dereferences a pointer

20
Q

How do you change the value of a variable passed to and within a function?

A

Have a pointer parameter for the variable and use the & operator on the variable for that argument and reassign a value to it within the function by dereferencing it with the Indirection Operater *.

21
Q

What is important to know for const values when needing to reference them in a function

A

You have to specify that the pointer parameter is a const

22
Q

How do you make the pointer immutable, and what is an important thing to remember about doing this

A

Add const to the initialization, and you have to initialize it, like:
C: int * const constPointer; // This will throw an error
C: int var1 = 1, * const constPointer = &var1, var2 = 2;
C: cout < < *constPointer; // Prints “1”
C: constPointer = &var2; // This will throw an error

23
Q

How can you use a pointer like a variable? What is this topic called?

A

You can initialize the pointer and assign to a new address using the “new” keyword/operator, which regards Dynamic Memory Allocation, like:
C: int *p = new int;
C: *p = 2;
C: cout < < *p; // Prints “2”

24
Q

How do you clear out the memory at the address that a pointer has? What if the pointer is for an array? What if the pointer is assigned to nullptr?

A

Use the ‘delete’ keyword/operator, like:
C: delete p;
C: delete [] pArray;

The ‘delete’ operation does nothing if it is pointing to nothing (null, 0).

25
Q

You can return pointers from a function, but what must it point to?

A

It must be pointing to dynamically allocated memory (using the ‘new’ operator) or to an address given to the function as an argument (the only thing that makes sense for this would be a given Array, so you’re essentially returning a literal address instead of an index).

Essentially, it must NOT be a pointer for a variable local to the function.

26
Q

What are those # things at the top of the source code called?

A

Header files

27
Q

What are the 3 types of Smart Pointers, what are their keywords, what do they do, and what header file do you need, and what is the initialization syntax?

A
  1. Unique (unique_ptr), ??
  2. Shared (shared_ptr), ??
  3. Weak (weak_ptr), ??
    #include <memory>
    Example:
    C: unique_ptr<int> p (new int); // Creates a Unique int pointer named 'p'</int></memory>