0% found this document useful (0 votes)
15 views

C++ CS cheat sheet

Uploaded by

Sara George
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

C++ CS cheat sheet

Uploaded by

Sara George
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

C11 mid-term cheat Sheet Structs are passed by value ::

every member copied when

passed /returned
to from a function
:. Pass struct pointer avoid excessive
to
opping
a

Access member mem of struct pointerp with

*
Coutput ( p) mem mem .
error messages (
.
or pf
compiler naming flag error
flags
↓ checking
Compilation :
g ++ -g- my program -
Wall - Nextra
my-program . pp
-

Debug flag (for valgrind) executable name code file name

memory
&
operation (location
k + = 2 - k = k + 2 is the address of -> number (
int 4
=
;
# include iostream #fstream int p =
&u

it stream intile ; intile) number


cont
ic"adress of a is
""" and

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)

Compier determines size of function's stack frame based the


of variables in the function
types
-

on

Stack : Space computed at compile-time +


automatically managed
Heap : Space computed at run-time dynamically' +
manually managed
section used from bottom
top , keeps track of what funk program is in + variables
use
of memory to
-

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

Vector constructor >


int

na Sprivate members

vectors reserve more space on the


heap
When Size ==
Capacity and the user tries to
add another element (doubles
Capacity e .

g push-back (value

Complexity
>
- memory
quantify amount
:
referring filled
time/space
> to Slots problem requires based on size of input

(ignore constants instructions


,
worst-case Scenario upper bound
,

capacity and values remain


,
user doesn't see as-size fastest to slowest : 0(1)
,
0(log(us) ,
O(n) , OCnlogin) ,
0 (n2)
,
O(z)
for ic 1000

< 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

typedet Card * Data;


names of members
Struct Node & local

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 =

copying(with array instruct)


To avoid excessive pass struct pointer
Encapsulation: restricting
,
user access to
only some of class's members a
*
private class members accessed/changed in that class's methods getters/setters p-member Celebone-celeb ; Celeb Celeb-p & One-celeb ;
only
· :· =

# include "name h" .

in
every corresponding
·
#include "Class h" .
: other. files when the
name . pp file
class members are objects/type of other classes

You might also like