Discussion slides for week of October 17, 2007

26
Discussion slides for we The Universiteek of October 17, 2007 y of Michigan 1

Transcript of Discussion slides for week of October 17, 2007

Discussion slides for weThe Universityy

eek of October 17, 2007y of Michigany g

1

Agenda

Constructors and Destructo

PA2 questions / discussion

Bi d bi i− Bits and binary operations

− Binary IO / Files

ios::binary− ios::binary

− Gprof

subversion/cvs (brief)− subversion/cvs (brief)

:

ors

2

Constructors

What are constructors used

Custom initializationCustom initialization

When a variable comes intoWhen a variable comes into

First space is allocated for it

Th i bl i i iti li dThe variable is initialized

Constructor called!

C t t h Can a constructor have para

Example

What about new statement

:

d for?

o scopeo scope

t

t ?ameters?

ts?

3

Destructors

What are destructors used

Custom deallocationCustom deallocation

Can destructors have para

When a variable goes out o

D t t ll d!Destructor called!

Space is deallocated

L l i bl d tLocal variables are destroye

Useful for deallocated all viclass

:

for?

ameters?

of scope

d i th d f tied in the reverse order of creation

irtual memory used in the

4

Example

: 5

Example (contd)

: 6

Bits and BinaryWhy do we care?− A deep understanding of com

f h t i h i t th bof what is happening at the b

− Common interview questions

Application examplesD i d i f − Device drivers, parts of opera

− Heavily optimized code

− Embedded devicesEmbedded devices

− Low-level file manipulation (e

− Storing arbitrary data format

:

mputers requires an understanding bit l lbit level

s

i ating systems

e.g. compression)

ts efficiently

7

Data RepresentationStandard variable types ho− char (1 byte): 1001000

int (2 4 bytes): 1001000− int (2-4 bytes): 1001000− long (4+ bytes): 1001000

Unsigned flag is often usefubeing interpreted as a nega

We often interpret chars asonly a particular interpreta

Assigning 00010000 to a chh (d l)− char x = 16; (decimal)

− char x = 020; (octal)− char x = 0x10; (hex); ( )

:

old sets of bits010110010001011001000101100100011001000110010001

ful to prevent any of the bits ation flag

s ASCII codes, but this is tion of the bits

har ))

8

Bit Manipulations

& AND 101& AND 101| OR 1010^ XOR 1010~ complement ~101 complement 101<< left shift 101

i ht hift 101>> right shift 101

:

0 & 1001 ????0 & 1001 = ????0 | 1001 = ???? 0 ^ 1001 = ????0 = ????0 ????0 << 2 = ????0 1 ????0 >> 1 = ????

9

Bit Manipulations

Masking Masking

− How to extract the midd

H t l th iddl − How to clear the middle

Packing

− How do we combine 0x0b t ?byte?

:

dle 4 bits from 01011100?

4 bit f 01011100? 4 bits from 01011100?

04 and 0x03 into a single

10

Bit Fields

Bit fields are sometimes uscompressed structure in m

Not directly useful for IO

Bit shifts/operations can ac

struct DISK_REGISTER {

unsigned ready:1;

unsigned error_occured:1;

unsigned disk_spinning:1;

};

:

seful for storing bits in a emory

ccomplish the same thing

11

Files are streams of bits

010010010101010100100100101010101000010010101111110010100110110010011000000000111101010000000001111010

:

010101010101001100101010101010011001001001010100101101010010101010100010001111001010000100011110010100

12

Symbols are just chunks

010010010101010100100100101010101000010010101111110010100110110010011000000000111101010000000001111010

:

of bits

010101010101001100101010101010011001001001010100101101010010101010100010001111001010000100011110010100

13

IO for binary files

O fil i biOpen files using binary− Prevents interpretations

from causing trouble

ifstream myFile ("datain.b

:

dy modes of newlines, spaces, etc.

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

14

Reading and Writing Bits

Can only read/write on byt

Don’t use >> and << (inse(

− These are for *formattin

Use read/write functions fo

No null character appen− No null character appenthe raw bits

− May not want to read evyvery large files; use bloc

:

te boundaries

ertion/extraction)/ )

ng*

or blocks of chars

ded no interpretation just ded, no interpretation, just

verything at one time for y gcks (e.g. 1KB at a time)

15

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);

:

os::in | ios::binary);

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

16

ios::binary

Used while opening files:

myInStream open(“fileNamyInStream.open( fileNa

h dDoes it convert the data in

Trick Question!

The files are already in

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

:

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

f l b file to binary?

binary! Duh!

revent interpretation of end-

17

Viewing binary files

A text editor interprets fileswell for generic binary fileswell for generic binary files

− xxd (hex +

− xxd -b (binary

− hexdump -C (hex +

:

s as ASCII, which won’t work ss

+ ASCII)

y + ASCII)

ASCII)

18

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 6084 0006580: 0000 0a26 010b 4601 000d c6050006580: 0000 0a26 010b 4601 000d c605 0006590: 0037 2400 0f84 a005 5e84 a005 0

0006cf6: 00000000 00000000 01101010 000006cfc: 00000000 00000000 00000000 000006cfc: 00000000 00000000 00000000 000006d02: 01001111 00000000 01101100 00

:

000d c605 & F000d c605 .......&..F.....5e84 d002 .....7$.....^...6084 98fe ....].......`...3804 0e84 ...7$...8.^.8...0000 0016 ..].......`.....0001 a005 & F0001 a005 ...&..F.........0e84 0000 .7$.....^.......

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

19

Profiling

A profiler allows you to idenA profiler allows you to idenare taking the most time

− Allows you to focus optimizat

T k 5%To make your program 5%− Optimizing a function that tak

Optimizing a function that tak− Optimizing a function that tak

− Optimizing a function that tak

:

ntify which pieces of code ntify which pieces of code

tion 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?

20

gprof

Compile with the -pg flag

Run the program as usual

− Will run somewhat slowe

You will have a “gmon.out”

− This is overwritten each

Run gprof to examine the d

gprof options [executable-file [

:

er

” file in your directory

time the program is run

data:

[profile-data-files...]] [> outfile]

21

gprof: flat profile output

: 22

gprof: call graph

: 23

Version Control

Subversion (svn) cvs proprietSubversion (svn), cvs, propriet− http://subversion.tigris.org/

http://svnbook red-bean com/− http://svnbook.red-bean.com/Useful for individual projects; *FunctionalityFunctionality− central repository for code− version history, rollbacks− revision merging− conflict detection, resolution assis

h li t l i− change lists, logging− branch management− tagginggg g

:

tary toolstary tools

crucial* for multiple developers

stance

24

Typical Use

Create a repository

Import some files into it

Check out a local “working

Make changesMake changes

Update to latest version

Fix any conflicts, test chang

Check in your changes

:

directory”

ges in latest version

25

Ques

:

stions?

26