C++ Flashcards

1
Q

lvalue

A

a locatable (in memory) value.
a left hand value.
an “assignable” value.

http://eli.thegreenplace.net/2011/12/15/understanding-lvalues-and-rvalues-in-c-and-c

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

rvalue

A

not an lvalue.
temporary object.
not addressable in memory

http://eli.thegreenplace.net/2011/12/15/understanding-lvalues-and-rvalues-in-c-and-c

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

c++11 move semantics

A

allows rvalue -> lvalue assignment?

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

Big 3

A
Relevent for class managing non trivial resources (ie heap data).
Consists of destructor, copy constructor and copy assignment operator (= overload)
If one is necessary to be defined, likely ALL are necessary due to complexities of class semantics.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Big 5

A

Big3 + c++11 move semantics.

+move construtor and move assignment

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

Copy and Swap Idiom

A

Relates to Big 3 and copy assignment.
Describes implementation of copy assignment.
Makes copy assignment simple and exception safe.
Requires definition of copy constructor, destructor and swap function.
1. create temp object (or in c++11 use passed in rvalue)
2. swap destination object and temp object data (pointer)
3. destroy temp object (c++11 rvalue destroyed automatically)

https://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom

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

Template Function Syntax

A

template
T Foo(Bar b){}
or
void Foo(T b){}

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

braced initialization

A

int a{0};
preferred as its more widely compatible in different situations.
one caveat is that if you’re overloading constructors and one constructor takes std::initializer_list then that will be preferred to all other constructors - even though they may be more sensible.

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

pimpl idiom to reduce build times (less header dependencies)

A
Basically moves all of class implementation to .cpp so header doesn't have to include details of members.
Therefore clients don't have to include those headers either.
Unique_ptr is preferred, but comes with gotcha's.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

smart pointers c++11/14

A

performance of unique_ptr almost matches raw.
prefer unique_ptr for single ownership.
shared_ptr is at least 2x as large as unique/raw and concurrent performance is a problem.
prefer shared_ptr for shared ownership.
prefer make_unique/make_shared to new.

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

use constexpr when applicable

A

constexpr functions can produce compile-time results.

ex: size std::array based on function output at compile-time.

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

use noexcept when applicable

A

improves ability for compiler to optimize.

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

override

A

declare functions as override when thats what you’re doing.

avoids potential bugs where its actually not overriding but rather calling a separate function.

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

prefer delete to private undefined functions

A

to disable copying, declare the copy constructor and delete: ex: Constructor() = delete;

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

enum classes

A

use them!!!

no need for pre/appending name as they are in their own namespace.

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

alias declarations

A

using foobar = std::vector;

can be used with templates more easily than typedef.

17
Q

nullptr

A

use it!!!

way! better than 0 or NULL which are basically hacks.

18
Q

auto

A

prefer, but has gotcha’s:
ex: auto bSomething = funcReturnsStdVectOfBools()[n];
in this unique case (amongst others) behavior is undefined.
The rvalue returned is of type std::vector::reference and the desired behavior is for this to be converted to a bool type not a &bool type - since a reference to the rvalue would result in undefined behavior. But this is just what auto bSomething will do.
The following is necessary:
ex: auto bSomething = static_castfunc…()[n];

19
Q

Template Class Syntax

A
template
class Foo
{};
20
Q

TDD in C++

A
  1. write test (won’t compile because no source)
  2. write source stubs (test now compiles, but fails)
  3. fill in source till test passes
  4. refactor so code is best possible form (and passes)

http://www.drdobbs.com/cpp/test-driven-development-in-cc/184401572

21
Q

Virtual Destructors

A
When a derived class is deleted by its base pointer a base virtual destructor is required to avoid undefined behavior.
Abstract interfaces require it.
22
Q

strace

A

trace system calls

23
Q

valgrind

A

heap memory analyzer

24
Q

gprof, oprofile, perf

A

performance analyzers

25
Q

gcov

A

code coverage analyzer

26
Q

perf

A

observe, collect stats on system.

CPU, I/O, software, scheduling.

27
Q

random numbers C/C++

A
C:
#include 
#include 
srand(time(NULL)); // seed, once per thread
rand(); // returns random number
C++:
#include 
#include 
srand((unsigned)time(0)); // seed, once per thread
rand(); // returns random number
28
Q

C unions

A
ex:
union Data {
   int i;
   float f;
   char str[20];
} data;
Aliasing single memory as different types.
Purpose is to time share memory with different types.
29
Q

C++ &&

A

rvalue reference.

overloaded construct/copy operators to use rvalues.

30
Q

C measuring time (clock_t)

A
#include 
clock_t start = clock(), diff;
doSomething();
diff = clock() - start;
unsigned ms = (diff * 1000)/CLOCKS_PER_SEC;
31
Q

C/C++ main signature

A
int main(int argc, char** argv)
int main(int argc, char* argv[])
int main(void)
32
Q

C struct packing

A

struct variables will align to memory so order/group them to optimally fill memory words (8bytes on 64bit)

64bit system (pointers are 8bytes):
struct foo {
int b; //4 bytes (will consume 8 bytes to align)
void* p; // 8 bytes
};
values will need to align in memory.
33
Q

const char*
char* const
const char* const

A

point to const char
const pointer to char
const pointer to const char

34
Q

space float vs. double

A

double takes 2x the space as it has double accuracy

typically float=4bytes, double=8

35
Q

C I/O

A
#include 
#include 
printf("%s, %c, %p, %u, %d\n", string, char, pointer, unsigned, int);
36
Q

C++ I/O

A
#include 
#include 
cout << "hello" << a << f << endl;
string input;
getline(cin, input);
cout << intput;
37
Q

C heap allocation/deallocate

A

struct node* head = malloc(sizeof(struct node));

free(head);

38
Q

C++ heap allocation/deallocation

A

struct node* head = new struct node();
delete head;
int array[] = new int[10];
delete [] array;

39
Q

Virtual Template Function?

A

Virtual methods mean a run-time vtable lookup.
Templates are resolved at compile-time.
Not compatible.