0% found this document useful (0 votes)
57 views1 page

CS 11 Cheat Sheet

Uploaded by

Sara George
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views1 page

CS 11 Cheat Sheet

Uploaded by

Sara George
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

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

k + = 2 - k = k + 2 & is the adress of operator location


...

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
infile.close();

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.

You might also like