C++ CS cheat sheet
C++ CS cheat sheet
passed /returned
to from a function
:. Pass struct pointer avoid excessive
to
opping
a
*
Coutput ( p) mem mem .
error messages (
.
or pf
compiler naming flag error
flags
↓ checking
Compilation :
g ++ -g- my program -
Wall - Nextra
my-program . pp
-
memory
&
operation (location
k + = 2 - k = k + 2 is the address of -> number (
int 4
=
;
# include iostream #fstream int p =
&u
name") value is
in file open ("non
, existent fire p
it (intile fail() ·
& When applied to a pointer, * is the "dereferencing
cout 12"
opening file failed "endI ; operator." If p is a pointer to location x, *p is the value at
exit (EXIT -
FAILURE location x.
3
.
infile close() If air arrdis
*
Carr i) Heap
is
array
.
: == +
into
It
*
i) grades ;
string curr; p is a
pointer men
:
(p +
= =
p[i]
infile >> curr; grades new int[size]
&arr [0]
=
air = =
while (!infile.eof))
G as name of is adress of zeroin element
delete /]
grades ;
{ array
cout curr
infile > >curr; Printer only needs type of thing
variables know size
-
to its
} pointing to
-
In computer memory all addresses are the same size maindfun( & num)
infile.close();
func (in+ * num)
on
When a variable of primitive type (int, char, bool, float) is passed to a function, a When an array is passed as an argument, only it's address is passed to the called
pass
by copy of that variable is stored in the stack frame of the called function. function.
value The original and the copy live at two different memory addresses. Thus, if the function changes that arr. it is changing the original as well.
Thus, if the function changes the value stored in that variable, it is changing the pass by reference
copy, not the original.
Redirection & Dilt
How does the compiler know how much space to allocate for a stack frame?
The size is completely determined by the types of the function's arguments and variables. /my-program <input . in > output out
The Program < inputin) output gt .
When is a function's stack frame pushed onto and popped off of the stack?
A function's stack frame is pushed when the function is called, and popped when the function returns. dist output. It output. out
If a function takes an array as an argument, it might be called different times with differently sized
arrays. But the function will always execute with the same amount of stack space. How is this possible?
The function will receive the address of the array argument, which will be stored in a statically-sized
pointer variable.
Arrays Stack vs .
Heap
int
grades [33 ; C-style strings
- This array is stored on the stack, in the stack frame of the function that declared it.
- When grades is passed to a function, the function actually receives the address of its stack location (pass-by-
reference). Char stra [b] =
E'h'e : 'I'l' ,
'o''o'3
- Automatically deleted from memory when it goes out of scope. char Str2 [6] = "hello"
;
*
int [b]
int
grades = new
; 3
- The pointer variable is stored on the stack, in the stack frame of the function that declared it. The array of
integers is stored on the heap.
- When grades is passed to a function, the function receives a copy of the heap address stored in the variable.
- The programmer must manually delete the associated heap memory.
CSIl-Final (in cout
use namespacestd ; -
,
#indet CLASS It
_
<prevent errors that include .
h files in other h .
> avoids double declaration
# define CLASS -H
# end it
main purpose :
Vectors : O(1) and variable size
na Sprivate members
g push-back (value
Complexity
>
- memory
quantify amount
:
referring filled
time/space
> to Slots problem requires based on size of input
< 10 size)
, ,
size is line push-back Binary search tree: Ollogin) arrays : O(n)
,
constant loop O(1)
arrs2s
by 1
Recursion :
When something is defined in terms of itself
duplicates one above it
So two
Recursive function
in a row
: a function that calls itself
mergesort < Onlogn)
*
replaces every Data with Card
int val
;
text ;
object uses dot operator to access members/methods of the class
string
*
Node left-child ;
*
Node right-child ;
every member copied
3; passed by value ,
can be using =
in
every corresponding
·
#include "Class h" .
: other. files when the
name . pp file
class members are objects/type of other classes