1/11
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
(Recap) Registers, Stack, Heap
Registers are small, extremely fast storage locations directly within the CPU. They hold data that the CPU needs immediate access to for executing instructions.
The stack is a region of memory that stores temporary data such as function parameters, return addresses, and local variables. e.g. int x = 10
The heap is a region of memory used for dynamic memory allocation. Blocks of memory are allocated and freed in an arbitrary order, which allows for flexible memory utilization but at a cost of higher overhead. e.g. int* ptr = new int(10);
(must be initialised with pointer)
What is a reference in C++?
A reference is an alias for another variable, essentially another name for the same memory location. For example, if int n;
then int& r = n;
defines r as a reference to n. Note that a reference must be initialized and must be associated with a variable, as shown: int& r; // Error: references must be initialized.
What is a pointer in C++?
A pointer is a variable that stores the address of another variable. For example, if int n;
then int* p = &n;
initializes p as a pointer to n.
&n is the memory address of variable n.
How to assign pointer and reference?
For variables on the stack:int *p = &n;
// unary operater extracts address
int &r = m;
For variables in heap:int* ptr = new int(10);
// creates new memory in heap and assigns address to pint &r = new int(10);
does not work, as m must be an existing object!
What does it mean to dereference a pointer and how is it done?
Dereferencing a pointer means accessing the value at the address the pointer is pointing to. This is done using the dereference operator (*).
For example, if int *p = &n;, then int i = *p; copies the value of n into i.
Difference between dereferencing and initialising?
Initialisation is when a new pointer or references is created and always starts with a type. e.g.
int *p = &n (initialises a pointer and inserts address of n)
int &r = n (initialises a reference and makes it an alias to n).
*p or &n alone would be dereferences.
What is a memory leak in programming?
A memory leak occurs when dynamically allocated memory is not freed after it's no longer needed, causing the program to consume more memory and potentially crash when it runs out of memory.
What causes a segmentation fault?
A segmentation fault occurs when a program tries to access memory that it shouldn't, either because the memory location is invalid or it has been freed (deallocated).
For example, a multiple pointers could be created for the same memory location:int *p = &n;
int *r = &n;
delete p;
This would cause r to throw an error when used.
What special pointer types do modern C++ implementations provide to help prevent common pointer errors?
Modern C++ implementations provide special pointer types that automatically manage and delete memory when it is no longer used, helping to prevent common errors like memory leaks and segmentation faults.
When should you use references instead of pointers in C++?
References should be used instead of pointers when you want to avoid the complexities and errors associated with pointer use, such as memory leaks and pointer arithmetic. References are safer as they are always bound to an object and cannot be NULL.
Why are references safer than pointers?
Initialization
Pointers can be initialized to any address, including nullptr
, or they can be left uninitialized, leading to undefined behavior if dereferenced.
References, on the other hand, must be initialized upon declaration to an existing variable. This requirement prevents the uninitialized state that can be problematic with pointer
Reassignment
Pointers can be reassigned to point to different objects or memory locations throughout their lifetime. This flexibility is powerful but also makes it easier to inadvertently create errors through incorrect memory access or leaks.
References cannot be reassigned once they are bound to an object. This immutability prevents the errors that can come from reassignment, making references safer for cases where the bound object does not need to change.
How can you demonstrate the use of a reference in C++ with a code example?
Consider this C++ code snippet:int i = 10;
int& k = i;
k = 20; // Changing k also changes i
cout << i; // Outputs 20, showing that i has changed through the reference.