swt.informatik.uni-freiburg.de · – 2014-04 – main – F o r m a l M e t hod s f o r C S e m i...

35
– 2014-04 – main – Formal Methods for C Seminar – Summer Semester 2014 Daniel Dietsch, Sergio Feo Arenis, Marius Greitschus, Bernd Westphal Content – 2014-04 – overview – 7/125 Brief history Comments Declarations and Scopes Variables Expressions and Statements Functions Scopes Pointers Dynamic Storage & Storage Duration Storage Class Specifiers Strings and I/O Tools & Modules Formal Methods for C Common Errors Function Pointers – 2014-04 – pointers – 36/125 Functions in the System’s Memory – 2014-04 – pointers – 37/125 1 void f() { return ; } ... 0x1000 0x1001 RET 0x1002 0x1003 0x1004 0x1005 0x1006 0x1007 0x1008 0x1009 0x100A 0x100B 0x100C 0x100D 0x100E 0x100F 0x1010 0x1011 0x1012 0x1013 0x1014 0x1015 0x1016 0x1017 ... the compiler chose to store the machine code of ’f’ at memory cell with address 0x1001 Calling Functions – 2014-04 – pointers – 38/125 1 void f() { return ; } 2 f(); Calling Functions – 2014-04 – pointers – 38/125 1 void f() { return ; } 2 f(); ... 0x1000 0x1001 RET 0x1002 0x1003 0x1004 0x1005 0x1006 0x1007 0x1008 0x1009 0x100A 0x100B CALL 0x100C 0x10 0x100D 0x01 0x100E 0x100F 0x1010 0x1011 0x1012 0x1013 0x1014 0x1015 0x1016 0x1017 ... calling ’f’ means machine op CALL with address of callee (here: f, address 0x1001)

Transcript of swt.informatik.uni-freiburg.de · – 2014-04 – main – F o r m a l M e t hod s f o r C S e m i...

Page 1: swt.informatik.uni-freiburg.de · – 2014-04 – main – F o r m a l M e t hod s f o r C S e m i na r – Su mm e r S e m e s t e r 2014 D a n i e l D i e t s c h, S e rg i o F

– 2014-04 – main –

Form

al

Meth

ods

for

C

Sem

ina

r–

Su

mm

erS

emester

20

14

Daniel

Dietsch

,Serg

ioFeo

Aren

is,Mariu

sGreitsch

us,BerndW

estp

hal

Conten

t

– 2014-04 – overview –

7/125

•Brief

history

•Comments

•Declaratio

nsandScopes

•Variab

les

•Expression

sandStatem

ents

•Function

s

•Scop

es

•Pointers

•Dynam

icStorag

e&

Storag

eDuratio

n

•Storag

eClass

Specifi

ers

•Strin

gsandI/O

•Tools&

Modules

•Form

alMeth

odsfor

C

•CommonErrors

Fu

nctio

nP

oin

ters

– 2014-04 – pointers –

36/125

Functio

ns

inth

eSystem

’sM

emory

– 2014-04 – pointers –

37/125

1void

f()

{retu

rn;

}

...0x1000

0x1001

RET

0x1002

0x1003

0x1004

0x1005

0x1006

0x1007

0x1008

0x1009

0x100A

0x100B

0x100C

0x100D

0x100E

0x100F

0x1010

0x1011

0x1012

0x1013

0x1014

0x1015

0x1016

0x1017

...

thecompiler

chose

tostore

themach

inecode

of’f’

atmem

orycell

with

address

0x1001

Callin

gF

unctio

ns

– 2014-04 – pointers –

38/125

1void

f()

{retu

rn;

}2

f();

Callin

gF

unctio

ns

– 2014-04 – pointers –

38/125

1void

f()

{retu

rn;

}2

f();

...0x1000

0x1001

RET

0x1002

0x1003

0x1004

0x1005

0x1006

0x1007

0x1008

0x1009

0x100A

0x100B

CALL

0x100C

0x10

0x100D

0x01

0x100E

0x100F

0x1010

0x1011

0x1012

0x1013

0x1014

0x1015

0x1016

0x1017

...

calling’f’

mean

smach

ineopCALLwith

address

ofcallee

(here:

f,address

0x1001)

Page 2: swt.informatik.uni-freiburg.de · – 2014-04 – main – F o r m a l M e t hod s f o r C S e m i na r – Su mm e r S e m e s t e r 2014 D a n i e l D i e t s c h, S e rg i o F

AP

oin

terto

‘f’

(16-b

itA

rchitectu

re)

– 2014-04 – pointers –

39/125

1void

f()

{retu

rn;

}2

f();

3void

(∗p)

()

=&f;

AP

oin

terto

‘f’

(16-b

itA

rchitectu

re)

– 2014-04 – pointers –

39/125

1void

f()

{retu

rn;

}2

f();

3void

(∗p)

()

=&f;

...0x1000

0x1001

RET

0x1002

0x1003

0x1004

0x10

0x1005

0x01

0x1006

0x1007

0x1008

0x1009

0x100A

0x100B

CALL

0x100C

0x10

0x100D

0x01

0x100E

0x100F

0x1010

0x1011

0x1012

0x1013

0x1014

0x1015

0x1016

0x1017

...

‘p’isavariab

lewhich

storestheaddress

ofa

functio

n(here:

of‘f’)

Dereferen

ceF

unctio

nP

oin

ters

– 2014-04 – pointers –

40/125

1void

f()

{retu

rn;

}2

f();

3void

(∗p)

()

=&f;

4(∗

p)();

...0x1000

0x1001

RET

0x1002

0x1003

0x1004

0x10

0x1005

0x01

0x1006

0x1007

0x1008

0x1009

0x100A

0x100B

CALL

0x100C

0x10

0x100D

0x01

0x100E

0x100F

0x1010

MOV

0x1011

0x10

0x1012

0x04

0x1013R

0x1014

CALL

0x1015R

0x1016

0x1017

...

Dereferen

ceF

unctio

nP

oin

ters

– 2014-04 – pointers –

40/125

1void

f()

{retu

rn;

}2

f();

3void

(∗p)

()

=&f;

4(∗

p)();

...0x1000

0x1001

RET

0x1002

0x1003

0x1004

0x10

0x1005

0x01

0x1006

0x1007

0x1008

0x1009

0x100A

0x100B

CALL

0x100C

0x10

0x100D

0x01

0x100E

0x100F

0x1010

MOV

0x1011

0x10

0x1012

0x04

0x1013R

0x1014

CALL

0x1015R

0x1016

0x1017

...

callingvia

‘p’mean

sread

valueofp(here:

0x1001)

into

register

R...

Dereferen

ceF

unctio

nP

oin

ters

– 2014-04 – pointers –

40/125

1void

f()

{retu

rn;

}2

f();

3void

(∗p)

()

=&f;

4(∗

p)();

...0x1000

0x1001

RET

0x1002

0x1003

0x1004

0x10

0x1005

0x01

0x1006

0x1007

0x1008

0x1009

0x100A

0x100B

CALL

0x100C

0x10

0x100D

0x01

0x100E

0x100F

0x1010

MOV

0x1011

0x10

0x1012

0x04

0x1013R

0x1014

CALL

0x1015R

0x1016

0x1017

...

callingvia

‘p’mean

sread

valueofp(here:

0x1001)

into

register

R...

...andthen

mach

ineop

CALLwith

R,calls

address

storedin

R

Po

inters

vs.A

rrays

– 2014-04 – pointers –

41/125

Page 3: swt.informatik.uni-freiburg.de · – 2014-04 – main – F o r m a l M e t hod s f o r C S e m i na r – Su mm e r S e m e s t e r 2014 D a n i e l D i e t s c h, S e rg i o F

Arra

ys

– 2014-04 – pointers –

42/125

1char

a[5

]=

{’H

’,

’e’,

’l’,

’l’,

’o’

};

reservesom

espace

for5ch

ars...

...andlet

apoin

tto

that

space

...0x1000

0x1001

0x10

0x1002

0x08

0x1003

0x1004

0x1005

0x1006

0x1007

0x1008

’H’

0x1009

’e’0x100A’l’

0x100B’l’

0x100C

’o’

0x100D

0x100E

0x100F

0x1010

0x1011

0x1012

0x1013

0x1014

0x1015

0x1016

0x1017

...

a

Arra

ys

– 2014-04 – pointers –

43/125

1char

a[5

]=

{’H

’,

’e’,

’l’,

’l’,

’o’

};

2int

i;

3fo

r(i=

0;

i<

5;++i)

4a[i]=

’x’;

...0x1000

0x1001

0x10

0x1002

0x08

0x1003

0x..

0x1004

0x..

0x1005

0x1006

0x1007

0x1008

’H’

0x1009

’e’0x100A’l’

0x100B’l’

0x100C

’o’

0x100D

0x100E

0x100F

0x1010

0x1011

0x1012

0x1013

0x1014

0x1015

0x1016

0x1017

...

i

Arra

ys

– 2014-04 – pointers –

43/125

1char

a[5

]=

{’H

’,

’e’,

’l’,

’l’,

’o’

};

2int

i;

3fo

r(i=

0;

i<

5;++i)

4a[i]=

’x’;

...0x1000

0x1001

0x10

0x1002

0x08

0x1003

0x00

0x1004

0x01

0x1005

0x1006

0x1007

0x1008

’x’0x1009

’e’0x100A’l’

0x100B’l’

0x100C

’o’

0x100D

0x100E

0x100F

0x1010

0x1011

0x1012

0x1013

0x1014

0x1015

0x1016

0x1017

...

i

Arra

ys

– 2014-04 – pointers –

43/125

1char

a[5

]=

{’H

’,

’e’,

’l’,

’l’,

’o’

};

2int

i;

3fo

r(i=

0;

i<

5;++i)

4a[i]=

’x’;

...0x1000

0x1001

0x10

0x1002

0x08

0x1003

0x00

0x1004

0x02

0x1005

0x1006

0x1007

0x1008

’x’0x1009

’x’0x100A’l’

0x100B’l’

0x100C

’o’

0x100D

0x100E

0x100F

0x1010

0x1011

0x1012

0x1013

0x1014

0x1015

0x1016

0x1017

...

i

Arra

ys

– 2014-04 – pointers –

43/125

1char

a[5

]=

{’H

’,

’e’,

’l’,

’l’,

’o’

};

2int

i;

3fo

r(i=

0;

i<

5;++i)

4a[i]=

’x’;

...0x1000

0x1001

0x10

0x1002

0x08

0x1003

0x00

0x1004

0x03

0x1005

0x1006

0x1007

0x1008

’x’0x1009

’x’0x100A

’x’0x100B’l’

0x100C

’o’

0x100D

0x100E

0x100F

0x1010

0x1011

0x1012

0x1013

0x1014

0x1015

0x1016

0x1017

...

i

Arra

ys

– 2014-04 – pointers –

43/125

1char

a[5

]=

{’H

’,

’e’,

’l’,

’l’,

’o’

};

2int

i;

3fo

r(i=

0;

i<

5;++i)

4a[i]=

’x’;

...0x1000

0x1001

0x10

0x1002

0x08

0x1003

0x00

0x1004

0x04

0x1005

0x1006

0x1007

0x1008

’x’0x1009

’x’0x100A

’x’0x100B

’x’0x100C

’o’

0x100D

0x100E

0x100F

0x1010

0x1011

0x1012

0x1013

0x1014

0x1015

0x1016

0x1017

...

i

Page 4: swt.informatik.uni-freiburg.de · – 2014-04 – main – F o r m a l M e t hod s f o r C S e m i na r – Su mm e r S e m e s t e r 2014 D a n i e l D i e t s c h, S e rg i o F

Arra

ys

– 2014-04 – pointers –

43/125

1char

a[5

]=

{’H

’,

’e’,

’l’,

’l’,

’o’

};

2int

i;

3fo

r(i=

0;

i<

5;++i)

4a[i]=

’x’;

...0x1000

0x1001

0x10

0x1002

0x08

0x1003

0x00

0x1004

0x05

0x1005

0x1006

0x1007

0x1008

’x’0x1009

’x’0x100A

’x’0x100B

’x’0x100C

’x’0x100D

0x100E

0x100F

0x1010

0x1011

0x1012

0x1013

0x1014

0x1015

0x1016

0x1017

...

i

Arra

ysvs.

Poin

ters

– 2014-04 – pointers –

44/125

1char

a[5

]=

{’H

’,

’e’,

’l’,

’l’,

’o’

};

2char∗

p=

a;

//

not&a

!3

for

(int

i=

0;

i<

5;++i,++p)

4∗p

=’o

’;

...0x1000

0x1001

0x10

0x1002

0x08

0x1003

0x..

0x1004

0x..

0x1005

0x1006

0x1007

0x1008

’x’0x1009

’x’0x100A

’x’0x100B

’x’0x100C

’x’0x100D

0x100E

0x100F

0x1010

0x1011

0x1012

0x1013

0x1014

0x1015

0x1016

0x1017

...

p

Arra

ysvs.

Poin

ters

– 2014-04 – pointers –

44/125

1char

a[5

]=

{’H

’,

’e’,

’l’,

’l’,

’o’

};

2char∗

p=

a;

//

not&a

!3

for

(int

i=

0;

i<

5;++i,++p)

4∗p

=’o

’;

...0x1000

0x1001

0x10

0x1002

0x08

0x1003

0x10

0x1004

0x08

0x1005

0x1006

0x1007

0x1008

’x’0x1009

’x’0x100A

’x’0x100B

’x’0x100C

’x’0x100D

0x100E

0x100F

0x1010

0x1011

0x1012

0x1013

0x1014

0x1015

0x1016

0x1017

...

p

Arra

ysvs.

Poin

ters

– 2014-04 – pointers –

44/125

1char

a[5

]=

{’H

’,

’e’,

’l’,

’l’,

’o’

};

2char∗

p=

a;

//

not&a

!3

for

(int

i=

0;

i<

5;++i,++p)

4∗p

=’o

’;

...0x1000

0x1001

0x10

0x1002

0x08

0x1003

0x10

0x1004

0x09

0x1005

0x1006

0x1007

0x1008

’o’

0x1009

’x’0x100A

’x’0x100B

’x’0x100C

’x’0x100D

0x100E

0x100F

0x1010

0x1011

0x1012

0x1013

0x1014

0x1015

0x1016

0x1017

...

p

Arra

ysvs.

Poin

ters

– 2014-04 – pointers –

44/125

1char

a[5

]=

{’H

’,

’e’,

’l’,

’l’,

’o’

};

2char∗

p=

a;

//

not&a

!3

for

(int

i=

0;

i<

5;++i,++p)

4∗p

=’o

’;

...0x1000

0x1001

0x10

0x1002

0x08

0x1003

0x10

0x1004

0x0A

0x1005

0x1006

0x1007

0x1008

’o’

0x1009

’o’

0x100A

’x’0x100B

’x’0x100C

’x’0x100D

0x100E

0x100F

0x1010

0x1011

0x1012

0x1013

0x1014

0x1015

0x1016

0x1017

...

p

Arra

ysvs.

Poin

ters

– 2014-04 – pointers –

44/125

1char

a[5

]=

{’H

’,

’e’,

’l’,

’l’,

’o’

};

2char∗

p=

a;

//

not&a

!3

for

(int

i=

0;

i<

5;++i,++p)

4∗p

=’o

’;

...0x1000

0x1001

0x10

0x1002

0x08

0x1003

0x10

0x1004

0x0B

0x1005

0x1006

0x1007

0x1008

’o’

0x1009

’o’

0x100A

’o’

0x100B

’x’0x100C

’x’0x100D

0x100E

0x100F

0x1010

0x1011

0x1012

0x1013

0x1014

0x1015

0x1016

0x1017

...

p

Page 5: swt.informatik.uni-freiburg.de · – 2014-04 – main – F o r m a l M e t hod s f o r C S e m i na r – Su mm e r S e m e s t e r 2014 D a n i e l D i e t s c h, S e rg i o F

Arra

ysvs.

Poin

ters

– 2014-04 – pointers –

44/125

1char

a[5

]=

{’H

’,

’e’,

’l’,

’l’,

’o’

};

2char∗

p=

a;

//

not&a

!3

for

(int

i=

0;

i<

5;++i,++p)

4∗p

=’o

’;

...0x1000

0x1001

0x10

0x1002

0x08

0x1003

0x10

0x1004

0x0C

0x1005

0x1006

0x1007

0x1008

’o’

0x1009

’o’

0x100A

’o’

0x100B

’o’

0x100C

’x’0x100D

0x100E

0x100F

0x1010

0x1011

0x1012

0x1013

0x1014

0x1015

0x1016

0x1017

...

p

Arra

ysvs.

Poin

ters

– 2014-04 – pointers –

44/125

1char

a[5

]=

{’H

’,

’e’,

’l’,

’l’,

’o’

};

2char∗

p=

a;

//

not&a

!3

for

(int

i=

0;

i<

5;++i,++p)

4∗p

=’o

’;

...0x1000

0x1001

0x10

0x1002

0x08

0x1003

0x10

0x1004

0x0D

0x1005

0x1006

0x1007

0x1008

’o’

0x1009

’o’

0x100A

’o’

0x100B

’o’

0x100C

’o’

0x100D

0x100E

0x100F

0x1010

0x1011

0x1012

0x1013

0x1014

0x1015

0x1016

0x1017

...

p

Integ

erA

rrays

– 2014-04 – pointers –

45/125

1int

a[3

]=

{10,

010,

0x1234

};

...0x1000

0x1001

0x10

0x1002

0x08

0x1003

0x1004

0x1005

0x1006

0x1007

0x1008

0x00

0x1009

0x0A

0x100A

0x00

0x100B

0x08

0x100C

0x12

0x100D

0x34

0x100E

0x100F

0x1010

0x1011

0x1012

0x1013

0x1014

0x1015

0x1016

0x1017

...

a

reservesom

espace

for3ints...

...andlet

apoin

tto

that

space

Integ

erA

rrays

– 2014-04 – pointers –

46/125

1int

a[3

]=

{10,

010,

0x1234

};

2int

i;

3fo

r(i=

0;

i<

3;++i)

4a[i]=

0x27;...

0x1000

0x1001

0x10

0x1002

0x08

0x1003

0x..

0x1004

0x..

0x1005

0x1006

0x1007

0x1008

0x00

0x1009

0x0A

0x100A

0x00

0x100B

0x08

0x100C

0x12

0x100D

0x34

0x100E

0x100F

0x1010

0x1011

0x1012

0x1013

0x1014

0x1015

0x1016

0x1017

...

i

Integ

erA

rrays

– 2014-04 – pointers –

46/125

1int

a[3

]=

{10,

010,

0x1234

};

2int

i;

3fo

r(i=

0;

i<

3;++i)

4a[i]=

0x27;...

0x1000

0x1001

0x10

0x1002

0x08

0x1003

0x00

0x1004

0x01

0x1005

0x1006

0x1007

0x1008

0x00

0x1009

0x27

0x100A

0x00

0x100B

0x08

0x100C

0x12

0x100D

0x34

0x100E

0x100F

0x1010

0x1011

0x1012

0x1013

0x1014

0x1015

0x1016

0x1017

...

i

Integ

erA

rrays

– 2014-04 – pointers –

46/125

1int

a[3

]=

{10,

010,

0x1234

};

2int

i;

3fo

r(i=

0;

i<

3;++i)

4a[i]=

0x27;...

0x1000

0x1001

0x10

0x1002

0x08

0x1003

0x00

0x1004

0x02

0x1005

0x1006

0x1007

0x1008

0x00

0x1009

0x27

0x100A

0x00

0x100B

0x27

0x100C

0x12

0x100D

0x34

0x100E

0x100F

0x1010

0x1011

0x1012

0x1013

0x1014

0x1015

0x1016

0x1017

...

i

Page 6: swt.informatik.uni-freiburg.de · – 2014-04 – main – F o r m a l M e t hod s f o r C S e m i na r – Su mm e r S e m e s t e r 2014 D a n i e l D i e t s c h, S e rg i o F

Integ

erA

rrays

– 2014-04 – pointers –

46/125

1int

a[3

]=

{10,

010,

0x1234

};

2int

i;

3fo

r(i=

0;

i<

3;++i)

4a[i]=

0x27;...

0x1000

0x1001

0x10

0x1002

0x08

0x1003

0x00

0x1004

0x03

0x1005

0x1006

0x1007

0x1008

0x00

0x1009

0x27

0x100A

0x00

0x100B

0x27

0x100C

0x00

0x100D

0x27

0x100E

0x100F

0x1010

0x1011

0x1012

0x1013

0x1014

0x1015

0x1016

0x1017

...

i

Integ

erA

rrays

vs.P

oin

ters

– 2014-04 – pointers –

47/125

1int

a[3

]=

{10,

010,

0x1234

};

2int∗

p=

a;

3fo

r(int

i=

0;

i<

3;++p)

4∗p

=0x3421;...

0x1000

0x1001

0x10

0x1002

0x08

0x1003

0x..

0x1004

0x..

0x1005

0x1006

0x1007

0x1008

0x00

0x1009

0x27

0x100A

0x00

0x100B

0x27

0x100C

0x00

0x100D

0x27

0x100E

0x100F

0x1010

0x1011

0x1012

0x1013

0x1014

0x1015

0x1016

0x1017

...

p

Integ

erA

rrays

vs.P

oin

ters

– 2014-04 – pointers –

47/125

1int

a[3

]=

{10,

010,

0x1234

};

2int∗

p=

a;

3fo

r(int

i=

0;

i<

3;++p)

4∗p

=0x3421;...

0x1000

0x1001

0x10

0x1002

0x08

0x1003

0x10

0x1004

0x08

0x1005

0x1006

0x1007

0x1008

0x00

0x1009

0x27

0x100A

0x00

0x100B

0x27

0x100C

0x00

0x100D

0x27

0x100E

0x100F

0x1010

0x1011

0x1012

0x1013

0x1014

0x1015

0x1016

0x1017

...

p

Integ

erA

rrays

vs.P

oin

ters

– 2014-04 – pointers –

47/125

1int

a[3

]=

{10,

010,

0x1234

};

2int∗

p=

a;

3fo

r(int

i=

0;

i<

3;++p)

4∗p

=0x3421;...

0x1000

0x1001

0x10

0x1002

0x08

0x1003

0x10

0x1004

0x0A

0x1005

0x1006

0x1007

0x1008

0x34

0x1009

0x21

0x100A

0x00

0x100B

0x27

0x100C

0x00

0x100D

0x27

0x100E

0x100F

0x1010

0x1011

0x1012

0x1013

0x1014

0x1015

0x1016

0x1017

...

p

Integ

erA

rrays

vs.P

oin

ters

– 2014-04 – pointers –

47/125

1int

a[3

]=

{10,

010,

0x1234

};

2int∗

p=

a;

3fo

r(int

i=

0;

i<

3;++p)

4∗p

=0x3421;...

0x1000

0x1001

0x10

0x1002

0x08

0x1003

0x10

0x1004

0x0C

0x1005

0x1006

0x1007

0x1008

0x34

0x1009

0x21

0x100A

0x34

0x100B

0x21

0x100C

0x00

0x100D

0x27

0x100E

0x100F

0x1010

0x1011

0x1012

0x1013

0x1014

0x1015

0x1016

0x1017

...

p

Integ

erA

rrays

vs.P

oin

ters

– 2014-04 – pointers –

47/125

1int

a[3

]=

{10,

010,

0x1234

};

2int∗

p=

a;

3fo

r(int

i=

0;

i<

3;++p)

4∗p

=0x3421;...

0x1000

0x1001

0x10

0x1002

0x08

0x1003

0x10

0x1004

0x0E

0x1005

0x1006

0x1007

0x1008

0x34

0x1009

0x21

0x100A

0x34

0x100B

0x21

0x100C

0x34

0x100D

0x21

0x100E

0x100F

0x1010

0x1011

0x1012

0x1013

0x1014

0x1015

0x1016

0x1017

...

p

Page 7: swt.informatik.uni-freiburg.de · – 2014-04 – main – F o r m a l M e t hod s f o r C S e m i na r – Su mm e r S e m e s t e r 2014 D a n i e l D i e t s c h, S e rg i o F

Po

inters

to’vo

id’,

Po

inter

Arith

metic

– 2014-04 – pointers –

48/125

Poin

terto

’void

– 2014-04 – pointers –

49/125

1int[3

]a

={

10,

010,

0x1234

};

2int∗

p=

a;

3void

∗q

=a;

4fo

r(int

i=

0;

i<

3;++i)

{5

p++;

6q++;

7}

...0x1000

0x1001

0x10

0x1002

0x08

0x1003

0x10

0x1004

0x08

0x1005

0x10

0x1006

0x08

0x1007

0x1008

0x00

0x1009

0x0A

0x100A

0x00

0x100B

0x08

0x100C

0x12

0x100D

0x34

0x100E

0x100F

0x1010

0x1011

0x1012

0x1013

0x1014

0x1015

0x1016

0x1017

...

pq

Poin

terto

’void

– 2014-04 – pointers –

49/125

1int[3

]a

={

10,

010,

0x1234

};

2int∗

p=

a;

3void

∗q

=a;

4fo

r(int

i=

0;

i<

3;++i)

{5

p++;

6q++;

7}

...0x1000

0x1001

0x10

0x1002

0x08

0x1003

0x10

0x1004

0x0A

0x1005

0x10

0x1006

0x08

0x1007

0x1008

0x00

0x1009

0x0A

0x100A

0x00

0x100B

0x08

0x100C

0x12

0x100D

0x34

0x100E

0x100F

0x1010

0x1011

0x1012

0x1013

0x1014

0x1015

0x1016

0x1017

...

pq

Poin

terto

’void

– 2014-04 – pointers –

49/125

1int[3

]a

={

10,

010,

0x1234

};

2int∗

p=

a;

3void

∗q

=a;

4fo

r(int

i=

0;

i<

3;++i)

{5

p++;

6q++;

7}

...0x1000

0x1001

0x10

0x1002

0x08

0x1003

0x10

0x1004

0x0A

0x1005

0x10

0x1006

0x09

0x1007

0x1008

0x00

0x1009

0x0A

0x100A

0x00

0x100B

0x08

0x100C

0x12

0x100D

0x34

0x100E

0x100F

0x1010

0x1011

0x1012

0x1013

0x1014

0x1015

0x1016

0x1017

...

pq

Poin

terto

’void

– 2014-04 – pointers –

49/125

1int[3

]a

={

10,

010,

0x1234

};

2int∗

p=

a;

3void

∗q

=a;

4fo

r(int

i=

0;

i<

3;++i)

{5

p++;

6q++;

7}

...0x1000

0x1001

0x10

0x1002

0x08

0x1003

0x10

0x1004

0x0C

0x1005

0x10

0x1006

0x09

0x1007

0x1008

0x00

0x1009

0x0A

0x100A

0x00

0x100B

0x08

0x100C

0x12

0x100D

0x34

0x100E

0x100F

0x1010

0x1011

0x1012

0x1013

0x1014

0x1015

0x1016

0x1017

...

pq

Poin

terto

’void

– 2014-04 – pointers –

49/125

1int[3

]a

={

10,

010,

0x1234

};

2int∗

p=

a;

3void

∗q

=a;

4fo

r(int

i=

0;

i<

3;++i)

{5

p++;

6q++;

7}

...0x1000

0x1001

0x10

0x1002

0x08

0x1003

0x10

0x1004

0x0C

0x1005

0x10

0x1006

0x0A

0x1007

0x1008

0x00

0x1009

0x0A

0x100A

0x00

0x100B

0x08

0x100C

0x12

0x100D

0x34

0x100E

0x100F

0x1010

0x1011

0x1012

0x1013

0x1014

0x1015

0x1016

0x1017

...

pq

Page 8: swt.informatik.uni-freiburg.de · – 2014-04 – main – F o r m a l M e t hod s f o r C S e m i na r – Su mm e r S e m e s t e r 2014 D a n i e l D i e t s c h, S e rg i o F

Poin

terto

’void

– 2014-04 – pointers –

49/125

1int[3

]a

={

10,

010,

0x1234

};

2int∗

p=

a;

3void

∗q

=a;

4fo

r(int

i=

0;

i<

3;++i)

{5

p++;

6q++;

7}

...0x1000

0x1001

0x10

0x1002

0x08

0x1003

0x10

0x1004

0x0E

0x1005

0x10

0x1006

0x0A

0x1007

0x1008

0x00

0x1009

0x0A

0x100A

0x00

0x100B

0x08

0x100C

0x12

0x100D

0x34

0x100E

0x100F

0x1010

0x1011

0x1012

0x1013

0x1014

0x1015

0x1016

0x1017

...

pq

Poin

terto

’void

– 2014-04 – pointers –

49/125

1int[3

]a

={

10,

010,

0x1234

};

2int∗

p=

a;

3void

∗q

=a;

4fo

r(int

i=

0;

i<

3;++i)

{5

p++;

6q++;

7}

...0x1000

0x1001

0x10

0x1002

0x08

0x1003

0x10

0x1004

0x0E

0x1005

0x10

0x1006

0x0B

0x1007

0x1008

0x00

0x1009

0x0A

0x100A

0x00

0x100B

0x08

0x100C

0x12

0x100D

0x34

0x100E

0x100F

0x1010

0x1011

0x1012

0x1013

0x1014

0x1015

0x1016

0x1017

...

pq

Poin

ters:O

bserva

tion

– 2014-04 – pointers –

50/125

•A

variable

ofpointer

typejust

storesanaddress.

•Sodovariab

lesofarray

type.

•Pointers

canpointto

acertain

type,

orto

void.

•“A

pointer

tovoid

shall

have

thesam

erepresen

tationandalig

nment

requirem

ents

asapointer

toacharacter

type.”

(6.2.5.26)

•Theeff

ectof“increm

entin

g”apointer

dependsonthetyp

epointed

to.

1int

a[2

];2

int∗

p=

a;

3++p;

//

poin

ts

toa[1

]4

void

∗q

=a;

5q

+=

sizeof(int);

//

poin

ts

toa[1

]6

++q;

//

may

poin

tin

to

the

middle

Poin

terA

rithm

etic

– 2014-04 – pointers –

51/125

1int[3

]a

={

10,

010,

0x1234

},

i=

0;

23int∗

p=

a;

//

not&a

!45

if

(a[0

]==

∗p)

i++;

6if

(a[1

]==

∗(p+1))

i++;

7if

(a[2

]==

∗(p+2))

i++;

89if

(&(a[2

])−

p==

2)

i++;

10

11

void

∗q

=a;

12

13

if

(a[2

]==

∗((int∗)(q

+(2

∗sizeof(int)))))

i++;

14

15

//

i==

5

void

assuch

does

not

have

values,

we

need

toca

st’q’here...

note:

void*

canbecasted

toeveryth

ing

Po

inters

for

Ca

llB

yR

eference

– 2014-04 – pointers –

52/125

Call

By

Referen

cew

ithP

oin

ters

– 2014-04 – pointers –

53/125

1void

f(

int

x,

int

y)

{2

x++,y++;

3}

4void

g(

int∗

p,

int∗

q)

{5

(∗p)+

+,

(∗q)+

+;

6}

7int

a=

2,

b=

5;

8f(

a,

b);

9g(

&a,&b

);...

0x1000

0x1001

0x00

0x1002

0x02

0x1003

0x00

0x1004

0x05

0x1005

0x1006

0x1007

0x1008

0x1009

0x100A

0x..

0x100B

0x..

0x100C

0x..

0x100D

0x..

0x100E

0x100F

0x1010

0x1011

0x1012

0x1013

0x..

0x1014

0x..

0x1015

0x..

0x1016

0x..

0x1017

...

ab

x

y

pq

Page 9: swt.informatik.uni-freiburg.de · – 2014-04 – main – F o r m a l M e t hod s f o r C S e m i na r – Su mm e r S e m e s t e r 2014 D a n i e l D i e t s c h, S e rg i o F

Call

By

Referen

cew

ithP

oin

ters

– 2014-04 – pointers –

53/125

1void

f(

int

x,

int

y)

{2

x++,y++;

3}

4void

g(

int∗

p,

int∗

q)

{5

(∗p)+

+,

(∗q)+

+;

6}

7int

a=

2,

b=

5;

8f(

a,

b);

9g(

&a,&b

);...

0x1000

0x1001

0x00

0x1002

0x02

0x1003

0x00

0x1004

0x05

0x1005

0x1006

0x1007

0x1008

0x1009

0x100A

0x00

0x100B

0x02

0x100C

0x00

0x100D

0x05

0x100E

0x100F

0x1010

0x1011

0x1012

0x1013

0x..

0x1014

0x..

0x1015

0x..

0x1016

0x..

0x1017

...

Call

By

Referen

cew

ithP

oin

ters

– 2014-04 – pointers –

53/125

1void

f(

int

x,

int

y)

{2

x++,y++;

3}

4void

g(

int∗

p,

int∗

q)

{5

(∗p)+

+,

(∗q)+

+;

6}

7int

a=

2,

b=

5;

8f(

a,

b);

9g(

&a,&b

);...

0x1000

0x1001

0x00

0x1002

0x02

0x1003

0x00

0x1004

0x05

0x1005

0x1006

0x1007

0x1008

0x1009

0x100A

0x00

0x100B

0x03

0x100C

0x00

0x100D

0x06

0x100E

0x100F

0x1010

0x1011

0x1012

0x1013

0x..

0x1014

0x..

0x1015

0x..

0x1016

0x..

0x1017

...

Call

By

Referen

cew

ithP

oin

ters

– 2014-04 – pointers –

53/125

1void

f(

int

x,

int

y)

{2

x++,y++;

3}

4void

g(

int∗

p,

int∗

q)

{5

(∗p)+

+,

(∗q)+

+;

6}

7int

a=

2,

b=

5;

8f(

a,

b);

9g(

&a,&b

);...

0x1000

0x1001

0x00

0x1002

0x02

0x1003

0x00

0x1004

0x05

0x1005

0x1006

0x1007

0x1008

0x1009

0x100A

0x..

0x100B

0x..

0x100C

0x..

0x100D

0x..

0x100E

0x100F

0x1010

0x1011

0x1012

0x1013

0x..

0x1014

0x..

0x1015

0x..

0x1016

0x..

0x1017

...

Call

By

Referen

cew

ithP

oin

ters

– 2014-04 – pointers –

53/125

1void

f(

int

x,

int

y)

{2

x++,y++;

3}

4void

g(

int∗

p,

int∗

q)

{5

(∗p)+

+,

(∗q)+

+;

6}

7int

a=

2,

b=

5;

8f(

a,

b);

9g(

&a,&b

);...

0x1000

0x1001

0x00

0x1002

0x02

0x1003

0x00

0x1004

0x05

0x1005

0x1006

0x1007

0x1008

0x1009

0x100A

0x..

0x100B

0x..

0x100C

0x..

0x100D

0x..

0x100E

0x100F

0x1010

0x1011

0x1012

0x1013

0x10

0x1014

0x01

0x1015

0x10

0x1016

0x03

0x1017

...

Call

By

Referen

cew

ithP

oin

ters

– 2014-04 – pointers –

53/125

1void

f(

int

x,

int

y)

{2

x++,y++;

3}

4void

g(

int∗

p,

int∗

q)

{5

(∗p)+

+,

(∗q)+

+;

6}

7int

a=

2,

b=

5;

8f(

a,

b);

9g(

&a,&b

);...

0x1000

0x1001

0x00

0x1002

0x03

0x1003

0x00

0x1004

0x06

0x1005

0x1006

0x1007

0x1008

0x1009

0x100A

0x..

0x100B

0x..

0x100C

0x..

0x100D

0x..

0x100E

0x100F

0x1010

0x1011

0x1012

0x1013

0x10

0x1014

0x01

0x1015

0x10

0x1016

0x03

0x1017

...

Call

By

Referen

cew

ithP

oin

ters

– 2014-04 – pointers –

53/125

1void

f(

int

x,

int

y)

{2

x++,y++;

3}

4void

g(

int∗

p,

int∗

q)

{5

(∗p)+

+,

(∗q)+

+;

6}

7int

a=

2,

b=

5;

8f(

a,

b);

9g(

&a,&b

);...

0x1000

0x1001

0x00

0x1002

0x03

0x1003

0x00

0x1004

0x06

0x1005

0x1006

0x1007

0x1008

0x1009

0x100A

0x..

0x100B

0x..

0x100C

0x..

0x100D

0x..

0x100E

0x100F

0x1010

0x1011

0x1012

0x1013

0x..

0x1014

0x..

0x1015

0x..

0x1016

0x..

0x1017

...

Page 10: swt.informatik.uni-freiburg.de · – 2014-04 – main – F o r m a l M e t hod s f o r C S e m i na r – Su mm e r S e m e s t e r 2014 D a n i e l D i e t s c h, S e rg i o F

Dyn

am

icSto

rage

&Sto

rage

Dura

tion

– 2014-04 – storage –

54/125

Dyn

am

icS

tora

ge

Allo

catio

n

– 2014-04 – storage –

55/125

AL

inked

List

– 2014-04 – storage –

56/125

data:

’A’

next:

0x1004

data:

’B’

next:

0x1001

data:

’C’

next:

0x0000

1typedef

struct

Node

{2

char

data

;3

struct

Node∗

next;

4}

Node;

56Node

c=

{’C

’,

0};

7Node

b=

{’B

’,&c

};

8Node

a=

{’A

’,&b

};

...0x1000

0x1001

’C’

0x1002

0x00

0x1003

0x00

0x1004

’B’

0x1005

0x10

0x1006

0x01

0x1007

’A’

0x1008

0x10

0x1009

0x04

0x100A

0x100B

0x100C

0x100D

0x100E

0x100F

0x1010

0x1011

0x1012

0x1013

0x1014

0x1015

0x1016

0x1017

...

Nodec

b.data

b.next=

&c

Dyn

am

icSto

rage

Allo

catio

n

– 2014-04 – storage –

57/125

1typedef

struct

Node

{2

char

data

;struct

Node∗

next;

}Node;

34Node

∗head

=0,

∗hlp

;56

void

insert(

char

d)

{7

hlp

=(Node∗)mallo

c(

sizeof(Node)

);

8hlp−>data

=d;

9hlp−>next

=head;

10

head

=hlp

;11

}12

13

insert(

’C’

);

14

insert(

’B’

);

15

insert(

’A’

);

allocate

somespace

fora

Node,retu

rnits

address;

may

fail(“ou

tof

mem

ory”),mallo

c(3)yield

s0then

...0x1000

0x1001

0x1002

0x1003

0x1004

0x1005

0x1006

0x1007

0x1008

0x1009

0x100A

0x100B

0x100C

0x100D

0x100E

0x100F

0x1010

0x1011

0x1012

0x1013

0x1014

0x1015

0x1016

0x1017

...

Dyn

am

icSto

rage

Allo

catio

n

– 2014-04 – storage –

57/125

1typedef

struct

Node

{2

char

data

;struct

Node∗

next;

}Node;

34Node

∗head

=0,

∗hlp

;56

void

insert(

char

d)

{7

hlp

=(Node∗)mallo

c(

sizeof(Node)

);

8hlp−>data

=d;

9hlp−>next

=head;

10

head

=hlp

;11

}12

13

insert(

’C’

);

14

insert(

’B’

);

15

insert(

’A’

);

...0x1000

0x1001

0x00

0x1002

0x00

0x1003

0x1004

0x1005

0x1006

0x1007

0x1008

0x1009

0x100A

0x100B

0x100C

0x100D

0x100E

0x100F

0x1010

0x1011

0x1012

0x1013

0x1014

0x1015

0x1016

0x1017

...

head

Dyn

am

icSto

rage

Allo

catio

n

– 2014-04 – storage –

57/125

1typedef

struct

Node

{2

char

data

;struct

Node∗

next;

}Node;

34Node

∗head

=0,

∗hlp

;56

void

insert(

char

d)

{7

hlp

=(Node∗)mallo

c(

sizeof(Node)

);

8hlp−>data

=d;

9hlp−>next

=head;

10

head

=hlp

;11

}12

13

insert(

’C’

);

14

insert(

’B’

);

15

insert(

’A’

);

...0x1000

0x1001

0x00

0x1002

0x00

0x1003

0x1004

0x1005

0x1006

0x1007

0x1008

0x1009

0x100A

0x100B

0x100C

0x100D

0x100E

0x100F

0x1010

0x1011

0x1012

0x1013

0x1014

0x1015

0x1016

0x1017

...

Page 11: swt.informatik.uni-freiburg.de · – 2014-04 – main – F o r m a l M e t hod s f o r C S e m i na r – Su mm e r S e m e s t e r 2014 D a n i e l D i e t s c h, S e rg i o F

Dyn

am

icSto

rage

Allo

catio

n

– 2014-04 – storage –

57/125

1typedef

struct

Node

{2

char

data

;struct

Node∗

next;

}Node;

34Node

∗head

=0,

∗hlp

;56

void

insert(

char

d)

{7

hlp

=(Node∗)mallo

c(

sizeof(Node)

);

8hlp−>data

=d;

9hlp−>next

=head;

10

head

=hlp

;11

}12

13

insert(

’C’

);

14

insert(

’B’

);

15

insert(

’A’

);

...0x1000

0x1001

0x00

0x1002

0x00

0x1003

0x10

0x1004

0x13

0x1005

0x1006

0x1007

0x1008

0x1009

0x100A

0x100B

0x100C

0x100D

0x100E

0x100F

0x1010

0x1011

0x1012

0x1013

’..’0x1014

0x..

0x1015

0x..

0x1016

0x1017

...

hlp

Dyn

am

icSto

rage

Allo

catio

n

– 2014-04 – storage –

57/125

1typedef

struct

Node

{2

char

data

;struct

Node∗

next;

}Node;

34Node

∗head

=0,

∗hlp

;56

void

insert(

char

d)

{7

hlp

=(Node∗)mallo

c(

sizeof(Node)

);

8hlp−>data

=d;

9hlp−>next

=head;

10

head

=hlp

;11

}12

13

insert(

’C’

);

14

insert(

’B’

);

15

insert(

’A’

);

...0x1000

0x1001

0x00

0x1002

0x00

0x1003

0x10

0x1004

0x13

0x1005

0x1006

0x1007

0x1008

0x1009

0x100A

0x100B

0x100C

0x100D

0x100E

0x100F

0x1010

0x1011

0x1012

0x1013

’C’

0x1014

0x..

0x1015

0x..

0x1016

0x1017

...

Dyn

am

icSto

rage

Allo

catio

n

– 2014-04 – storage –

57/125

1typedef

struct

Node

{2

char

data

;struct

Node∗

next;

}Node;

34Node

∗head

=0,

∗hlp

;56

void

insert(

char

d)

{7

hlp

=(Node∗)mallo

c(

sizeof(Node)

);

8hlp−>data

=d;

9hlp−>next

=head;

10

head

=hlp

;11

}12

13

insert(

’C’

);

14

insert(

’B’

);

15

insert(

’A’

);

...0x1000

0x1001

0x00

0x1002

0x00

0x1003

0x10

0x1004

0x13

0x1005

0x1006

0x1007

0x1008

0x1009

0x100A

0x100B

0x100C

0x100D

0x100E

0x100F

0x1010

0x1011

0x1012

0x1013

’C’

0x1014

0x00

0x1015

0x00

0x1016

0x1017

...

Dyn

am

icSto

rage

Allo

catio

n

– 2014-04 – storage –

57/125

1typedef

struct

Node

{2

char

data

;struct

Node∗

next;

}Node;

34Node

∗head

=0,

∗hlp

;56

void

insert(

char

d)

{7

hlp

=(Node∗)mallo

c(

sizeof(Node)

);

8hlp−>data

=d;

9hlp−>next

=head;

10

head

=hlp

;11

}12

13

insert(

’C’

);

14

insert(

’B’

);

15

insert(

’A’

);

...0x1000

0x1001

0x10

0x1002

0x13

0x1003

0x10

0x1004

0x13

0x1005

0x1006

0x1007

0x1008

0x1009

0x100A

0x100B

0x100C

0x100D

0x100E

0x100F

0x1010

0x1011

0x1012

0x1013

’C’

0x1014

0x00

0x1015

0x00

0x1016

0x1017

...

Dyn

am

icSto

rage

Allo

catio

n

– 2014-04 – storage –

57/125

data:

’C’

next:

0x0000

1typedef

struct

Node

{2

char

data

;struct

Node∗

next;

}Node;

34Node

∗head

=0,

∗hlp

;56

void

insert(

char

d)

{7

hlp

=(Node∗)mallo

c(

sizeof(Node)

);

8hlp−>data

=d;

9hlp−>next

=head;

10

head

=hlp

;11

}12

13

insert(

’C’

);

14

insert(

’B’

);

15

insert(

’A’

);

...0x1000

0x1001

0x10

0x1002

0x13

0x1003

0x10

0x1004

0x13

0x1005

0x1006

0x1007

0x1008

0x1009

0x100A

0x100B

0x100C

0x100D

0x100E

0x100F

0x1010

0x1011

0x1012

0x1013

’C’

0x1014

0x00

0x1015

0x00

0x1016

0x1017

...

Dyn

am

icSto

rage

Allo

catio

n

– 2014-04 – storage –

57/125

data:

’C’

next:

0x0000

1typedef

struct

Node

{2

char

data

;struct

Node∗

next;

}Node;

34Node

∗head

=0,

∗hlp

;56

void

insert(

char

d)

{7

hlp

=(Node∗)mallo

c(

sizeof(Node)

);

8hlp−>data

=d;

9hlp−>next

=head;

10

head

=hlp

;11

}12

13

insert(

’C’

);

14

insert(

’B’

);

15

insert(

’A’

);

...0x1000

0x1001

0x10

0x1002

0x13

0x1003

0x10

0x1004

0x13

0x1005

0x1006

0x1007

0x1008

0x1009

0x100A

0x100B

0x100C

0x100D

0x100E

0x100F

0x1010

0x1011

0x1012

0x1013

’C’

0x1014

0x00

0x1015

0x00

0x1016

0x1017

...

Page 12: swt.informatik.uni-freiburg.de · – 2014-04 – main – F o r m a l M e t hod s f o r C S e m i na r – Su mm e r S e m e s t e r 2014 D a n i e l D i e t s c h, S e rg i o F

Dyn

am

icSto

rage

Allo

catio

n

– 2014-04 – storage –

57/125

data:

’C’

next:

0x0000

1typedef

struct

Node

{2

char

data

;struct

Node∗

next;

}Node;

34Node

∗head

=0,

∗hlp

;56

void

insert(

char

d)

{7

hlp

=(Node∗)mallo

c(

sizeof(Node)

);

8hlp−>data

=d;

9hlp−>next

=head;

10

head

=hlp

;11

}12

13

insert(

’C’

);

14

insert(

’B’

);

15

insert(

’A’

);

...0x1000

0x1001

0x10

0x1002

0x13

0x1003

0x10

0x1004

0x08

0x1005

0x1006

0x1007

0x1008

’..’0x1009

0x..

0x100A

0x..

0x100B

0x100C

0x100D

0x100E

0x100F

0x1010

0x1011

0x1012

0x1013

’C’

0x1014

0x00

0x1015

0x00

0x1016

0x1017

...

Dyn

am

icSto

rage

Allo

catio

n

– 2014-04 – storage –

57/125

data:

’C’

next:

0x0000

1typedef

struct

Node

{2

char

data

;struct

Node∗

next;

}Node;

34Node

∗head

=0,

∗hlp

;56

void

insert(

char

d)

{7

hlp

=(Node∗)mallo

c(

sizeof(Node)

);

8hlp−>data

=d;

9hlp−>next

=head;

10

head

=hlp

;11

}12

13

insert(

’C’

);

14

insert(

’B’

);

15

insert(

’A’

);

...0x1000

0x1001

0x10

0x1002

0x13

0x1003

0x10

0x1004

0x08

0x1005

0x1006

0x1007

0x1008

’B’

0x1009

0x..

0x100A

0x..

0x100B

0x100C

0x100D

0x100E

0x100F

0x1010

0x1011

0x1012

0x1013

’C’

0x1014

0x00

0x1015

0x00

0x1016

0x1017

...

Dyn

am

icSto

rage

Allo

catio

n

– 2014-04 – storage –

57/125

data:

’C’

next:

0x0000

1typedef

struct

Node

{2

char

data

;struct

Node∗

next;

}Node;

34Node

∗head

=0,

∗hlp

;56

void

insert(

char

d)

{7

hlp

=(Node∗)mallo

c(

sizeof(Node)

);

8hlp−>data

=d;

9hlp−>next

=head;

10

head

=hlp

;11

}12

13

insert(

’C’

);

14

insert(

’B’

);

15

insert(

’A’

);

...0x1000

0x1001

0x10

0x1002

0x13

0x1003

0x10

0x1004

0x08

0x1005

0x1006

0x1007

0x1008

’B’

0x1009

0x10

0x100A

0x13

0x100B

0x100C

0x100D

0x100E

0x100F

0x1010

0x1011

0x1012

0x1013

’C’

0x1014

0x00

0x1015

0x00

0x1016

0x1017

...

Dyn

am

icSto

rage

Allo

catio

n

– 2014-04 – storage –

57/125

data:

’C’

next:

0x0000

1typedef

struct

Node

{2

char

data

;struct

Node∗

next;

}Node;

34Node

∗head

=0,

∗hlp

;56

void

insert(

char

d)

{7

hlp

=(Node∗)mallo

c(

sizeof(Node)

);

8hlp−>data

=d;

9hlp−>next

=head;

10

head

=hlp

;11

}12

13

insert(

’C’

);

14

insert(

’B’

);

15

insert(

’A’

);

...0x1000

0x1001

0x10

0x1002

0x08

0x1003

0x10

0x1004

0x08

0x1005

0x1006

0x1007

0x1008

’B’

0x1009

0x10

0x100A

0x13

0x100B

0x100C

0x100D

0x100E

0x100F

0x1010

0x1011

0x1012

0x1013

’C’

0x1014

0x00

0x1015

0x00

0x1016

0x1017

...

Dyn

am

icSto

rage

Allo

catio

n

– 2014-04 – storage –

57/125

data:

’B’

next:

0x1013

data:

’C’

next:

0x0000

1typedef

struct

Node

{2

char

data

;struct

Node∗

next;

}Node;

34Node

∗head

=0,

∗hlp

;56

void

insert(

char

d)

{7

hlp

=(Node∗)mallo

c(

sizeof(Node)

);

8hlp−>data

=d;

9hlp−>next

=head;

10

head

=hlp

;11

}12

13

insert(

’C’

);

14

insert(

’B’

);

15

insert(

’A’

);

...0x1000

0x1001

0x10

0x1002

0x08

0x1003

0x10

0x1004

0x08

0x1005

0x1006

0x1007

0x1008

’B’

0x1009

0x10

0x100A

0x13

0x100B

0x100C

0x100D

0x100E

0x100F

0x1010

0x1011

0x1012

0x1013

’C’

0x1014

0x00

0x1015

0x00

0x1016

0x1017

...

Dyn

am

icSto

rage

Allo

catio

n

– 2014-04 – storage –

57/125

data:

’B’

next:

0x1013

data:

’C’

next:

0x0000

1typedef

struct

Node

{2

char

data

;struct

Node∗

next;

}Node;

34Node

∗head

=0,

∗hlp

;56

void

insert(

char

d)

{7

hlp

=(Node∗)mallo

c(

sizeof(Node)

);

8hlp−>data

=d;

9hlp−>next

=head;

10

head

=hlp

;11

}12

13

insert(

’C’

);

14

insert(

’B’

);

15

insert(

’A’

);

...0x1000

0x1001

0x10

0x1002

0x08

0x1003

0x10

0x1004

0x08

0x1005

0x1006

0x1007

0x1008

’B’

0x1009

0x10

0x100A

0x13

0x100B

0x100C

0x100D

0x100E

0x100F

0x1010

0x1011

0x1012

0x1013

’C’

0x1014

0x00

0x1015

0x00

0x1016

0x1017

...

Page 13: swt.informatik.uni-freiburg.de · – 2014-04 – main – F o r m a l M e t hod s f o r C S e m i na r – Su mm e r S e m e s t e r 2014 D a n i e l D i e t s c h, S e rg i o F

Dyn

am

icSto

rage

Allo

catio

n

– 2014-04 – storage –

57/125

data:

’B’

next:

0x1013

data:

’C’

next:

0x0000

1typedef

struct

Node

{2

char

data

;struct

Node∗

next;

}Node;

34Node

∗head

=0,

∗hlp

;56

void

insert(

char

d)

{7

hlp

=(Node∗)mallo

c(

sizeof(Node)

);

8hlp−>data

=d;

9hlp−>next

=head;

10

head

=hlp

;11

}12

13

insert(

’C’

);

14

insert(

’B’

);

15

insert(

’A’

);

...0x1000

0x1001

0x10

0x1002

0x08

0x1003

0x10

0x1004

0x0D

0x1005

0x1006

0x1007

0x1008

’B’

0x1009

0x10

0x100A

0x13

0x100B

0x100C

0x100D

’..’0x100E

0x..

0x100F

0x..

0x1010

0x1011

0x1012

0x1013

’C’

0x1014

0x00

0x1015

0x00

0x1016

0x1017

...

Dyn

am

icSto

rage

Allo

catio

n

– 2014-04 – storage –

57/125

data:

’B’

next:

0x1013

data:

’C’

next:

0x0000

1typedef

struct

Node

{2

char

data

;struct

Node∗

next;

}Node;

34Node

∗head

=0,

∗hlp

;56

void

insert(

char

d)

{7

hlp

=(Node∗)mallo

c(

sizeof(Node)

);

8hlp−>data

=d;

9hlp−>next

=head;

10

head

=hlp

;11

}12

13

insert(

’C’

);

14

insert(

’B’

);

15

insert(

’A’

);

...0x1000

0x1001

0x10

0x1002

0x08

0x1003

0x10

0x1004

0x0D

0x1005

0x1006

0x1007

0x1008

’B’

0x1009

0x10

0x100A

0x13

0x100B

0x100C

0x100D

’A’

0x100E

0x..

0x100F

0x..

0x1010

0x1011

0x1012

0x1013

’C’

0x1014

0x00

0x1015

0x00

0x1016

0x1017

...

Dyn

am

icSto

rage

Allo

catio

n

– 2014-04 – storage –

57/125

data:

’B’

next:

0x1013

data:

’C’

next:

0x0000

1typedef

struct

Node

{2

char

data

;struct

Node∗

next;

}Node;

34Node

∗head

=0,

∗hlp

;56

void

insert(

char

d)

{7

hlp

=(Node∗)mallo

c(

sizeof(Node)

);

8hlp−>data

=d;

9hlp−>next

=head;

10

head

=hlp

;11

}12

13

insert(

’C’

);

14

insert(

’B’

);

15

insert(

’A’

);

...0x1000

0x1001

0x10

0x1002

0x08

0x1003

0x10

0x1004

0x0D

0x1005

0x1006

0x1007

0x1008

’B’

0x1009

0x10

0x100A

0x13

0x100B

0x100C

0x100D

’A’

0x100E

0x10

0x100F

0x08

0x1010

0x1011

0x1012

0x1013

’C’

0x1014

0x00

0x1015

0x00

0x1016

0x1017

...

Dyn

am

icSto

rage

Allo

catio

n

– 2014-04 – storage –

57/125

data:

’B’

next:

0x1013

data:

’C’

next:

0x0000

1typedef

struct

Node

{2

char

data

;struct

Node∗

next;

}Node;

34Node

∗head

=0,

∗hlp

;56

void

insert(

char

d)

{7

hlp

=(Node∗)mallo

c(

sizeof(Node)

);

8hlp−>data

=d;

9hlp−>next

=head;

10

head

=hlp

;11

}12

13

insert(

’C’

);

14

insert(

’B’

);

15

insert(

’A’

);

...0x1000

0x1001

0x10

0x1002

0x0D

0x1003

0x10

0x1004

0x0D

0x1005

0x1006

0x1007

0x1008

’B’

0x1009

0x10

0x100A

0x13

0x100B

0x100C

0x100D

’A’

0x100E

0x10

0x100F

0x08

0x1010

0x1011

0x1012

0x1013

’C’

0x1014

0x00

0x1015

0x00

0x1016

0x1017

...

Dyn

am

icSto

rage

Allo

catio

n

– 2014-04 – storage –

57/125

data:

’A’

next:

0x1008

data:

’B’

next:

0x1013

data:

’C’

next:

0x0000

1typedef

struct

Node

{2

char

data

;struct

Node∗

next;

}Node;

34Node

∗head

=0,

∗hlp

;56

void

insert(

char

d)

{7

hlp

=(Node∗)mallo

c(

sizeof(Node)

);

8hlp−>data

=d;

9hlp−>next

=head;

10

head

=hlp

;11

}12

13

insert(

’C’

);

14

insert(

’B’

);

15

insert(

’A’

);

...0x1000

0x1001

0x10

0x1002

0x0D

0x1003

0x10

0x1004

0x0D

0x1005

0x1006

0x1007

0x1008

’B’

0x1009

0x10

0x100A

0x13

0x100B

0x100C

0x100D

’A’

0x100E

0x10

0x100F

0x08

0x1010

0x1011

0x1012

0x1013

’C’

0x1014

0x00

0x1015

0x00

0x1016

0x1017

...

Dyn

am

icSto

rage

Managem

ent

– 2014-04 – storage –

58/125

Dynam

icStorag

eAllo

cation:

•void*mallo

c(size

tsize

);

“[...]

allocates

sizebytes

andretu

rnsapointer

totheallo

catedmem

ory.Thememory

isnotinitia

lized.[...]”

•“Onerror,

[thisfunctio

n]retu

rnsNULL.”

•void

free(void*ptr

)

“[...]

freesthemem

oryspace

pointed

toby

ptr,

which

must

have

been

returned

byaprevio

uscall

tomallo

c(),[...].”

•“Otherw

ise,or

iffre

e(ptr)

has

alreadybeen

calledbefore,

undefinedbehavioroccu

rs.”

•“Ifptr

isNULL,nooperatio

nisperform

ed.”

•Nogarb

ageco

llectio

n!

Managem

entofdynam

icstorag

eisresp

onsib

ilityoftheprogrammer.

Unaccessib

le,notfree’d

mem

oryiscalled

memory

leak.

Page 14: swt.informatik.uni-freiburg.de · – 2014-04 – main – F o r m a l M e t hod s f o r C S e m i na r – Su mm e r S e m e s t e r 2014 D a n i e l D i e t s c h, S e rg i o F

Dyn

am

icSto

rage

Managem

ent

Exa

mple

– 2014-04 – storage –

59/125

data:

’A’

next:

0x1008

data:

’B’

next:

0x1013

data:

’C’

next:

0x0000

1void

remove()

{2

if

(hlp

=head)

{3

head

=hlp−>next;

4free(hlp

);

5}

6}

7in

sert(

’C’

);

insert(

’B’

);

insert(

’A’

);

8remove();

9in

sert(

’X’

);

...0x1000

0x1001

0x10

0x1002

0x0D

0x1003

0x10

0x1004

0x0D

0x1005

0x1006

0x1007

0x1008

’B’

0x1009

0x10

0x100A

0x13

0x100B

0x100C

0x100D

’A’

0x100E

0x10

0x100F

0x08

0x1010

0x1011

0x1012

0x1013

’C’

0x1014

0x00

0x1015

0x00

0x1016

0x1017

...

Dyn

am

icSto

rage

Managem

ent

Exa

mple

– 2014-04 – storage –

59/125

data:

’A’

next:

0x1008

data:

’B’

next:

0x1013

data:

’C’

next:

0x0000

1void

remove()

{2

if

(hlp

=head)

{3

head

=hlp−>next;

4free(hlp

);

5}

6}

7in

sert(

’C’

);

insert(

’B’

);

insert(

’A’

);

8remove();

9in

sert(

’X’

);

...0x1000

0x1001

0x10

0x1002

0x0D

0x1003

0x10

0x1004

0x0D

0x1005

0x1006

0x1007

0x1008

’B’

0x1009

0x10

0x100A

0x13

0x100B

0x100C

0x100D

’A’

0x100E

0x10

0x100F

0x08

0x1010

0x1011

0x1012

0x1013

’C’

0x1014

0x00

0x1015

0x00

0x1016

0x1017

...

Dyn

am

icSto

rage

Managem

ent

Exa

mple

– 2014-04 – storage –

59/125

data:

’B’

next:

0x1013

data:

’C’

next:

0x0000

1void

remove()

{2

if

(hlp

=head)

{3

head

=hlp−>next;

4free(hlp

);

5}

6}

7in

sert(

’C’

);

insert(

’B’

);

insert(

’A’

);

8remove();

9in

sert(

’X’

);

...0x1000

0x1001

0x10

0x1002

0x08

0x1003

0x10

0x1004

0x0D

0x1005

0x1006

0x1007

0x1008

’B’

0x1009

0x10

0x100A

0x13

0x100B

0x100C

0x100D

’A’

0x100E

0x10

0x100F

0x08

0x1010

0x1011

0x1012

0x1013

’C’

0x1014

0x00

0x1015

0x00

0x1016

0x1017

...

Dyn

am

icSto

rage

Managem

ent

Exa

mple

– 2014-04 – storage –

59/125

data:

’B’

next:

0x1013

data:

’C’

next:

0x0000

1void

remove()

{2

if

(hlp

=head)

{3

head

=hlp−>next;

4free(hlp

);

5}

6}

7in

sert(

’C’

);

insert(

’B’

);

insert(

’A’

);

8remove();

9in

sert(

’X’

);

...0x1000

0x1001

0x10

0x1002

0x08

0x1003

0x10

0x1004

0x0D

0x1005

0x1006

0x1007

0x1008

’B’

0x1009

0x10

0x100A

0x13

0x100B

0x100C

0x100D

’A’

0x100E

0x10

0x100F

0x08

0x1010

0x1011

0x1012

0x1013

’C’

0x1014

0x00

0x1015

0x00

0x1016

0x1017

...

Dyn

am

icSto

rage

Managem

ent

Exa

mple

– 2014-04 – storage –

59/125

data:

’B’

next:

0x1013

data:

’C’

next:

0x0000

1void

remove()

{2

if

(hlp

=head)

{3

head

=hlp−>next;

4free(hlp

);

5}

6}

7in

sert(

’C’

);

insert(

’B’

);

insert(

’A’

);

8remove();

9in

sert(

’X’

);

...0x1000

0x1001

0x10

0x1002

0x08

0x1003

0x10

0x1004

0x0D

0x1005

0x1006

0x1007

0x1008

’B’

0x1009

0x10

0x100A

0x13

0x100B

0x100C

0x100D

’A’

0x100E

0x10

0x100F

0x08

0x1010

0x1011

0x1012

0x1013

’C’

0x1014

0x00

0x1015

0x00

0x1016

0x1017

...

Dyn

am

icSto

rage

Managem

ent

Exa

mple

– 2014-04 – storage –

59/125

data:

’X’

next:

0x1008

data:

’B’

next:

0x1013

data:

’C’

next:

0x0000

1void

remove()

{2

if

(hlp

=head)

{3

head

=hlp−>next;

4free(hlp

);

5}

6}

7in

sert(

’C’

);

insert(

’B’

);

insert(

’A’

);

8remove();

9in

sert(

’X’

);

...0x1000

0x1001

0x10

0x1002

0x0D

0x1003

0x10

0x1004

0x0D

0x1005

0x1006

0x1007

0x1008

’B’

0x1009

0x10

0x100A

0x13

0x100B

0x100C

0x100D

’X’

0x100E

0x10

0x100F

0x08

0x1010

0x1011

0x1012

0x1013

’C’

0x1014

0x00

0x1015

0x00

0x1016

0x1017

...

Page 15: swt.informatik.uni-freiburg.de · – 2014-04 – main – F o r m a l M e t hod s f o r C S e m i na r – Su mm e r S e m e s t e r 2014 D a n i e l D i e t s c h, S e rg i o F

Dyn

am

icSto

rage

Managem

ent

Exa

mple

– 2014-04 – storage –

59/125

data:

’B’

next:

0x1013

data:

’C’

next:

0x0000

1void

remove()

{2

if

(hlp

=head)

{3

head

=hlp−>next;

4free(hlp

);

5}

6}

7in

sert(

’C’

);

insert(

’B’

);

insert(

’A’

);

8remove();

9in

sert(

’X’

);

...0x1000

0x1001

0x10

0x1002

0x08

0x1003

0x10

0x1004

0x0D

0x1005

0x1006

0x1007

0x1008

’B’

0x1009

0x10

0x100A

0x13

0x100B

0x100C

0x100D

’A’

0x100E

0x10

0x100F

0x08

0x1010

’X’

0x1011

0x10

0x1012

0x08

0x1013

’C’

0x1014

0x00

0x1015

0x00

0x1016

0x1017

...

Dyn

am

icL

inked

List

Iteratio

n

– 2014-04 – storage –

60/125

1Node∗

find(

char

d)

{2

hlp

=head;

3while

(hlp

){

4if

(hlp−>data

==

d)

5break;

6hlp

=hlp−>next;

7}

8retu

rn

hlp

;9

}10

insert(

’C’

);

insert(

’B’

);

insert(

’A’

);

11

find(

’B’

);

//

yie

lds

0x1008

12

find(

’O’

);

//

yie

lds

0x0000,

aka.

NULL

...0x1000

0x1001

0x10

0x1002

0x0D

0x1003

0x10

0x1004

0x0D

0x1005

0x1006

0x1007

0x1008

’B’

0x1009

0x10

0x100A

0x13

0x100B

0x100C

0x100D

’A’

0x100E

0x10

0x100F

0x08

0x1010

0x1011

0x1012

0x1013

’C’

0x1014

0x00

0x1015

0x00

0x1016

0x1017

...

Poin

tersto

Stru

ct/Unio

n—

‘.’vs.

‘->

– 2014-04 – storage –

61/125

1typedef

struct

{2

int

x;

3int

y;

4}

coord

inate;

56coord

inate

pos=

{13,

27

};

78coord

inate∗

p=

&pos;

9

10

int

tmp;

11

12

tmp

=(∗

p).x;

13

(∗p).x

=(∗

p).y;

14

(∗p).y

=tm

p;

15

16

tmp

=p−

>x;

17

p−>x

=p−

>y;

18

p−>y

=tm

p;

Sto

rage

Du

ratio

no

fO

bjects

– 2014-04 – storage –

62/125

Sto

rage

Dura

tion

of

Objects

(6.2

.4)

– 2014-04 – storage –

63/125

•“sta

tic”–e.g

.variab

lesin

program

scope:

•live

from

program

startto

end

•ifnotexp

licitlyinitialized

,set

to0(6.7.8)

•“automatic”

–non-static

variables

inlocal

scope:

•live

from

block

entry

toexit

•notautomatically

initialised

:“initial

value[...]

isindeterm

inate”

•“allo

cated”–dynam

icobjects:

•live

from

mallo

cto

free

•notautomatically

initialised

“Ifan

object

isreferred

tooutsid

eofits

lifetime,

thebehavioris

undefined.

Thevalu

eofapointer

beco

mes

indeterm

inate

when

theobject

itpoints

toreach

estheendofits

lifetime.”

Exa

mple:

Anato

my

of

aL

inux

Pro

gra

min

Mem

ory

– 2014-04 – storage –

64/125

c©Gusta

voDuarte

2009,used

byperm

ission.

http://duartes.org/gustavo/blog/post/anatomy-of-a-program-in-memory

local

variables

livehere

mallo

c()/free()

work

here

uninitialised

global

variables,

setto

0,here

initialised

global

variables

here

program

code

liveshere

Page 16: swt.informatik.uni-freiburg.de · – 2014-04 – main – F o r m a l M e t hod s f o r C S e m i na r – Su mm e r S e m e s t e r 2014 D a n i e l D i e t s c h, S e rg i o F

Sto

rage

Dura

tion

“A

uto

matic”

(Sim

plifi

ed)

– 2014-04 – storage –

65/125

1void

h()

{int

y;

y++;}

2void

g()

{int

x=

5;

x++;}

3int∗

f()

{int

c=

3;

g();

h();

h();

retu

rn

&c;

}45

int

a=

27,

b,

∗p;

6p

=f();

7b

=∗p;

...0x1000

0x1001

0x1002

0x1003

0x1004

0x1005

0x1006

0x1007

0x1008

0x1009

0x100A

0x100B

0x100C

0x100D

0x100E

0x100F

0x1010

0x1011

0x1012

0x00

0x1013

0x00

0x1014

0x00

0x1015

0x00

0x1016

0x00

0x1017

0x1B

...

pb

a

stackpointer

–stack

endsat

0x1012in

thiscase;

stackgrow

sdow

nward

s(to

smaller

addr.)

Sto

rage

Dura

tion

“A

uto

matic”

(Sim

plifi

ed)

– 2014-04 – storage –

65/125

1void

h()

{int

y;

y++;}

2void

g()

{int

x=

5;

x++;}

3int∗

f()

{int

c=

3;

g();

h();

h();

retu

rn

&c;

}45

int

a=

27,

b,

∗p;

6p

=f();

7b

=∗p;

...0x1000

0x1001

0x1002

0x1003

0x1004

0x1005

0x1006

0x1007

0x1008

0x1009

0x100A

0x100B

0x100C

0x100D

0x100E

0x100F

0x1010

0x1011

0x1012

0x00

0x1013

0x00

0x1014

0x00

0x1015

0x00

0x1016

0x00

0x1017

0x1B

...

pb

a

Sto

rage

Dura

tion

“A

uto

matic”

(Sim

plifi

ed)

– 2014-04 – storage –

65/125

1void

h()

{int

y;

y++;}

2void

g()

{int

x=

5;

x++;}

3int∗

f()

{int

c=

3;

g();

h();

h();

retu

rn

&c;

}45

int

a=

27,

b,

∗p;

6p

=f();

7b

=∗p;

...0x1000

0x1001

0x1002

0x1003

0x1004

0x1005

0x1006

0x1007

0x1008

0x1009

0x100A

0x100B

0x100C

0x100D

0x100E

0x100F

0x1010

0x00

0x1011

0x03

0x1012

0x00

0x1013

0x00

0x1014

0x00

0x1015

0x00

0x1016

0x00

0x1017

0x1B

...

cp

ba

Sto

rage

Dura

tion

“A

uto

matic”

(Sim

plifi

ed)

– 2014-04 – storage –

65/125

1void

h()

{int

y;

y++;}

2void

g()

{int

x=

5;

x++;}

3int∗

f()

{int

c=

3;

g();

h();

h();

retu

rn

&c;

}45

int

a=

27,

b,

∗p;

6p

=f();

7b

=∗p;

...0x1000

0x1001

0x1002

0x1003

0x1004

0x1005

0x1006

0x1007

0x1008

0x1009

0x100A

0x100B

0x100C

0x100D

0x100E

0x00

0x100F

0x05

0x1010

0x00

0x1011

0x03

0x1012

0x00

0x1013

0x00

0x1014

0x00

0x1015

0x00

0x1016

0x00

0x1017

0x1B

...x

pb

a

Sto

rage

Dura

tion

“A

uto

matic”

(Sim

plifi

ed)

– 2014-04 – storage –

65/125

1void

h()

{int

y;

y++;}

2void

g()

{int

x=

5;

x++;}

3int∗

f()

{int

c=

3;

g();

h();

h();

retu

rn

&c;

}45

int

a=

27,

b,

∗p;

6p

=f();

7b

=∗p;

...0x1000

0x1001

0x1002

0x1003

0x1004

0x1005

0x1006

0x1007

0x1008

0x1009

0x100A

0x100B

0x100C

0x100D

0x100E

0x00

0x100F

0x06

0x1010

0x00

0x1011

0x03

0x1012

0x00

0x1013

0x00

0x1014

0x00

0x1015

0x00

0x1016

0x00

0x1017

0x1B

...

pb

a

Sto

rage

Dura

tion

“A

uto

matic”

(Sim

plifi

ed)

– 2014-04 – storage –

65/125

1void

h()

{int

y;

y++;}

2void

g()

{int

x=

5;

x++;}

3int∗

f()

{int

c=

3;

g();

h();

h();

retu

rn

&c;

}45

int

a=

27,

b,

∗p;

6p

=f();

7b

=∗p;

...0x1000

0x1001

0x1002

0x1003

0x1004

0x1005

0x1006

0x1007

0x1008

0x1009

0x100A

0x100B

0x100C

0x100D

0x100E

0x00

0x100F

0x06

0x1010

0x00

0x1011

0x03

0x1012

0x00

0x1013

0x00

0x1014

0x00

0x1015

0x00

0x1016

0x00

0x1017

0x1B

...

xnolonger

alive!

pb

a

Page 17: swt.informatik.uni-freiburg.de · – 2014-04 – main – F o r m a l M e t hod s f o r C S e m i na r – Su mm e r S e m e s t e r 2014 D a n i e l D i e t s c h, S e rg i o F

Sto

rage

Dura

tion

“A

uto

matic”

(Sim

plifi

ed)

– 2014-04 – storage –

65/125

1void

h()

{int

y;

y++;}

2void

g()

{int

x=

5;

x++;}

3int∗

f()

{int

c=

3;

g();

h();

h();

retu

rn

&c;

}45

int

a=

27,

b,

∗p;

6p

=f();

7b

=∗p;

...0x1000

0x1001

0x1002

0x1003

0x1004

0x1005

0x1006

0x1007

0x1008

0x1009

0x100A

0x100B

0x100C

0x100D

0x100E

0x00

0x100F

0x06

0x1010

0x00

0x1011

0x03

0x1012

0x00

0x1013

0x00

0x1014

0x00

0x1015

0x00

0x1016

0x00

0x1017

0x1B

...

(now

)y–notexp

licitlyinitialised

,thusinitial

valueisindeterm

inate

pb

a

Sto

rage

Dura

tion

“A

uto

matic”

(Sim

plifi

ed)

– 2014-04 – storage –

65/125

1void

h()

{int

y;

y++;}

2void

g()

{int

x=

5;

x++;}

3int∗

f()

{int

c=

3;

g();

h();

h();

retu

rn

&c;

}45

int

a=

27,

b,

∗p;

6p

=f();

7b

=∗p;

...0x1000

0x1001

0x1002

0x1003

0x1004

0x1005

0x1006

0x1007

0x1008

0x1009

0x100A

0x100B

0x100C

0x100D

0x100E

0x00

0x100F

0x07

0x1010

0x00

0x1011

0x03

0x1012

0x00

0x1013

0x00

0x1014

0x00

0x1015

0x00

0x1016

0x00

0x1017

0x1B

...

pb

a

Sto

rage

Dura

tion

“A

uto

matic”

(Sim

plifi

ed)

– 2014-04 – storage –

65/125

1void

h()

{int

y;

y++;}

2void

g()

{int

x=

5;

x++;}

3int∗

f()

{int

c=

3;

g();

h();

h();

retu

rn

&c;

}45

int

a=

27,

b,

∗p;

6p

=f();

7b

=∗p;

...0x1000

0x1001

0x1002

0x1003

0x1004

0x1005

0x1006

0x1007

0x1008

0x1009

0x100A

0x100B

0x100C

0x100D

0x100E

0x00

0x100F

0x07

0x1010

0x00

0x1011

0x03

0x1012

0x00

0x1013

0x00

0x1014

0x00

0x1015

0x00

0x1016

0x00

0x1017

0x1B

...

ynolonger

alive!

pb

a

Sto

rage

Dura

tion

“A

uto

matic”

(Sim

plifi

ed)

– 2014-04 – storage –

65/125

1void

h()

{int

y;

y++;}

2void

g()

{int

x=

5;

x++;}

3int∗

f()

{int

c=

3;

g();

h();

h();

retu

rn

&c;

}45

int

a=

27,

b,

∗p;

6p

=f();

7b

=∗p;

...0x1000

0x1001

0x1002

0x1003

0x1004

0x1005

0x1006

0x1007

0x1008

0x1009

0x100A

0x100B

0x100C

0x100D

0x100E

0x00

0x100F

0x07

0x1010

0x00

0x1011

0x03

0x1012

0x00

0x1013

0x00

0x1014

0x00

0x1015

0x00

0x1016

0x00

0x1017

0x1B

...

pb

a

Sto

rage

Dura

tion

“A

uto

matic”

(Sim

plifi

ed)

– 2014-04 – storage –

65/125

1void

h()

{int

y;

y++;}

2void

g()

{int

x=

5;

x++;}

3int∗

f()

{int

c=

3;

g();

h();

h();

retu

rn

&c;

}45

int

a=

27,

b,

∗p;

6p

=f();

7b

=∗p;

...0x1000

0x1001

0x1002

0x1003

0x1004

0x1005

0x1006

0x1007

0x1008

0x1009

0x100A

0x100B

0x100C

0x100D

0x100E

0x00

0x100F

0x08

0x1010

0x00

0x1011

0x03

0x1012

0x00

0x1013

0x00

0x1014

0x00

0x1015

0x00

0x1016

0x00

0x1017

0x1B

...

pb

a

Sto

rage

Dura

tion

“A

uto

matic”

(Sim

plifi

ed)

– 2014-04 – storage –

65/125

1void

h()

{int

y;

y++;}

2void

g()

{int

x=

5;

x++;}

3int∗

f()

{int

c=

3;

g();

h();

h();

retu

rn

&c;

}45

int

a=

27,

b,

∗p;

6p

=f();

7b

=∗p;

...0x1000

0x1001

0x1002

0x1003

0x1004

0x1005

0x1006

0x1007

0x1008

0x1009

0x100A

0x100B

0x100C

0x100D

0x100E

0x00

0x100F

0x08

0x1010

0x00

0x1011

0x03

0x1012

0x00

0x1013

0x00

0x1014

0x00

0x1015

0x00

0x1016

0x00

0x1017

0x1B

...

pb

a

Page 18: swt.informatik.uni-freiburg.de · – 2014-04 – main – F o r m a l M e t hod s f o r C S e m i na r – Su mm e r S e m e s t e r 2014 D a n i e l D i e t s c h, S e rg i o F

Sto

rage

Dura

tion

“A

uto

matic”

(Sim

plifi

ed)

– 2014-04 – storage –

65/125

1void

h()

{int

y;

y++;}

2void

g()

{int

x=

5;

x++;}

3int∗

f()

{int

c=

3;

g();

h();

h();

retu

rn

&c;

}45

int

a=

27,

b,

∗p;

6p

=f();

7b

=∗p;

...0x1000

0x1001

0x1002

0x1003

0x1004

0x1005

0x1006

0x1007

0x1008

0x1009

0x100A

0x100B

0x100C

0x100D

0x100E

0x00

0x100F

0x08

0x1010

0x00

0x1011

0x03

0x1012

0x00

0x1013

0x00

0x1014

0x00

0x1015

0x00

0x1016

0x00

0x1017

0x1B

...

pb

a

Sto

rage

Dura

tion

“A

uto

matic”

(Sim

plifi

ed)

– 2014-04 – storage –

65/125

1void

h()

{int

y;

y++;}

2void

g()

{int

x=

5;

x++;}

3int∗

f()

{int

c=

3;

g();

h();

h();

retu

rn

&c;

}45

int

a=

27,

b,

∗p;

6p

=f();

7b

=∗p;

...0x1000

0x1001

0x1002

0x1003

0x1004

0x1005

0x1006

0x1007

0x1008

0x1009

0x100A

0x100B

0x100C

0x100D

0x100E

0x00

0x100F

0x08

0x1010

0x00

0x1011

0x03

0x1012

0x10

0x1013

0x10

0x1014

0x00

0x1015

0x00

0x1016

0x00

0x1017

0x1B

...

pb

a

Sto

rage

Dura

tion

“A

uto

matic”

(Sim

plifi

ed)

– 2014-04 – storage –

65/125

1void

h()

{int

y;

y++;}

2void

g()

{int

x=

5;

x++;}

3int∗

f()

{int

c=

3;

g();

h();

h();

retu

rn

&c;

}45

int

a=

27,

b,

∗p;

6p

=f();

7b

=∗p;

...0x1000

0x1001

0x1002

0x1003

0x1004

0x1005

0x1006

0x1007

0x1008

0x1009

0x100A

0x100B

0x100C

0x100D

0x100E

0x00

0x100F

0x08

0x1010

0x00

0x1011

0x03

0x1012

0x10

0x1013

0x10

0x1014

0x??

0x1015

0x??

0x1016

0x00

0x1017

0x1B

...

prefers

toanon-alive

object,

the

behavior

isundefined

(everythingmay

happen,fro

m‘crash

’to

‘ignore’).

pb

a

Sto

rage

Cla

ssesand

Qualifi

ers

– 2014-04 – modifiers –

66/125

Sto

rage

Cla

ssS

pecifi

ers(6

.7.1

)

– 2014-04 – modifiers –

67/125

Sto

rage

Cla

ssSpecifi

ers(6

.7.1

)

– 2014-04 – modifiers –

68/125

1typedef

char

letter;

23extern

int

x;

4extern

int

f();

56static

int

x;

//

two

uses!

(−>

later)

7static

int

f();

89

10

auto

x;

//

”his

toric

”11

12

register

y;

//

”his

toric

Page 19: swt.informatik.uni-freiburg.de · – 2014-04 – main – F o r m a l M e t hod s f o r C S e m i na r – Su mm e r S e m e s t e r 2014 D a n i e l D i e t s c h, S e rg i o F

Sto

rage

Cla

ssSpecifi

ers:extern

(6.7

.1)

– 2014-04 – modifiers –

69/125

1//

not

defin

ed

here

,”im

porte

d”...

2//

3extern

int

x;

4extern

void

f();

56//

decla

red

and

defin

ed

here

,”exported”

...

7//

8int

y;

9

10

int

g()

{11

x=

y=

27;

12

f();

13

}

•→

modules,

linkin

g(later)

•usually

onlyexternin

head

ers(later)

Sto

rage

Cla

ssSpecifi

ers:static

(6.7

.1)

– 2014-04 – modifiers –

70/125

1//

decla

red

and

defin

ed

here

,2

//

not

”exported”

...

3//

4static

int

x;

5static

void

g();

678int

f()

{9

static

int

a=

0;

10

a++;

11

prin

tf(

”%s\n”,

a);

12

}13

14

f();

f();

f();

//

yie

lds

1,

2,

3

Qu

alifi

ers(6

.7.3

)

– 2014-04 – modifiers –

71/125

Qualifi

ers(6

.7.3

)

– 2014-04 – modifiers –

72/125

1int

x;

23const

int

y;

45vola

tile

int

z;

67int∗

restric

tp;

//

alia

sin

g89

10

const

vola

tile

int

a;

restrict:

•“[...

lengthyform

aldefinitio

n...]”

•“[...]

Ifthese

requirem

ents

arenotmet,

then

thebehavior

isundefined.”

•→

use

extre

mely

carefully

(i.e.ifin

doubt,notat

all)

Excu

rsion:

Mem

ory

Mapped

I/O

– 2014-04 – modifiers –

73/125

•Intuitio

n:somemem

oryaddresses

arewired

tohard

ware

•writin

gto

theaddress

causes

apin

tochangelogical

value

•readingtheaddress

gives

logical

valueofapin

...0x1000

0x1001

0x1002

0x1003

0x1004

0x1005

0x1006

0x00

0x1007

0x00

0x1008

0x1009

0x100A

0x100B

0x100C

0x100D

0x100E

0x100F

0x1010

0x1011

0x1012

0x1013

0x1014

0x1015

0x1016

0x1017

...

Excu

rsion:

Mem

ory

Mapped

I/O

– 2014-04 – modifiers –

73/125

•Intuitio

n:somemem

oryaddresses

arewired

tohard

ware

•writin

gto

theaddress

causes

apin

tochangelogical

value

•readingtheaddress

gives

logical

valueofapin

...0x1000

0x1001

0x1002

0x1003

0x1004

0x1005

0x1006

0x01

0x1007

0x00

0x1008

0x1009

0x100A

0x100B

0x100C

0x100D

0x100E

0x100F

0x1010

0x1011

0x1012

0x1013

0x1014

0x1015

0x1016

0x1017

...

Page 20: swt.informatik.uni-freiburg.de · – 2014-04 – main – F o r m a l M e t hod s f o r C S e m i na r – Su mm e r S e m e s t e r 2014 D a n i e l D i e t s c h, S e rg i o F

Excu

rsion:

Mem

ory

Mapped

I/O

– 2014-04 – modifiers –

73/125

•Intuitio

n:somemem

oryaddresses

arewired

tohard

ware

•writin

gto

theaddress

causes

apin

tochangelogical

value

•readingtheaddress

gives

logical

valueofapin

...0x1000

0x1001

0x1002

0x1003

0x1004

0x1005

0x1006

0x01

0x1007

0x01

0x1008

0x1009

0x100A

0x100B

0x100C

0x100D

0x100E

0x100F

0x1010

0x1011

0x1012

0x1013

0x1014

0x1015

0x1016

0x1017

...

Excu

rsion:

Mem

ory

Mapped

I/O

– 2014-04 – modifiers –

73/125

•Intuitio

n:somemem

oryaddresses

arewired

tohard

ware

•writin

gto

theaddress

causes

apin

tochangelogical

value

•readingtheaddress

gives

logical

valueofapin

...0x1000

0x1001

0x1002

0x1003

0x1004

0x1005

0x1006

0x01

0x1007

0x00

0x1008

0x1009

0x100A

0x100B

0x100C

0x100D

0x100E

0x100F

0x1010

0x1011

0x1012

0x1013

0x1014

0x1015

0x1016

0x1017

...

Excu

rsion:

Mem

ory

Mapped

I/O

– 2014-04 – modifiers –

73/125

•Intuitio

n:somemem

oryaddresses

arewired

tohard

ware

•writin

gto

theaddress

causes

apin

tochangelogical

value

•readingtheaddress

gives

logical

valueofapin

...0x1000

0x1001

0x1002

0x1003

0x1004

0x1005

0x1006

0x01

0x1007

0x00

0x1008

0x1009

0x100A

0x100B

0x100C

0x100D

0x100E

0x100F

0x1010

0x1011

0x1012

0x1013

0x1014

0x1015

0x1016

0x1017

...

•Thecompiler

does

notkn

ow,“m

emory

ismemory”

.

Qualifi

ers:volatile

(6.7

.3)

– 2014-04 – modifiers –

74/125

1vola

tile

char∗

out=

0x1006;

2vola

tile

char∗

in=

0x1007;

34out=

0x01;

//

switch

lamp

on

56if

(in

&0x01)

{/∗

...

∗/

}78

if

((in

&0x01)&&

(in

&0x01))

{/∗

...

∗/

}

...0x1000

0x1001

0x1002

0x1003

0x1004

0x1005

0x1006

0x00

0x1007

0x00

0x1008

0x1009

0x100A

0x100B

0x100C

0x100D

0x100E

0x100F

0x1010

0x1011

0x1012

0x1013

0x1014

0x1015

0x1016

0x1017

...

Strin

gs

&In

put/O

utp

ut

– 2014-04 – stringsandio –

75/125

Strin

gs

– 2014-04 – stringsandio –

76/125

Page 21: swt.informatik.uni-freiburg.de · – 2014-04 – main – F o r m a l M e t hod s f o r C S e m i na r – Su mm e r S e m e s t e r 2014 D a n i e l D i e t s c h, S e rg i o F

Strin

gs

are

0-T

ermin

ated

ch

ar

Arra

ys

– 2014-04 – stringsandio –

77/125

1char∗

msg

=”Hello

”;

2char∗

str

=msg

;

...0x1000

0x1001

0x10

0x1002

0x06

0x1003

0x1004

0x1005

0x1006

’H’

0x1007

’e’0x1008’l’

0x1009’l’

0x100A

’o’

0x100B

’\0’

0x100C

0x100D

0x100E

0x100F

0x1010

0x1011

0x1012

0x1013

0x1014

0x1015

0x1016

0x..

0x1017

0x..

...

msg

(char)

0

Strin

gs

are

0-T

ermin

ated

ch

ar

Arra

ys

– 2014-04 – stringsandio –

77/125

1char∗

msg

=”Hello

”;

2char∗

str

=msg

;

...0x1000

0x1001

0x10

0x1002

0x06

0x1003

0x1004

0x1005

0x1006

’H’

0x1007

’e’0x1008’l’

0x1009’l’

0x100A

’o’

0x100B

’\0’

0x100C

0x100D

0x100E

0x100F

0x1010

0x1011

0x1012

0x1013

0x1014

0x1015

0x1016

0x10

0x1017

0x06

...

msg

str

(char)

0

Strin

gM

anip

ula

tion

(Annex

B)

– 2014-04 – stringsandio –

78/125

#inclu

de<strin

g.h>

provid

esam

ongothers:

•size

tstrle

n(const

char*

s)

“[...]calcu

lateslen

gthof

strings,exclu

dingtheterm

inatin

gnullbyte

(’\0’).”

•intstrcm

p(const

char*

s1,const

char*

s2)

“[...]com

pares

thetwostrin

gss1

ands2.

Itretu

rnsan

integer

lessthan,equal

to,or

greaterthan

zeroifs1

isfou

nd,

respectively,

tobeless

than,to

match

,or

begreater

than

s2.”

•char*

strcpy(char*

s1,const

char*

s2)

“Thestrcpy()

function

copies

thestrin

gpoin

tedto

bys2,

inclu

dingtheterm

inatin

gnullbyte

(’\0’),to

thebuffer

poin

tedto

bys1.”

•char*

strncp

y(char*

s1,const

char*

s2,size

tn)

Noneofthese

functio

nsallo

catesmemory!

Allo

cateandcopy:

(notC99,butPOSIX)

•char*

strdup(const

char*

s)

Inp

ut/O

utp

ut

– 2014-04 – stringsandio –

79/125

Prin

ting

– 2014-04 – stringsandio –

80/125

1#in

clu

de

<stdio

.h>

23prin

tf(

”%s−

%i−

%f\n”,

”Hello

”,

27,

3.1

4);

Tools

&M

odules

– 2014-04 – tools –

81/125

Page 22: swt.informatik.uni-freiburg.de · – 2014-04 – main – F o r m a l M e t hod s f o r C S e m i na r – Su mm e r S e m e s t e r 2014 D a n i e l D i e t s c h, S e rg i o F

Hello

,A

gain

– 2014-04 – tools –

82/125

1#in

clu

de

<stdio

.h>

23int

g(

int

x)

{retu

rn

x/2;

}45

int

f()

{retu

rn

g(1);

}67

int

main

()

{8

prin

tf(

”Hello

World

.\n”

);

9retu

rn

f();

10

}

•%

gcc

helloworld.c

•%

ls

•a.out

helloworld.c

•%

./a.out

•Hello

World.

•%

Zoom

In:

Prep

rocessin

g,

Com

pilin

g,

Lin

king

– 2014-04 – tools –

83/125

1#in

clu

de

<stdio

.h>

23int

g(

int

x)

{retu

rn

x/2;

}45

int

f()

{retu

rn

g(1);

}67

int

main

()

{8

prin

tf(

”Hello

World

.\n”

);

9retu

rn

f();

10

}

•%

gcc

-E

helloworld.c

>helloworld.i

•%

gcc

-c

-o

helloworld.i

•%

ld

-o

helloworld

[...]

helloworld.o

[...]

•%

./helloworld

•Hello

World.

•%

Modules

– 2014-04 – tools –

84/125

1#in

clu

de

<stdio

.h>

23int

g(

int

x)

{4

retu

rn

x/2;

5}

67int

f()

{8

retu

rn

g(1);

9}

10

11

int

main

()

{12

prin

tf(

”Hello

World

.\n”

);

13

retu

rn

f();

14

}

Modules

– 2014-04 – tools –

84/125

1#in

clu

de

<stdio

.h>

23int

g(

int

x)

{4

retu

rn

x/2;

5}

67int

f()

{8

retu

rn

g(1);

9}

10

11

int

main

()

{12

prin

tf(

”Hello

World

.\n”

);

13

retu

rn

f();

14

}Split

into:

•.h(head

er):declaratio

ns

•.c:

definitio

ns,

use

head

ersto

“im

port”

declaratio

ns

Modules

– 2014-04 – tools –

84/125

1#in

clu

de

<stdio

.h>

23int

g(

int

x)

{4

retu

rn

x/2;

5}

67int

f()

{8

retu

rn

g(1);

9}

10

11

int

main

()

{12

prin

tf(

”Hello

World

.\n”

);

13

retu

rn

f();

14

}Split

into:

•.h(head

er):declaratio

ns

•.c:

definitio

ns,

use

head

ersto

“im

port”

declaratio

ns

1#ifndef

GH

2#defin

eG

H34

extern

int

5g(

int

x);

6#endif

1#ifndef

FH

2#defin

eF

H34

extern

int

5f();

6#endif

1#in

clu

de

”g.h

”23

int

g(

int

x)

{4

retu

rn

x/2;

5}

1#in

clu

de

”g.h

”2

#in

clu

de

”f.h

”34

int

f()

{5

retu

rn

g(1);

6}

1#in

clu

de

<stdio

.h>

2#in

clu

de

”f.h

”34

int

main

()

{5

prin

tf(

”Hello

World

.\n”

);

6retu

rn

f();

7}

g.h

f.h

g.c

f.c

helloworld.c

Modules

At

Work

– 2014-04 – tools –

85/125

preprocess

&co

mpile

:

•%gcc

-c

g.cf.c

\helloworld.c

•%ls

*.o

•f.o

g.ohelloworld.o

link:

•%gcc

g.o

f.ohelloworld.o

exe

cute:

•%./a.out

•Hello

World.

1#ifndef

GH

2#defin

eG

H34

extern

int

5g(

int

x);

6#endif

1#ifndef

FH

2#defin

eF

H34

extern

int

5f();

6#endif

1#in

clu

de

”g.h

”23

int

g(

int

x)

{4

retu

rn

x/2;

5}

1#in

clu

de

”g.h

”2

#in

clu

de

”f.h

”34

int

f()

{5

retu

rn

g(1);

6}

1#in

clu

de

<stdio

.h>

2#in

clu

de

”f.h

”34

int

main

()

{5

prin

tf(

”Hello

World

.\n”

);

6retu

rn

f();

7}

g.h

f.h

g.c

f.c

helloworld.c

Page 23: swt.informatik.uni-freiburg.de · – 2014-04 – main – F o r m a l M e t hod s f o r C S e m i na r – Su mm e r S e m e s t e r 2014 D a n i e l D i e t s c h, S e rg i o F

Modules

At

Work

– 2014-04 – tools –

85/125

preprocess

&co

mpile

:

•%gcc

-c

g.cf.c

\helloworld.c

•%ls

*.o

•f.o

g.ohelloworld.o

link:

•%gcc

g.o

f.ohelloworld.o

exe

cute:

•%./a.out

•Hello

World.

1#ifndef

GH

2#defin

eG

H34

extern

int

5g(

int

x);

6#endif

1#ifndef

FH

2#defin

eF

H34

extern

int

5f();

6#endif

1#in

clu

de

”g.h

”23

int

g(

int

x)

{4

retu

rn

x/2;

5}

1#in

clu

de

”g.h

”2

#in

clu

de

”f.h

”34

int

f()

{5

retu

rn

g(1);

6}

1#in

clu

de

<stdio

.h>

2#in

clu

de

”f.h

”34

int

main

()

{5

prin

tf(

”Hi!\

n”

);

6retu

rn

f();

7}

g.h

f.h

g.c

f.c

helloworld.c

Modules

At

Work

– 2014-04 – tools –

85/125

preprocess

&co

mpile

:

•%gcc

-c

g.cf.c

\helloworld.c

•%ls

*.o

•f.o

g.ohelloworld.o

link:

•%gcc

g.o

f.ohelloworld.o

exe

cute:

•%./a.out

•Hello

World.

fixandre-build

:

•%gcc

-c

helloworld.c

•%gcc

g.o

f.ohelloworld.o

•%./a.out

•Hi!

1#ifndef

GH

2#defin

eG

H34

extern

int

5g(

int

x);

6#endif

1#ifndef

FH

2#defin

eF

H34

extern

int

5f();

6#endif

1#in

clu

de

”g.h

”23

int

g(

int

x)

{4

retu

rn

x/2;

5}

1#in

clu

de

”g.h

”2

#in

clu

de

”f.h

”34

int

f()

{5

retu

rn

g(1);

6}

1#in

clu

de

<stdio

.h>

2#in

clu

de

”f.h

”34

int

main

()

{5

prin

tf(

”Hi!\

n”

);

6retu

rn

f();

7}

g.h

f.h

g.c

f.c

helloworld.c

Prep

rocessin

g

– 2014-04 – tools –

86/125

1#in

clu

de

<stdio

.h>

2#in

clu

de

”f.h

”34

int

main

()

{5

prin

tf(

”Hello

World

.\n”

);

6retu

rn

f();

7}•

%gcc

-E

helloworld.c

-o

helloworld.i

1#

1”hello

world

.c”

2#

1”<

comman

d−lin

e>”

3#

1”hello

world

.c”

4#

1”/usr/in

clu

de/stdio

.h”

13

45

#28

”/usr/in

clu

de/stdio

.h”

34

6#

1”/usr/in

clu

de/fe

atures.h

”1

34

7#

323

”/usr/in

clu

de/fe

atures.h

”3

48

#1

”/usr/in

clu

de/x86

64−lin

ux−gnu/bits/pre

defs

.h”

13

49

#324

”/usr/in

clu

de/fe

atures.h

”2

34

10

#356

”/usr/in

clu

de/fe

atures.h

”3

411

#1

”/usr/in

clu

de/x86

64−lin

ux−gnu/sys/cdefs

.h”

13

412

#359

”/usr/in

clu

de/x86

64−lin

ux−gnu/sys/cdefs

.h”

34

13

#1

”/usr/in

clu

de/x86

64−lin

ux−gnu/bits/word

size.h

”1

34

14

#360

”/usr/in

clu

de/x86

64−lin

ux−gnu/sys/cdefs

.h”

23

415

#357

”/usr/in

clu

de/fe

atures.h

”2

34

16

#388

”/usr/in

clu

de/fe

atures.h

”3

417

#1

”/usr/in

clu

de/x86

64−lin

ux−gnu/gnu/stubs.h

”1

34

18

19

20

21

#1

”/usr/in

clu

de/x86

64−lin

ux−gnu/bits/word

size.h

”1

34

22

#5

”/usr/in

clu

de/x86

64−lin

ux−gnu/gnu/stubs.h

”2

34

23

24

[...]

25

26

extern

int

ftrylo

ckfile

(FILE

∗stre

am

)attrib

ute

((

nothro

w))

;27

28

29

extern

void

funlo

ckfile

(FILE

∗stre

am

)attrib

ute

((

nothro

w));

30

#936

”/usr/in

clu

de/stdio

.h”

34

31

32

#2

”hello

world

.c”

233

#1

”f.h

”1

34

35

36

37

extern

int

38

f();

39

#3

”hello

world

.c”

240

41

int

main

()

{42

prin

tf(

”Hello

World

.\n”

);

43

retu

rn

f();

44

}

helloworld.c

helloworld.i

Prep

rocessin

gD

irectives(6

.10)

– 2014-04 – tools –

87/125

1#in

clu

de

<stdio

.h>

2#in

clu

de

”battery

.h”

34#defin

ePI

3.1

415

56#defin

eDEBUG

7#ifdef

DEBUG

8fprin

tf(

stderr

,”honk\n”

);

9#endif

10

11

#if

GNUC

>=

312

#defin

epure

attrib

ute

((pure

))

13

#else

14

#defin

epure

/∗

no

pure

∗/

15

#endif

16

17

extern

int

f()

pure

;

Lin

king

– 2014-04 – tools –

88/125

provides:

int

g(int)

needs:

./.

provides:

int

f()

needs:

int

g(int)

providesint

main()

needs:

int

f(int)

int

printf(const

char*,...)

provides:

int

printf(const

char*,...)

...

needs:

...

g.o

f.o

helloworld.o

libc.a

Lin

king

– 2014-04 – tools –

88/125

provides:

int

g(int)

needs:

./.

provides:

int

f()

needs:

int

g(int)

providesint

main()

needs:

int

f(int)

int

printf(const

char*,...)

provides:

int

printf(const

char*,...)

...

needs:

...

g.o

f.o

helloworld.o

libc.a

Page 24: swt.informatik.uni-freiburg.de · – 2014-04 – main – F o r m a l M e t hod s f o r C S e m i na r – Su mm e r S e m e s t e r 2014 D a n i e l D i e t s c h, S e rg i o F

Lin

king

– 2014-04 – tools –

88/125

provides:

int

g(int)

needs:

./.

provides:

int

f()

needs:

int

g(int)

providesint

main()

needs:

int

f(int)

int

printf(const

char*,...)

provides:

int

printf(const

char*,...)

...

needs:

...

g.o

f.o

helloworld.o

libc.a

Lin

king

– 2014-04 – tools –

88/125

provides:

int

g(int)

needs:

./.

provides:

int

f()

needs:

int

g(int)

providesint

main()

needs:

int

f(int)

int

printf(const

char*,...)

provides:

int

printf(const

char*,...)

...

needs:

...

g.o

f.o

helloworld.o

libc.a

Lin

king

– 2014-04 – tools –

88/125

provides:

int

g(int)

needs:

./.

provides:

int

f()

needs:

int

g(int)

providesint

main()

needs:

int

f(int)

int

printf(const

char*,...)

provides:

int

printf(const

char*,...)

...

needs:

...

g.o

f.o

helloworld.o

libc.a

a.out

Com

piler

– 2014-04 – tools –

89/125

gcc

[OPTIO

N]...

infile...

-E–prepro

cesson

ly

-c–com

pile

only,

don

’tlink

Exa

mple:gcc

-cmain.c—

produces

main.o

-ooutfile

–write

outputto

outfile

Exa

mple:gcc

-c-o

x.o

main.c—

produces

x.o

-g–adddebuginform

ation

-W,-W

all,

...–enable

warn

ings

-Idir

–adddirto

inclu

depath

forsearch

inghead

ers

-Ldir

–adddirto

library

path

forsearch

inglibraries

-Dmacro

[=defn]–definemacro

(todefn)

Exa

mple:gcc

-DDEBUG

-DMAGICNUMBER=27

-l library

linkagain

stliblibrary.{a,so},

order

matters

Exa

mple:gcc

a.ob.o

main.o

-lxy

→cf.

man

gcc

gdb(1

),ddd(1

),nm

(1),

make(1

)

– 2014-04 – tools –

90/125

•CommandLineDebugger:gdb

a.out

[core]

•GUIDebugger:

ddd

a.out

[core]

(works

best

with

debugginginform

ationcompiled

in(gcc

-g))

•Insp

ect

Object

File

s:

nm

a.o

•Build

Utility

:

make

See

battery

contro

llerexercise

foran

example.

Core

Dum

ps

– 2014-04 – tools –

91/125

c©Gusta

voDuarte

2009,used

byperm

ission.

http://duartes.org/gustavo/blog/post/anatomy-of-a-program-in-memory

•Reca

ll:Anatom

yof

aLinuxProgram

inMem

ory

•Core

dump:(basically)

thismem

orywritten

toafile.

Page 25: swt.informatik.uni-freiburg.de · – 2014-04 – main – F o r m a l M e t hod s f o r C S e m i na r – Su mm e r S e m e s t e r 2014 D a n i e l D i e t s c h, S e rg i o F

Core

Dum

ps

– 2014-04 – tools –

91/125

c©Gusta

voDuarte

2009,used

byperm

ission.

http://duartes.org/gustavo/blog/post/anatomy-of-a-program-in-memory

•Reca

ll:Anatom

yof

aLinuxProgram

inMem

ory

•Core

dump:(basically)

thismem

orywritten

toafile.

1int

main

()

{2

int

∗p;

3∗p

=27;

4retu

rn

0;

5}

1%

gcc

−g

core

.c

2%

limit

core

dumpsize

3core

dumpsize

0kbytes

4%

limit

core

dumpsize

1g

5%

./a.out

6Segmenta

tion

fault

(core

dumped

)7

%ls

−lh

core

8−rw

−−−−−−−

1user

user

232K

Feb

29

11:1

1core

9%

gdb

a.out

core

10

GNU

gdb

(GDB)

7.4.1

−debian

11

[...]

12

Core

was

genera

ted

by

‘./a.out’.

13

Program

term

inated

with

sig

nal

11,

Segmenta

tion

fault

.14

#0

0x00000000004004b4

inmain

()

at

core

.c:3

15

3∗p

=27;

16

(gdb)

pp

17

$1

=(in

t∗)

0x0

18

(gdb)

q

Form

al

Meth

ods

for

C

– 2014-04 – assert –

92/125

Co

rrectness

an

dR

equ

iremen

ts

– 2014-04 – assert –

93/125

Correctn

ess

– 2014-04 – assert –

94/125

•Correctn

essisdefined

with

resp

ect

toaspecifi

cation.

•A

program

(functio

n,...)

isco

rrect

(wrt.

specifi

cationϕ)

ifandonly

ifitsatisfi

esϕ.

•Definitio

nof“satisfi

es”:in

aminute.

Examples:

•ϕ1 :

theretu

rnvalu

eis10divid

edby

param

eter(if

param

eternot0)

•ϕ2 :

thevalu

eofvariab

lexis“alw

ays”strictly

greater

than

3

•ϕ3 :

thevalu

eofiincreases

ineach

loopiteratio

n

•...

Com

mon

Pattern

s

– 2014-04 – assert –

95/125

•State

Invaria

nts:

“at

this

program

point,thevalu

eofpmust

notbeNULL”

“at

allpro

gram

points,

thevalu

eofpmust

notbeNULL”

(cf.sequence

points

(Annex

C))

•Data

Invaria

nts:

“thevalu

eofnmust

bethelen

gth

ofs”

•(F

unctio

n)Pre/Post

Conditio

ns:

Pre-C

onditio

n:theparam

etermust

notbe0

Post-C

onditio

n:theretu

rnvalu

eis10divid

edby

theparam

eter

•LoopInvaria

nts:

“thevalu

eofiisbetw

een0andarray

length

minus1”

Po

or

Ma

n’s

Req

uirem

ents

Sp

ecifica

tion

aka

.H

ow

toF

orm

alize

Req

uirem

ents

inC

?

– 2014-04 – assert –

96/125

Page 26: swt.informatik.uni-freiburg.de · – 2014-04 – main – F o r m a l M e t hod s f o r C S e m i na r – Su mm e r S e m e s t e r 2014 D a n i e l D i e t s c h, S e rg i o F

Dia

gnostics

(7.2

)

– 2014-04 – assert –

97/125

1#in

clu

de

<assert.h>

2void

assert(

/∗

scala

r∗/

expressio

n);

Dia

gnostics

(7.2

)

– 2014-04 – assert –

97/125

1#in

clu

de

<assert.h>

2void

assert(

/∗

scala

r∗/

expressio

n);

•“Theassert

macro

puts

diag

nostic

testsinto

program

s;[...]

When

itisexecu

ted,ifexpression(w

hich

shall

have

ascalar

type)

isfalse

(that

is,compares

equal

to0),

theassert

macro

•writes

inform

ationab

outtheparticu

larcall

that

failed[...]

onthe

standard

errorstream

inan

implem

entatio

n-defined

format.

•Itthen

callstheabort

functio

n.”

Dia

gnostics

(7.2

)

– 2014-04 – assert –

97/125

1#in

clu

de

<assert.h>

2void

assert(

/∗

scala

r∗/

expressio

n);

•“Theassert

macro

puts

diag

nostic

testsinto

program

s;[...]

When

itisexecu

ted,ifexpression(w

hich

shall

have

ascalar

type)

isfalse

(that

is,compares

equal

to0),

theassert

macro

•writes

inform

ationab

outtheparticu

larcall

that

failed[...]

onthe

standard

errorstream

inan

implem

entatio

n-defined

format.

•Itthen

callstheabort

functio

n.”

Pitfall:

•Ifmacro

NDEBUGisdefinedwhen

inclu

ding<asse

rt.h>,

expressionis

notevaluated(th

usshould

besid

e-effect

free).

abort

(7.2

0.4

.1)

– 2014-04 – assert –

98/125

1#in

clu

de

<stdlib

.h>

23void

abort

();

•“Theab

ortfunctio

ncau

sesabnorm

alpro

gram

terminatio

nto

occu

r,unless

[...]

•[...]

Anim

plem

entatio

n-defined

formofthestatu

sunsuccessfu

lterm

inatio

nisretu

rned

tothehost

enviro

nmentby

mean

softhefunctio

ncall

raise(SIGABRT).”

(→Core

Dumps)

Com

mon

Pattern

sw

ithassert

– 2014-04 – assert –

99/125

•State

Invaria

nts:

“at

this

program

point,thevalu

eofpmust

notbeNULL”

“at

allpro

gram

points,

thevalu

eofpmust

notbeNULL”

(cf.sequence

points

(Annex

C))

•Data

Invaria

nts:

“thevalu

eofnmust

bethelen

gth

ofs”

•(F

unctio

n)Pre/Post

Conditio

ns:

Pre-C

onditio

n:theparam

etermust

notbe0

Post-C

onditio

n:theretu

rnvalu

eis10divid

edby

theparam

eter

•LoopInvaria

nts:

“thevalu

eofiisbetw

een0andarray

length

minus1”

Sta

teIn

varia

nts

with

<assert.h

>

– 2014-04 – assert –

100/125

1void

f()

{2

int∗

p=

(int∗)mallo

c(sizeof(int));

34if

(!p)

5retu

rn;

67assert(p);

//

assu

me

pis

valid

from

here

8//

...

9}

10

11

void

g()

{12

Node∗

p=

find(

’a’

);

13

14

assert(p);

//

we

inserted

’a’

befo

re15

//

...

16

}

Page 27: swt.informatik.uni-freiburg.de · – 2014-04 – main – F o r m a l M e t hod s f o r C S e m i na r – Su mm e r S e m e s t e r 2014 D a n i e l D i e t s c h, S e rg i o F

Data

Inva

riants

with

<assert.h

>

– 2014-04 – assert –

101/125

1typedef

struct

{2

char∗

s;

3int

n;

4}

str;

56str∗

constru

ct(

char∗

s)

{7

str∗

x=

(str∗)mallo

c(

sizeof(str)

);

8//

...

9assert(

(x−

>s==

NULL

&&

x−>n

==

−1)

10

||(x−

>n

=strle

n(

x−>s

))

);

11

}

Pre/P

ost

Conditio

ns

with

<assert.h

>

– 2014-04 – assert –

102/125

1int

f(

int

x)

{2

assert(

x!=

0);

//

pre−conditio

n34

int

r=

10/x;

56assert(

r==

10/x

);

//

post−

condititio

n78

retu

rn

r;

9}

Loop

Inva

riants

with

<assert.h

>

– 2014-04 – assert –

103/125

1void

f(

int

a[]

,int

n)

{2

int

i=

0;

34//

hold

sbefo

reth

eloop

5assert(

0<=

i&&

i<=

n);

6assert(

i<

1||

a[i−1]==

0);

78while

(i<

n)

{9

//

hold

sbefo

reeach

iteratio

n10

assert(

0<=

i&&

i<=

n);

11

assert(

i<

1||

a[i−1]==

0);

12

13

a[i+

+]=

0;

14

}15

//

hold

saft

er

exitin

gth

eloop

16

assert(

0<=

i&&

i<=

n);

17

assert(

i<

1||

a[i−1]==

0);

18

19

retu

rn;

20

}

Old

Varia

bles,

Ghost

Varia

bles

– 2014-04 – assert –

104/125

1void

xorS

wap(

unsigned

int∗

a,

unsigned

int∗

b)

{2

#ifndefNDEBUG

3unsigned

int

∗old

a=

a,

∗old

b=

b;

4#endif

5assert(

a&&

b);

assert(

a!=

b);

//

pre−conditio

n67

∗a

=∗a

+∗b;

8∗b

=∗a

−∗b;

9∗a

=∗a

−∗b;

10

11

assert(

∗a

==

∗old

b&&

∗b

==

∗old

a);

//

post−

con−

12

assert(

a==

old

a&&

b==

old

b);

//

ditio

n13

}

Outlo

ok

– 2014-04 – assert –

105/125

•Someverifi

cationtoolssim

ply

verifyfor

eachassertstatem

ent:

When

executed

,expressionisnotfalse.

•Someverifi

cationtoolssupport

sophisticated

requirem

ents

specifi

cation

languages

likeACSLwith

explicit

support

for

•pre/

post

conditio

ns

•ghost

variables,

old

values

•data

invarian

ts

•loopinvarian

ts

•...

Dep

end

ab

leVerifi

catio

n(Ja

ckson

)

– 2014-04 – assert –

106/125

Page 28: swt.informatik.uni-freiburg.de · – 2014-04 – main – F o r m a l M e t hod s f o r C S e m i na r – Su mm e r S e m e s t e r 2014 D a n i e l D i e t s c h, S e rg i o F

Dep

endability

– 2014-04 – assert –

107/125

•“Theprogram

hasbeenverifi

ed.”

tellsus

Dep

endability

– 2014-04 – assert –

107/125

•“Theprogram

hasbeenverifi

ed.”

tellsusnotvery

much

.

Dep

endability

– 2014-04 – assert –

107/125

•“Theprogram

hasbeenverifi

ed.”

tellsusnotvery

much

.

•Onewants

tokn

ow(an

dshould

state):

Dep

endability

– 2014-04 – assert –

107/125

•“Theprogram

hasbeenverifi

ed.”

tellsusnotvery

much

.

•Onewants

tokn

ow(an

dshould

state):

•W

hich

specifi

catio

nshave

been

consid

ered?

Dep

endability

– 2014-04 – assert –

107/125

•“Theprogram

hasbeenverifi

ed.”

tellsusnotvery

much

.

•Onewants

tokn

ow(an

dshould

state):

•W

hich

specifi

catio

nshave

been

consid

ered?

•Under

which

assu

mptio

nswas

theverifi

cationconducted

?

•Platform

assumptio

ns:

finite

word

s(size?),

math

ematical

integ

ers,...

•Enviro

nmentassu

mptio

ns,inputvalu

es,...

Assu

mptio

nsare

often

implicit,

“inthetool”!

Dep

endability

– 2014-04 – assert –

107/125

•“Theprogram

hasbeenverifi

ed.”

tellsusnotvery

much

.

•Onewants

tokn

ow(an

dshould

state):

•W

hich

specifi

catio

nshave

been

consid

ered?

•Under

which

assu

mptio

nswas

theverifi

cationconducted

?

•Platform

assumptio

ns:

finite

word

s(size?),

math

ematical

integ

ers,...

•Enviro

nmentassu

mptio

ns,inputvalu

es,...

Assu

mptio

nsare

often

implicit,

“inthetool”!

•Andwhatdoesverifi

catio

nmeanafter

all?

•In

somecontexts:

testin

g.

•In

somecontexts:

review.

•In

somecontexts:

model-ch

eck

ingpro

cedure.

(“Weverifi

edtheprogram

!”–“What

did

thetoolsay?”

–“Verifi

cationfailed

.”)

•In

somecontexts:

model-ch

eck

ingtoolcla

imsco

rrectn

ess.

Page 29: swt.informatik.uni-freiburg.de · – 2014-04 – main – F o r m a l M e t hod s f o r C S e m i na r – Su mm e r S e m e s t e r 2014 D a n i e l D i e t s c h, S e rg i o F

Com

mon

Erro

rs

– 2014-04 – pitfalls –

108/125

Distin

guish

– 2014-04 – pitfalls –

109/125

Most

generic

erro

rsboildow

nto:

•specifi

edbutunwantedbehaviour,

e.g.under/

overfl

ows

•initia

lisatio

nissu

es

e.g.automatic

block

scopeobjects

•unsp

ecifi

edbehaviour(J.1

)e.g

.ord

erofevalu

ationin

somecases

•undefinedbehaviour(J.2

)

•im

plementatio

ndefinedbehaviour(J.3

)

Confo

rmance

(4)

– 2014-04 – pitfalls –

110/125

•“A

program

that

is

•correct

inall

other

aspects,

•op

eratingon

correctdata,

•con

tainingunsp

ecifi

edbehavio

r

shall

beacorrect

program

andact

inaccord

ance

with

5.1.2.3.(P

rogram

Execu

tion)

•A

conform

ingpro

gram

isonethat

isaccep

table

toaconform

ing

implem

entatio

n.

•Strictly

conform

ingpro

gram

sare

inten

ded

tobemaxim

allyportab

leam

ong

conform

ingim

plem

entatio

ns.

•Anim

plem

entatio

n[ofC,acompiler]

shall

beacco

mpanied

byadocument

that

defines

allim

plem

entatio

n-defined

andlocale-sp

ecificcharacteristics

andall

extensio

ns.

Over-

an

dU

nd

erflo

ws

– 2014-04 – pitfalls –

111/125

Over-

and

Underfl

ow

s,C

astin

g

– 2014-04 – pitfalls –

112/125

•Notspecifi

cto

C...

1void

f(

short

a,

int

b)

{2

a=

b;

//

typin

gok,

but...

3}

45short

a;

//

provisio

nin

g,

implic

itcast

6if

(++a

<0)

{/∗

no

∗/

}78

if

(++i>

MAX

INT)

{9

/∗

no

∗/

}10

11

12

int

e=

0;

13

14

void

set

error()

{e++;}

15

void

cle

ar

error()

{e

=0;

}16

17

void

g()

{if

(e)

{/∗

...

∗/

}}

Initia

lisatio

n(6

.7.8

)

– 2014-04 – pitfalls –

113/125

Page 30: swt.informatik.uni-freiburg.de · – 2014-04 – main – F o r m a l M e t hod s f o r C S e m i na r – Su mm e r S e m e s t e r 2014 D a n i e l D i e t s c h, S e rg i o F

Initia

lisatio

n(6

.7.8

)

– 2014-04 – pitfalls –

114/125

•“Ifan

object

that

has

automatic

storageduratio

nisnotinitialized

explicitly,

itsvalu

eisindeterm

inate.”

1void

f()

{2

int

a;

34prin

tf(

”%i\

n”,

a);

//

surprise...

5}

Un

specifi

edB

eha

viou

r(J.1

)

– 2014-04 – pitfalls –

115/125

Unsp

ecified

Beh

avio

ur

(J.1)

– 2014-04 – pitfalls –

116/125

Each

implem

entatio

n(ofacompiler)

documents

how

thechoice

ismade.

Forexample

•wheth

ertwostrin

gliterals

resultin

distin

ctarrays

(6.4.5)

•theord

erin

which

thefunction

design

ator,argu

ments,

andsubexpression

swith

intheargu

ments

areevalu

atedin

afunction

call(6.5.2.2)

•thelayou

tof

storagefor

function

param

eters(6.9.1)

•theresu

ltof

roundingwhen

thevalu

eisou

tof

range

(7.12.9.5,...)

•theord

erandcon

tiguity

ofstorage

allocated

bysuccessive

callsto

malloc(7.20.3)

•etc.

pp.

1char

a[]

=”hello

”,

b[]

=”hello

”;

//

a==

b?

23i=

0;

f(++i,++i,++i

);

//

f(1,2

,3)?

45int

g()

{int

a,

b;

}//

&a

>&b

?67

int∗

p=

mallo

c(sizeof(int));

8int∗

q=

mallo

c(sizeof(int));

//

q>

p?

Un

defi

ned

Beh

avio

ur

(J.2)

– 2014-04 – pitfalls –

117/125

Undefi

ned

Beh

avio

ur

(3.4

.3)

– 2014-04 – pitfalls –

118/125

“Behavio

ur,uponuse

ofanon-portab

leor

erroneouspro

gram

constru

ctor

oferro

neousdata,

forwhich

thisIntern

ational

Stan

dard

imposes

no

requirem

ents.”

“Possib

leundefinedbehaviourrangesfro

m

•ignorin

gthesitu

ationcompletely

with

unpredicta

ble

results,

•to

behavin

gdurin

gtra

nsla

tionorprogram

execu

tion

inadocumented

manner

characteristic

oftheenviro

nment

(with

orwith

outtheissu

ance

ofadiag

nostic

messag

e),

•to

terminatin

gatra

nsla

tionorexecu

tion

(with

theissu

ance

ofadiag

nostic

messag

e).”

“Anexam

ple

ofundefined

behavio

uristhebehavio

uronintegeroverflow.”

Undefi

ned

Beh

avio

ur

(J.2)

– 2014-04 – pitfalls –

119/125

More

examples:

•an

identifi

er[...]

contain

san

invalid

multibyte

character

(5.2.1.2)

Page 31: swt.informatik.uni-freiburg.de · – 2014-04 – main – F o r m a l M e t hod s f o r C S e m i na r – Su mm e r S e m e s t e r 2014 D a n i e l D i e t s c h, S e rg i o F

Undefi

ned

Beh

avio

ur

(J.2)

– 2014-04 – pitfalls –

119/125

More

examples:

•an

identifi

er[...]

contain

san

invalid

multibyte

character

(5.2.1.2)

•an

object

isreferred

toou

tsideof

itslifetim

e(6.2.4)

Undefi

ned

Beh

avio

ur

(J.2)

– 2014-04 – pitfalls –

119/125

More

examples:

•an

identifi

er[...]

contain

san

invalid

multibyte

character

(5.2.1.2)

•an

object

isreferred

toou

tsideof

itslifetim

e(6.2.4)

•thevalu

eof

apoin

terto

anob

jectwhole

lifetimehas

ended

isused

(6.2.4)

Undefi

ned

Beh

avio

ur

(J.2)

– 2014-04 – pitfalls –

119/125

More

examples:

•an

identifi

er[...]

contain

san

invalid

multibyte

character

(5.2.1.2)

•an

object

isreferred

toou

tsideof

itslifetim

e(6.2.4)

•thevalu

eof

apoin

terto

anob

jectwhole

lifetimehas

ended

isused

(6.2.4)

•con

versionto

orfrom

aninteger

typepro

duces

avalu

eou

tsidetheran

gethat

canberepresen

ted(6.3.1.4)

Undefi

ned

Beh

avio

ur

(J.2)

– 2014-04 – pitfalls –

119/125

More

examples:

•an

identifi

er[...]

contain

san

invalid

multibyte

character

(5.2.1.2)

•an

object

isreferred

toou

tsideof

itslifetim

e(6.2.4)

•thevalu

eof

apoin

terto

anob

jectwhole

lifetimehas

ended

isused

(6.2.4)

•con

versionto

orfrom

aninteger

typepro

duces

avalu

eou

tsidetheran

gethat

canberepresen

ted(6.3.1.4)

•con

versionbetw

eentwopoin

tertyp

espro

duces

aresu

ltthat

isincorrectly

aligned

(6.3.2.3)

Undefi

ned

Beh

avio

ur

(J.2)

– 2014-04 – pitfalls –

119/125

More

examples:

•an

identifi

er[...]

contain

san

invalid

multibyte

character

(5.2.1.2)

•an

object

isreferred

toou

tsideof

itslifetim

e(6.2.4)

•thevalu

eof

apoin

terto

anob

jectwhole

lifetimehas

ended

isused

(6.2.4)

•con

versionto

orfrom

aninteger

typepro

duces

avalu

eou

tsidetheran

gethat

canberepresen

ted(6.3.1.4)

•con

versionbetw

eentwopoin

tertyp

espro

duces

aresu

ltthat

isincorrectly

aligned

(6.3.2.3)

•theprogram

attempts

tomodify

astrin

gliteral

(6.4.5)

Undefi

ned

Beh

avio

ur

(J.2)

– 2014-04 – pitfalls –

119/125

More

examples:

•an

identifi

er[...]

contain

san

invalid

multibyte

character

(5.2.1.2)

•an

object

isreferred

toou

tsideof

itslifetim

e(6.2.4)

•thevalu

eof

apoin

terto

anob

jectwhole

lifetimehas

ended

isused

(6.2.4)

•con

versionto

orfrom

aninteger

typepro

duces

avalu

eou

tsidetheran

gethat

canberepresen

ted(6.3.1.4)

•con

versionbetw

eentwopoin

tertyp

espro

duces

aresu

ltthat

isincorrectly

aligned

(6.3.2.3)

•theprogram

attempts

tomodify

astrin

gliteral

(6.4.5)

•an

exception

alcon

dition

occu

rsdurin

gtheevalu

ationof

anexpression

(6.5)

Page 32: swt.informatik.uni-freiburg.de · – 2014-04 – main – F o r m a l M e t hod s f o r C S e m i na r – Su mm e r S e m e s t e r 2014 D a n i e l D i e t s c h, S e rg i o F

Undefi

ned

Beh

avio

ur

(J.2)

– 2014-04 – pitfalls –

119/125

More

examples:

•an

identifi

er[...]

contain

san

invalid

multibyte

character

(5.2.1.2)

•an

object

isreferred

toou

tsideof

itslifetim

e(6.2.4)

•thevalu

eof

apoin

terto

anob

jectwhole

lifetimehas

ended

isused

(6.2.4)

•con

versionto

orfrom

aninteger

typepro

duces

avalu

eou

tsidetheran

gethat

canberepresen

ted(6.3.1.4)

•con

versionbetw

eentwopoin

tertyp

espro

duces

aresu

ltthat

isincorrectly

aligned

(6.3.2.3)

•theprogram

attempts

tomodify

astrin

gliteral

(6.4.5)

•an

exception

alcon

dition

occu

rsdurin

gtheevalu

ationof

anexpression

(6.5)

•thevalu

eof

thesecon

dop

erandof

the/or

%op

eratoriszero

(6.5.5)

Undefi

ned

Beh

avio

ur

(J.2)

– 2014-04 – pitfalls –

119/125

More

examples:

•an

identifi

er[...]

contain

san

invalid

multibyte

character

(5.2.1.2)

•an

object

isreferred

toou

tsideof

itslifetim

e(6.2.4)

•thevalu

eof

apoin

terto

anob

jectwhole

lifetimehas

ended

isused

(6.2.4)

•con

versionto

orfrom

aninteger

typepro

duces

avalu

eou

tsidetheran

gethat

canberepresen

ted(6.3.1.4)

•con

versionbetw

eentwopoin

tertyp

espro

duces

aresu

ltthat

isincorrectly

aligned

(6.3.2.3)

•theprogram

attempts

tomodify

astrin

gliteral

(6.4.5)

•an

exception

alcon

dition

occu

rsdurin

gtheevalu

ationof

anexpression

(6.5)

•thevalu

eof

thesecon

dop

erandof

the/or

%op

eratoriszero

(6.5.5)

•poin

tersthat

donot

poin

tinto,

orjust

beyon

d,thesam

earray

object

aresubtracted

(6.5.6)

Undefi

ned

Beh

avio

ur

(J.2)

– 2014-04 – pitfalls –

119/125

More

examples:

•an

identifi

er[...]

contain

san

invalid

multibyte

character

(5.2.1.2)

•an

object

isreferred

toou

tsideof

itslifetim

e(6.2.4)

•thevalu

eof

apoin

terto

anob

jectwhole

lifetimehas

ended

isused

(6.2.4)

•con

versionto

orfrom

aninteger

typepro

duces

avalu

eou

tsidetheran

gethat

canberepresen

ted(6.3.1.4)

•con

versionbetw

eentwopoin

tertyp

espro

duces

aresu

ltthat

isincorrectly

aligned

(6.3.2.3)

•theprogram

attempts

tomodify

astrin

gliteral

(6.4.5)

•an

exception

alcon

dition

occu

rsdurin

gtheevalu

ationof

anexpression

(6.5)

•thevalu

eof

thesecon

dop

erandof

the/or

%op

eratoriszero

(6.5.5)

•poin

tersthat

donot

poin

tinto,

orjust

beyon

d,thesam

earray

object

aresubtracted

(6.5.6)

•Anarray

subscrip

tisou

tof

range

[...](6.5.6)

Undefi

ned

Beh

avio

ur

(J.2)

– 2014-04 – pitfalls –

119/125

More

examples:

•an

identifi

er[...]

contain

san

invalid

multibyte

character

(5.2.1.2)

•an

object

isreferred

toou

tsideof

itslifetim

e(6.2.4)

•thevalu

eof

apoin

terto

anob

jectwhole

lifetimehas

ended

isused

(6.2.4)

•con

versionto

orfrom

aninteger

typepro

duces

avalu

eou

tsidetheran

gethat

canberepresen

ted(6.3.1.4)

•con

versionbetw

eentwopoin

tertyp

espro

duces

aresu

ltthat

isincorrectly

aligned

(6.3.2.3)

•theprogram

attempts

tomodify

astrin

gliteral

(6.4.5)

•an

exception

alcon

dition

occu

rsdurin

gtheevalu

ationof

anexpression

(6.5)

•thevalu

eof

thesecon

dop

erandof

the/or

%op

eratoriszero

(6.5.5)

•poin

tersthat

donot

poin

tinto,

orjust

beyon

d,thesam

earray

object

aresubtracted

(6.5.6)

•Anarray

subscrip

tisou

tof

range

[...](6.5.6)

•theprogram

removes

thedefinition

ofamacro

whose

nam

ebegin

swith

anunderscore

andeith

eran

uppercase

letteror

anoth

erunderscore

(7.1.3)

Undefi

ned

Beh

avio

ur

(J.2)

– 2014-04 – pitfalls –

119/125

More

examples:

•an

identifi

er[...]

contain

san

invalid

multibyte

character

(5.2.1.2)

•an

object

isreferred

toou

tsideof

itslifetim

e(6.2.4)

•thevalu

eof

apoin

terto

anob

jectwhole

lifetimehas

ended

isused

(6.2.4)

•con

versionto

orfrom

aninteger

typepro

duces

avalu

eou

tsidetheran

gethat

canberepresen

ted(6.3.1.4)

•con

versionbetw

eentwopoin

tertyp

espro

duces

aresu

ltthat

isincorrectly

aligned

(6.3.2.3)

•theprogram

attempts

tomodify

astrin

gliteral

(6.4.5)

•an

exception

alcon

dition

occu

rsdurin

gtheevalu

ationof

anexpression

(6.5)

•thevalu

eof

thesecon

dop

erandof

the/or

%op

eratoriszero

(6.5.5)

•poin

tersthat

donot

poin

tinto,

orjust

beyon

d,thesam

earray

object

aresubtracted

(6.5.6)

•Anarray

subscrip

tisou

tof

range

[...](6.5.6)

•theprogram

removes

thedefinition

ofamacro

whose

nam

ebegin

swith

anunderscore

andeith

eran

uppercase

letteror

anoth

erunderscore

(7.1.3)

•etc.

pp.

Null-P

oin

ter

– 2014-04 – pitfalls –

120/125

1int

main

()

{2

int

∗p;

3∗p

=27;

4retu

rn

0;

5}

Page 33: swt.informatik.uni-freiburg.de · – 2014-04 – main – F o r m a l M e t hod s f o r C S e m i na r – Su mm e r S e m e s t e r 2014 D a n i e l D i e t s c h, S e rg i o F

Null-P

oin

ter

– 2014-04 – pitfalls –

120/125

1int

main

()

{2

int

∗p;

3∗p

=27;

4retu

rn

0;

5}

•“A

ninteger

constan

texpression

with

thevalu

e0,

orsuch

anexpression

castto

typevoid*,

iscalled

anullpointerco

nsta

nt.

[...]”

•“T

hemacro

NULLisdefined

in<std

def.h

>(an

doth

erhead

ers)as

anullpoin

tercon

stant;see

7.17.”

•“A

mon

gtheinvalid

values

fordereferen

cingapoin

terby

theunary

*op

eratorare

anullpoin

ter,[...]”

(6.5.3.2)

Seg

men

tatio

nV

iola

tion

– 2014-04 – pitfalls –

121/125

1int

main

()

{2

int

∗p

=(int∗)0

x12345678;

3∗p

=27;

45∗(int∗)(((void

∗)p)

+1)

=13;

6retu

rn

0;

7}

Seg

men

tatio

nV

iola

tion

– 2014-04 – pitfalls –

121/125

1int

main

()

{2

int

∗p

=(int∗)0

x12345678;

3∗p

=27;

45∗(int∗)(((void

∗)p)

+1)

=13;

6retu

rn

0;

7}

•Modern

operatin

gsystem

sprovid

ememory

protectio

n.

•Accessin

gmem

orywhich

thepro

cessisnot

allowed

toaccess

isob

servedby

theop

eratingsystem

.

•Typically

aninstan

ceof

“accessingan

object

outsid

eits

lifetime”.

•But:

other

way

rounddoes

not

hold

,accessin

gan

object

outsid

eits

lifetimedoes

not

imply

asegm

entation

violation.

Seg

men

tatio

nV

iola

tion

– 2014-04 – pitfalls –

121/125

1int

main

()

{2

int

∗p

=(int∗)0

x12345678;

3∗p

=27;

45∗(int∗)(((void

∗)p)

+1)

=13;

6retu

rn

0;

7}

•Modern

operatin

gsystem

sprovid

ememory

protectio

n.

•Accessin

gmem

orywhich

thepro

cessisnot

allowed

toaccess

isob

servedby

theop

eratingsystem

.

•Typically

aninstan

ceof

“accessingan

object

outsid

eits

lifetime”.

•But:

other

way

rounddoes

not

hold

,accessin

gan

object

outsid

eits

lifetimedoes

not

imply

asegm

entation

violation.

•Som

eplatform

s(e.g.

SPARC):

unalign

edmem

oryaccess,

i.e.ou

tsideword

bou

ndaries,

not

supported

byhard

ware

(“buserror”).

Operatin

gsystem

notifi

espro

cess,defau

lthandler:

terminate,

dumpcore.

Imp

lemen

tatio

n-D

efin

edB

eha

viou

r(J.3

)

– 2014-04 – pitfalls –

122/125

Implem

enta

tion-D

efined

Beh

avio

ur

(J.3)

– 2014-04 – pitfalls –

123/125

“A

conform

ingim

plem

entatio

nisreq

uired

todocumentits

choice

ofbehavior

ineach

oftheareas

listedin

thissubclau

se.Thefollow

ingare

implem

entatio

n-defined:”

Page 34: swt.informatik.uni-freiburg.de · – 2014-04 – main – F o r m a l M e t hod s f o r C S e m i na r – Su mm e r S e m e s t e r 2014 D a n i e l D i e t s c h, S e rg i o F

Implem

enta

tion-D

efined

Beh

avio

ur

(J.3)

– 2014-04 – pitfalls –

123/125

“A

conform

ingim

plem

entatio

nisreq

uired

todocumentits

choice

ofbehavior

ineach

oftheareas

listedin

thissubclau

se.Thefollow

ingare

implem

entatio

n-defined:”

•J.3.2

Environ

ment,e.g.

Theset

ofsign

als,their

semantics,

andtheir

defau

lthandling(7.14).

Implem

enta

tion-D

efined

Beh

avio

ur

(J.3)

– 2014-04 – pitfalls –

123/125

“A

conform

ingim

plem

entatio

nisreq

uired

todocumentits

choice

ofbehavior

ineach

oftheareas

listedin

thissubclau

se.Thefollow

ingare

implem

entatio

n-defined:”

•J.3.2

Environ

ment,e.g.

Theset

ofsign

als,their

semantics,

andtheir

defau

lthandling(7.14).

•J.3.3

Identifi

ers,e.g.

Thenumber

ofsign

ifican

tinitial

characters

inan

identifi

er(5.2.4.1,

6.4.2).

Implem

enta

tion-D

efined

Beh

avio

ur

(J.3)

– 2014-04 – pitfalls –

123/125

“A

conform

ingim

plem

entatio

nisreq

uired

todocumentits

choice

ofbehavior

ineach

oftheareas

listedin

thissubclau

se.Thefollow

ingare

implem

entatio

n-defined:”

•J.3.2

Environ

ment,e.g.

Theset

ofsign

als,their

semantics,

andtheir

defau

lthandling(7.14).

•J.3.3

Identifi

ers,e.g.

Thenumber

ofsign

ifican

tinitial

characters

inan

identifi

er(5.2.4.1,

6.4.2).

•J.3.4

Characters,

e.g.Thenumber

ofbits

inabyte

(3.6).

Implem

enta

tion-D

efined

Beh

avio

ur

(J.3)

– 2014-04 – pitfalls –

123/125

“A

conform

ingim

plem

entatio

nisreq

uired

todocumentits

choice

ofbehavior

ineach

oftheareas

listedin

thissubclau

se.Thefollow

ingare

implem

entatio

n-defined:”

•J.3.2

Environ

ment,e.g.

Theset

ofsign

als,their

semantics,

andtheir

defau

lthandling(7.14).

•J.3.3

Identifi

ers,e.g.

Thenumber

ofsign

ifican

tinitial

characters

inan

identifi

er(5.2.4.1,

6.4.2).

•J.3.4

Characters,

e.g.Thenumber

ofbits

inabyte

(3.6).

•J.3.5

Integers,

e.g.Anyexten

ded

integer

types

that

existin

theim

plem

entation

(6.2.5).

Implem

enta

tion-D

efined

Beh

avio

ur

(J.3)

– 2014-04 – pitfalls –

123/125

“A

conform

ingim

plem

entatio

nisreq

uired

todocumentits

choice

ofbehavior

ineach

oftheareas

listedin

thissubclau

se.Thefollow

ingare

implem

entatio

n-defined:”

•J.3.2

Environ

ment,e.g.

Theset

ofsign

als,their

semantics,

andtheir

defau

lthandling(7.14).

•J.3.3

Identifi

ers,e.g.

Thenumber

ofsign

ifican

tinitial

characters

inan

identifi

er(5.2.4.1,

6.4.2).

•J.3.4

Characters,

e.g.Thenumber

ofbits

inabyte

(3.6).

•J.3.5

Integers,

e.g.Anyexten

ded

integer

types

that

existin

theim

plem

entation

(6.2.5).

•J.3.6

Floatin

gPoin

t,e.g.

Theaccu

racyof

thefloatin

g-poin

top

erations[...]

(5.2.4.2.2).

Implem

enta

tion-D

efined

Beh

avio

ur

(J.3)

– 2014-04 – pitfalls –

123/125

“A

conform

ingim

plem

entatio

nisreq

uired

todocumentits

choice

ofbehavior

ineach

oftheareas

listedin

thissubclau

se.Thefollow

ingare

implem

entatio

n-defined:”

•J.3.2

Environ

ment,e.g.

Theset

ofsign

als,their

semantics,

andtheir

defau

lthandling(7.14).

•J.3.3

Identifi

ers,e.g.

Thenumber

ofsign

ifican

tinitial

characters

inan

identifi

er(5.2.4.1,

6.4.2).

•J.3.4

Characters,

e.g.Thenumber

ofbits

inabyte

(3.6).

•J.3.5

Integers,

e.g.Anyexten

ded

integer

types

that

existin

theim

plem

entation

(6.2.5).

•J.3.6

Floatin

gPoin

t,e.g.

Theaccu

racyof

thefloatin

g-poin

top

erations[...]

(5.2.4.2.2).

•J.3.7

Arrays

andPoin

ters,e.g.

Theresu

ltof

convertin

gapoin

terto

aninteger

orvice

versa(6.3.2.3).

Page 35: swt.informatik.uni-freiburg.de · – 2014-04 – main – F o r m a l M e t hod s f o r C S e m i na r – Su mm e r S e m e s t e r 2014 D a n i e l D i e t s c h, S e rg i o F

Implem

enta

tion-D

efined

Beh

avio

ur

(J.3)

– 2014-04 – pitfalls –

123/125

“A

conform

ingim

plem

entatio

nisreq

uired

todocumentits

choice

ofbehavior

ineach

oftheareas

listedin

thissubclau

se.Thefollow

ingare

implem

entatio

n-defined:”

•J.3.2

Environ

ment,e.g.

Theset

ofsign

als,their

semantics,

andtheir

defau

lthandling(7.14).

•J.3.3

Identifi

ers,e.g.

Thenumber

ofsign

ifican

tinitial

characters

inan

identifi

er(5.2.4.1,

6.4.2).

•J.3.4

Characters,

e.g.Thenumber

ofbits

inabyte

(3.6).

•J.3.5

Integers,

e.g.Anyexten

ded

integer

types

that

existin

theim

plem

entation

(6.2.5).

•J.3.6

Floatin

gPoin

t,e.g.

Theaccu

racyof

thefloatin

g-poin

top

erations[...]

(5.2.4.2.2).

•J.3.7

Arrays

andPoin

ters,e.g.

Theresu

ltof

convertin

gapoin

terto

aninteger

orvice

versa(6.3.2.3).

•etc.

pp.

Loca

leand

Com

mon

Exten

sions

(J.4,J.5

)

– 2014-04 – pitfalls –

124/125

•J.4

Locale-sp

ecificbehavio

ur

•J.5

Commonexten

sions

“Thefollow

ingexten

sionsare

widely

used

inmanysystem

s,butare

not

portab

leto

allim

plem

entatio

ns.”

Referen

ces

– 2014-04 – main –

125/125

[ISO,1999]ISO

(1999).

Program

minglan

guages

–C.Tech

nical

Report

ISO/IEC9899:1999,ISO.Seco

ndeditio

n,1999-12-01.