C++ Crash Course - Ch 4 - THE OBJECT LIFE CYCLE
Object Lifetime
- Storage duration begins, storage is allocated.
- Constructor is called.
- Object's lifetime begins.
- Object can be used in code.
- Object's lifetime ends.
- Destructor is called.
- Storage duration ends, storage is deallocated.
Automatic Storage Duration
Local (or automatic) variables are automatically allocated storage when they enter scope. The storage is deallocated when they leave scope, except for static
variables.
static variables
Declared using the static
keyword.
Global scope
Storage is allocated at program start and deallocated when program finishes.
Local scope
Storage allocated when first encountered during run time. Deallocated when program finishes. However the value set during any initialisation is only set when the variable's storage is allocated.
void power_up_rat_thing(int isotopes) {
static int rat_things_power = 200;
rat_things_power += isotopes;
}
On the first run through this function, rat_things_power
is set to 200, the value of Γ¬sotopes
is then added to this. On subsequent runs through this, rat_things_power
already has storage allocated and so it won't be reinitialised to 200, instead it will retain the value it had from the end of the previous call to this function.
Static members
Members of a class that aren't associated with a particular instance of that class.
Dynamic Storage
Allocated/deallocated on request.
int* my_int_ptr = new int { 42 }; // allocate storage for an int
delete my_int_ptr; // free up the storage for the int
Once deleted the contents of the storage of the int
are undefined. Compilers don't typically clean up memory - this can lead to a bug called use after free.
Dynamic Arrays
int* my_int_array_ptr = new int[400];
delete[] my_int_array_ptr;
Exceptions
c++ uses the 'try... throw... catch...' mechanism.
Exception Types
Exceptions thrown should be once of the exceptions from the stdlib
(found in in <stdexcept>
header).
The exceptions listed in <stdexcept>
are:-
Logic errors
logic_error
domain_error
invalid_argument
length_error
out_of_range
Runtime errors
runtime_error
range_error
overflow_error
underflow_error
system_error
- operating system errors.code()
method returnsstd::errc
that can explain the error
#include <stdexcept>
#include <cstdio>
struct Groucho {
void forget(int x) {
if(x == 0xFACE) {
throw std::runtime_error { "I'd be glad to make an exception." };
}
printf("Forgot 0x%x\n", x);
}
}
int main() {
Groucho groucho;
try {
groucho.forget(0xCODE);
groucho.forget(0xFACE);
groucho.forget(0xC0FFEE);
} catch (const std::runtime_error& e) {
printf("exception caught with message: %s\n", e.what());
}
}
Side note: if you are running on an ubuntu base you might need to compile code like this with...
gcc groucho.cpp -lstdc++
, the-l
flag specifies that you are linking with the standard c++ libraries. Supposedly the following negates the need for the -l flag...sudo apt-get install lib32z1 libc6-i386 libc6-dev-i386 lib32gcc1 lib32stdc++6
, the issue seems to be caused by compiling with a 32 bit compiler when you only have 64 bit libraries available.