Docsity

Constructors and Destructors - Lecture Slides | EECS 281, Study notes of Algorithms and Programming

Partial preview of the text

Download Constructors and Destructors - Lecture Slides | EECS 281 and more Study notes Algorithms and Programming in PDF only on Docsity!

Discussion slides for we

The University

e y

ek of October 17, 2007

y

of Michigan

y

g

Agenda

z

Constructors and Destructo

z

PA2 questions / discussion

Bi

d bi

i

−

Bi

ts and binary operations

−

Binary IO / Filesios::binary

−

ios::binary

−

Gprofsubversion/cvs (brief)

−

subversion/

cvs

(brief)

o

rs

Destructors

z

What are destructors used

z

Custom deallocation

z

Custom deallocation

z

Can destructors have para

z

When a variable goes out o

D

t

t

ll d!

z

D

estructor called!

z

Space is deallocatedL

l

i bl

d

t

z

L

ocal variables are destroye

z

Useful for deallocated all viclass

for? ameters? of scope

d i

th

d

f

ti

ed in the reverse order of creationirtual memory used in the

Example

Bits and Binary

z

Why do we care?

−

A deep understanding of com

f

h

t i

h

i

t th

b

of what is happening at the b

−

Common interview questions

z

Application examples

D

i

d i

f

−

D

evice drivers, parts of opera

−

Heavily optimized code

−

Embedded devicesEmbedded devices

−

Low-level file manipulation (e

−

Storing arbitrary data format

m

puters requires an understanding

bit l

l

bit levels

i

ating systemse.g. compression)ts efficiently

Data Representation

z

Standard variable types ho

−

char (1 byte):

1001000

int (2 4 bytes):

1001000

−

int (

-4 bytes):

1001000

−

long (4+ bytes):

1001000

z

Unsigned flag is often usefubeing interpreted as a nega

z

We often interpret chars asonly a particular interpreta

z

Assigning 00010000 to a ch

h

(d

l)

−

char x = 16;

(decimal)

−

char x = 020;

(octal)

−

char x = 0x10;

(hex)

;

(

)

o

ld sets of bits

(^010110010001011001000101100100011001000110010001) ful to prevent any of the bits

ation flags ASCII codes, but this is

tion of the bitshar ))

Bit Manipulations

Masking

z

Masking

−

How to extract the middH

t

l

th

iddl

−

H

ow to clear the middle

z

Packing

−

How do we combine 0x0b t ?byte?

d

le 4 bits from 01011100?4 bit

f

4 bits from 01011100?

04 and 0x03 into a single

Bit Fields

z

Bit fields are sometimes uscompressed structure in m

z

Not directly useful for IO

z

Bit shifts/operations can ac

struct DISK_REGISTER {

unsigned ready:1;unsigned error_occured:1;unsigned disk_spinning:1;…

};

s

eful for storing bits in aemory ccomplish the same thing

Symbols are just chunks

of bits

IO for binary files

O

fil

i

bi

z

O

pen files using binary

−

Prevents interpretationsfrom causing trouble

ifstream myFile ("datain.b

d

y

mode

s of newlines, spaces, etc.^ b

in", ios::in | ios::binary);

Reading and Writing Bits

Read from a file:

char buffer[100];ifstream myFile ("datain.bin", iomyFile.read (buffer, 100);// do stuff with the data

Write to a file:

char b ffer[100]char

buffer[100];

// fill the buffer with the data yoofstream myFile ("dataout bin"ofstream myFile ( dataout.binmyFile.write (buffer, 100);

o

s::in | ios::binary);

ou want" ios::out | ios::binary);

, ios::out | ios::binary);

ios::binary

z

Used while opening files:

myInStream open

(“

fileNa

myInStream

.open( fileNa h

d

z

Does it convert the data in

z

Trick Question!

z

The files are already in

z

What ios::binary does is prof-line characters, etc

ame txt

” ios::in | ios::binary);

ame.txt , ios::in | ios::binary);

f l

b

f

ile to binary? binary! Duh!

revent interpretation of end-

xxd output

0006530: 0000 0016 0000 0a26 000b 46010006530:

0000 0016 0000 0a26 000b 4601

0006540: 0001 d002 0037 2400 0f84 d002 50006550: 0e84 0000 5d84 0000 1184 98fe 60006560: 0f00 0037 2400 0f84 3804 5e84 30006570: 0000 5d84 0000 1184 0000 60840006580: 0000 0a26 010b 4601 000d c6050006580: 0000 0a26 010b 4601 000d c6050006590: 0037 2400 0f84 a005 5e84 a005 00006cf6: 00000000 00000000 01101010 000006cfc: 00000000 00000000 00000000 000006cfc: 00000000 00000000 00000000 000006d02: 01001111 00000000 01101100 00

000d c

& F

000d

c605 .......&..F.....

5e84 d002 .....7$.....^...6084 98fe ....].......`... 3804 0e84 ...7$...8.^.8...

0000 0016 ..].......`.....0001 a

& F

0001 a005 ...&..F......... 0e84 0000 .7$.....^.......

000000 00000000 00000000 ..j...000000 00000001 00000000000000 00000001 00000000 ...... 0000000 01100101 00000000 O.l.e.

Profiling^ z

A profiler allows you to iden

z

A profiler allows you to iden are taking the most time

−

Allows you to focus optimizat

T

k

z

T

o make your program 5% −

Optimizing a function that takOptimizing a function that tak

−

Optimizing a function that tak

−

Optimizing a function that tak

ntify which pieces of codentify which pieces of code t

ion effort where it is necessary

% f

t

% faster:

kes 1% of the total time?kes 10% of the total time?kes 10% of the total time?kes 50% of the total time?