CS 11 Cheat Sheet
CS 11 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
-
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
infile.close();
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.