Pointers and Reference Flashcards

1
Q

What is a pointer used for?

A
  • Inside functions, pointers can be used to access data that are defined outside the function. Those variables may not be in scope so you cant access them by their name.
  • Pointers can be used to operate on arrays very efficiently
  • We can allocate memory dynamically on the heap or free store
    • This memory doesn’t even have a variable name
    • The only way to get to it is via a pointer
  • With OO. pointers are how polymorphism works
  • Can access specific addresses in memory
    • Useful in embedded and system applications
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What to pay attention to when declaring pointers?

A
  • Always initialize pointers
  • Uninitialized pointers contain garbage data and can point anywhere
  • Initializing to zero or nullptr(C++ 11) represents address zero
    • Implies that the pointer is pointing nowhere
      • If you don’t initialize a pointer to point to a variable or function then you should initialize it to nullptr to make it null
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the sizeof all pointers?

A

4, the pointer is pointing to an address not the actual object, therefor all pointers have a sizeof 4.

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

What is the use of untyped VOID* pointers?

A

The void pointer, also known as the generic pointer, is a special type of pointer that can be pointed at objects of any data type! A void pointer is declared like a normal pointer, using the void keyword as the pointer’s type:

void* ptr; // ptr is a void pointer

Unlike typed pointers (e.g. int* number) void* pointers can be assigned any type of datatype.

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

What is dereferencing a pointer?

A

Dereferencing a pointer means that we are accessing the data the pointer is pointing to.

if score_ptr is a pointer and has a valid address then you can access the data at the address contained in the score_ptr using the dereferencing operator *

example:

int score = 100;
int *scopre_ptr = &score;

cout

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

How can I convert a void* to a char*?

A

First I need to cast the void* to a char*

char* ptr = (char*)voidp;

then I can use it just like I would a normal char pointer

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

How can I allocate dynamic storage (on the heap)

A

Using the new keyword.

int* int_ptr = nullptr;

int_ptr = new int; // allocated memory to store an integer (32bit or 4 bytes)

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

Whats the difference between stack and heap?

A

Stack

A stack is a special area of computer’s memory which stores temporary variables created by a function. In stack, variables are declared, stored and initialized during runtime.

It is a temporary storage memory. When the computing task is complete, the memory of the variable will be automatically erased. The stack section mostly contains methods, local variable, and reference variables.

Heap

The heap is a memory used by programming languages to store global variables. By default, all global variable are stored in heap memory space. It supports Dynamic memory allocation.

The heap is not managed automatically for you and is not as tightly managed by the CPU. It is more like a free-floating region of memory.

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

How can I allocate a array on the heap?

A

int * array_ptr = new int[size];

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

How can we free a dynamic storage from the heap?

A

Using the delete keyword.

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

What happens if I dereference an array?

int scores[] {100, 95, 95};

cout << *scores << endl;

A

It’ll point to the first element in the array therefor return 100.

An array is an address to the first element inside the memory.

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

Given an array

int array[] {1,2,3,4}

how can I access the elements inside the array with subscript notation and Offset notation

A

subscript:

array[index]

offset:

*(array + index)

this works for pointers pointing to the array the exact same way. An array is just a pointer pointing to the first element

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

What do the increment and decrement arithmetic operators do with pointers?

A

They will point the pointer to the next or last object in memory incremented or decremented by sizeof(type). Example:

int_ptr++;

if the type is an int it will either inc or dec by 4 bytes, if its a double it will be 8 bytes etc.

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

What is the difference between:

const int *score_ptr {&high_score};

and

int *const score_ptr {&high_score};

A

In the first example when setting the pointer to type const int changing the value the pointer is pointing to won’t be permitted, but changing the address the pointer is pointing to is ok. Example:

\*score\_ptr = 86; //ERROR
score\_ptr = &low\_score; //OK

In the second example when setting the pointer itself to a constant, it will be reversed. So setting the value of the reference will be OK but resigning the pointer to a different address won’t.

example:

\*score\_ptr = 86; //OK
score\_ptr = &low\_score; //ERROR

When setting both the datatype as well as the pointer to a constant both won’t be able to change after the declaration.

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

Given the following function:

void double_data(int* int_ptr) {
*int_ptr *= 2;
}

what values can I pass to it?

A

Either an int pointer pointing to an integer variable:

int* int_ptr = &value;

or a reference to an integer variable:

int value = 10;
double(&data);

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

What is the problem with these 2 functions?

int func() {
int size {};

return &size;
}

int *func2 () {
int size {};
int *int_ptr {&size};
return int_ptr;
}

A

They both return a pointer or reference to a local variable on the stack which will be freed at some point in time and can result in runtime errors.

Instead the functions should create storage on the heap that can later be freed by the calling function.

17
Q

Why would the first loop not change any element values?

vector stooges{ “1”, “2”, “3” };
for (auto str : stooges)
str = “FUNNY”;

for (auto str : stooges)
cout << str << endl;

A

Because the loop doesn’t create a reference for the vector. It should be

for (auto &str : stooges)
str = “FUNNY”;

18
Q

How can I create an alias for a local variable?
int num = 100;

A

Alias:

int &ref = num;

19
Q

Given the following 2 ways to loop through and change elements from an array:

for (auto str: stooges)
cout << str;

for (auto const &str: stooges)
cout << str;

which one is more efficient?

A

The second one because it’s using an alias to the array instead of creating a copy and also its write-safe because it’s setting the alias to a constant.

20
Q

What is an L value?

A

An L value is an object that occupies a location in memory and is addressable, also it has a name.

example:

int x = 100;

or

string name;

wrong example:

100 = x; (100 is a literal not an L value)

21
Q

What is an R-value?

A
  • An r-value is not addressable and not assignable.
  • on the right-hand side of an assignment expression
  • a literal

example:

x + 200

“Frank”

max(20, 30);

22
Q

Given the following function:

int square (int &n) {

return n*n;

}

would the following call cause a problem?

square(5);

A

Yes because 5 is an r-value but the function requires an l-value, in this case an int variable.

23
Q

What does a **ptr do?

A

A **ptr points to an address that points to another address.

Example:

int b = 2;
int *a = &b;

int **ptr = &a;

std: :cout << **ptr << std::endl;
result: 2