56
APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 24 March 16, 2012

Lecture 24 - dgp.toronto.edujdeber/aps105h/201201/lectures/Lecture24/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 24 March 16, 2012

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Lecture 24 - dgp.toronto.edujdeber/aps105h/201201/lectures/Lecture24/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 24 March 16, 2012

APS105Winter 2012

Jonathan Deberjdeber -at- cs -dot- toronto -dot- edu

Lecture 24March 16, 2012

Page 2: Lecture 24 - dgp.toronto.edujdeber/aps105h/201201/lectures/Lecture24/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 24 March 16, 2012

Today

• (Even More) Dynamic Memory Allocation

2

Page 3: Lecture 24 - dgp.toronto.edujdeber/aps105h/201201/lectures/Lecture24/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 24 March 16, 2012

Dynamic Memory Allocation

3

!"##$%&' and friends

Page 4: Lecture 24 - dgp.toronto.edujdeber/aps105h/201201/lectures/Lecture24/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 24 March 16, 2012

Automatic Allocation

4

• We’ve been doing this since the first week

• Variables have a name and an address

()*+(,(+-+.,

.

/ 0

!"

#"

$"

()*+"102,"132+-+/,"142+-+0,

()*+56+-+7(,

Page 5: Lecture 24 - dgp.toronto.edujdeber/aps105h/201201/lectures/Lecture24/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 24 March 16, 2012

!"##$%&'

• “Memory Allocator”

• 8()%#9:;+<=*:#(>?@A

• You ask !"##$%&' for some memory, it finds some, and then gives it to you

• B$(:+5 pointer (“generic” pointer)

• Different pool of memory (called the heap)

5

B$(:+5!"##$%&=(C;D*+=(C;',

for now think of it as ()*

Page 6: Lecture 24 - dgp.toronto.edujdeber/aps105h/201201/lectures/Lecture24/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 24 March 16, 2012

6

!"##$%&4',

B$(:+5

%@"E+5%6+-+

!"##$%&4',%@"E+%+-+

()*+(+-+.,()*+F+-+7(,

GHG

%#"

Dotted border means “dynamically allocated”

5%6+-+GHG,

}Wrong

Page 7: Lecture 24 - dgp.toronto.edujdeber/aps105h/201201/lectures/Lecture24/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 24 March 16, 2012

7

()*+5(6+-+!"##$%&I++

%@"E is 4 byte (everywhere)()* is 0 bytes (on ECF)

%#"

!"##$%&I',%@"E+5%6+-+

!#"

number of bytes

number of elements size of each element

',5+0

Page 8: Lecture 24 - dgp.toronto.edujdeber/aps105h/201201/lectures/Lecture24/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 24 March 16, 2012

!"#$%&'( and )*++%,'(

8

()*+5(6+-+!"##$%&I+5+=(C;$J&()*'',

You should always use =(C;$J&'

()*+5(6+-+!"##$%&I+5+0',

%@"E+5%6+-+!"##$%&I',

%@"E+5%6+-+!"##$%&I+5+=(C;$J&%@"E'',

Page 9: Lecture 24 - dgp.toronto.edujdeber/aps105h/201201/lectures/Lecture24/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 24 March 16, 2012

9

()*+!"()+&B$(:'K++()*+(+-+/,++()*+5F+-+=L9"E;I&7(',++6E()*J&MN:MO+5F',++++E;*9E)+3,P

!"()&'

"- .-

/-

=L9"E;I&'

0$!1+2-

3

()*+5=L9"E;I&()*+56'K++()*+5E;=9#*+-+!"##$%&=(C;$J&()*'',++5E;=9#*+-+56+5+56,++E;*9E)+E;=9#*,P

Page 10: Lecture 24 - dgp.toronto.edujdeber/aps105h/201201/lectures/Lecture24/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 24 March 16, 2012

9

()*+!"()+&B$(:'K++()*+(+-+/,++()*+5F+-+=L9"E;I&7(',++6E()*J&MN:MO+5F',++++E;*9E)+3,P

!"()&'

"- .-

/-

=L9"E;I&'

0$!1+2-

3

()*+5=L9"E;I&()*+56'K++()*+5E;=9#*+-+!"##$%&=(C;$J&()*'',++5E;=9#*+-+56+5+56,++E;*9E)+E;=9#*,P

Page 11: Lecture 24 - dgp.toronto.edujdeber/aps105h/201201/lectures/Lecture24/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 24 March 16, 2012

9

()*+!"()+&B$(:'K++()*+(+-+/,++()*+5F+-+=L9"E;I&7(',++6E()*J&MN:MO+5F',++++E;*9E)+3,P

!"()&'

"- .-

/-

=L9"E;I&'

0$!1+2-

3

()*+5=L9"E;I&()*+56'K++()*+5E;=9#*+-+!"##$%&=(C;$J&()*'',++5E;=9#*+-+56+5+56,++E;*9E)+E;=9#*,P

0

Page 12: Lecture 24 - dgp.toronto.edujdeber/aps105h/201201/lectures/Lecture24/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 24 March 16, 2012

9

()*+!"()+&B$(:'K++()*+(+-+/,++()*+5F+-+=L9"E;I&7(',++6E()*J&MN:MO+5F',++++E;*9E)+3,P

!"()&'

"- .-

=L9"E;I&'

0$!1+2-

3

()*+5=L9"E;I&()*+56'K++()*+5E;=9#*+-+!"##$%&=(C;$J&()*'',++5E;=9#*+-+56+5+56,++E;*9E)+E;=9#*,P

0

Page 13: Lecture 24 - dgp.toronto.edujdeber/aps105h/201201/lectures/Lecture24/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 24 March 16, 2012

9

()*+!"()+&B$(:'K++()*+(+-+/,++()*+5F+-+=L9"E;I&7(',++6E()*J&MN:MO+5F',++++E;*9E)+3,P

!"()&'

"- .-

=L9"E;I&'

0$!1+2-

3

()*+5=L9"E;I&()*+56'K++()*+5E;=9#*+-+!"##$%&=(C;$J&()*'',++5E;=9#*+-+56+5+56,++E;*9E)+E;=9#*,P

0

Page 14: Lecture 24 - dgp.toronto.edujdeber/aps105h/201201/lectures/Lecture24/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 24 March 16, 2012

9

()*+!"()+&B$(:'K++()*+(+-+/,++()*+5F+-+=L9"E;I&7(',++6E()*J&MN:MO+5F',++++E;*9E)+3,P

!"()&'

"- .-

=L9"E;I&'

3

()*+5=L9"E;I&()*+56'K++()*+5E;=9#*+-+!"##$%&=(C;$J&()*'',++5E;=9#*+-+56+5+56,++E;*9E)+E;=9#*,P

0

Page 15: Lecture 24 - dgp.toronto.edujdeber/aps105h/201201/lectures/Lecture24/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 24 March 16, 2012

9

()*+!"()+&B$(:'K++()*+(+-+/,++()*+5F+-+=L9"E;I&7(',++6E()*J&MN:MO+5F',++++E;*9E)+3,P

!"()&'

"- .-3

()*+5=L9"E;I&()*+56'K++()*+5E;=9#*+-+!"##$%&=(C;$J&()*'',++5E;=9#*+-+56+5+56,++E;*9E)+E;=9#*,P

0

Page 16: Lecture 24 - dgp.toronto.edujdeber/aps105h/201201/lectures/Lecture24/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 24 March 16, 2012

9

()*+!"()+&B$(:'K++()*+(+-+/,++()*+5F+-+=L9"E;I&7(',++6E()*J&MN:MO+5F',++++E;*9E)+3,P

!"()&'

"- .-3

()*+5=L9"E;I&()*+56'K++()*+5E;=9#*+-+!"##$%&=(C;$J&()*'',++5E;=9#*+-+56+5+56,++E;*9E)+E;=9#*,P

0

Page 17: Lecture 24 - dgp.toronto.edujdeber/aps105h/201201/lectures/Lecture24/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 24 March 16, 2012

9

()*+!"()+&B$(:'K++()*+(+-+/,++()*+5F+-+=L9"E;I&7(',++6E()*J&MN:MO+5F',++++E;*9E)+3,P

!"()&'

"- .-3

()*+5=L9"E;I&()*+56'K++()*+5E;=9#*+-+!"##$%&=(C;$J&()*'',++5E;=9#*+-+56+5+56,++E;*9E)+E;=9#*,P

0

Page 18: Lecture 24 - dgp.toronto.edujdeber/aps105h/201201/lectures/Lecture24/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 24 March 16, 2012

9

()*+!"()+&B$(:'K++()*+(+-+/,++()*+5F+-+=L9"E;I&7(',++6E()*J&MN:MO+5F',++++E;*9E)+3,P

!"()&'

"- .-30

()*+5=L9"E;I&()*+56'K++()*+5E;=9#*+-+!"##$%&=(C;$J&()*'',++5E;=9#*+-+56+5+56,++E;*9E)+E;=9#*,P

Stack

Heap 0

Page 19: Lecture 24 - dgp.toronto.edujdeber/aps105h/201201/lectures/Lecture24/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 24 March 16, 2012

10

Cleaning Up!"#$!$%$&'!"#$($%$)'!"#$*+$%$,!'

&!"

#"

)$"

Page 20: Lecture 24 - dgp.toronto.edujdeber/aps105h/201201/lectures/Lecture24/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 24 March 16, 2012

10

Cleaning Up()*+(+-+0,()*+F+-+.,()*+56+-+7(,

0!"

#"

.&"

6+-+7F,

Page 21: Lecture 24 - dgp.toronto.edujdeber/aps105h/201201/lectures/Lecture24/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 24 March 16, 2012

10

Cleaning Up()*+(+-+0,()*+F+-+.,()*+56+-+7(,

()*+5(6+-+!"##$%&=(C;$J&()*'',()*+5F6+-+!"##$%&=(C;$J&()*'',5(6+-+0,5F6+-+.,

0!"

#"

.&"

6+-+7F,

0 .

!#" &#"

Page 22: Lecture 24 - dgp.toronto.edujdeber/aps105h/201201/lectures/Lecture24/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 24 March 16, 2012

10

Cleaning Up()*+(+-+0,()*+F+-+.,()*+56+-+7(,

()*+5(6+-+!"##$%&=(C;$J&()*'',()*+5F6+-+!"##$%&=(C;$J&()*'',5(6+-+0,5F6+-+.,

0!"

#"

.&"

6+-+7F,

0 .

!#"

(6+-+F6,

&#"

Page 23: Lecture 24 - dgp.toronto.edujdeber/aps105h/201201/lectures/Lecture24/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 24 March 16, 2012

10

Cleaning Up()*+(+-+0,()*+F+-+.,()*+56+-+7(,

()*+5(6+-+!"##$%&=(C;$J&()*'',()*+5F6+-+!"##$%&=(C;$J&()*'',5(6+-+0,5F6+-+., This value is lost

This bit of memory is lost

0!"

#"

.&"

6+-+7F,

0 .

!#"

(6+-+F6,

&#"

Page 24: Lecture 24 - dgp.toronto.edujdeber/aps105h/201201/lectures/Lecture24/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 24 March 16, 2012

11

()*+!"()+&B$(:'K++()*+)+-+4333333333,++++J$E+&()*+(+-+3,+(+<+),+(QQ'++K++++!"##$%&=(C;$J&()*'',++P++++E;*9E)+3,P

!"()&'

"- 455555555

Page 25: Lecture 24 - dgp.toronto.edujdeber/aps105h/201201/lectures/Lecture24/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 24 March 16, 2012

12

Memory Leaks• A memory leak is when you lose memory

• Possible because !"##$%&'ed memory does not have a name, only an address

• Causes your program to use more and more memory

• In C, it’s your responsibility to prevent leaks

• Entirely manual process

()*+5(6+-+!"##$%&=(C;$J&()*'',()*+5F6+-+!"##$%&=(C;$J&()*'',(6+-+F6, This memory was leaked

Page 26: Lecture 24 - dgp.toronto.edujdeber/aps105h/201201/lectures/Lecture24/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 24 March 16, 2012

JE;;&'

• Returns memory to the pool

• It keeps track of the sizes of allocated blocks

• Any memory you get from !"##$%&' must be cleaned up using JE;;&'

13

()*+56+-+!"##$%&=(C;$J&()*'',???JE;;&6',

B$(:+JE;;&B$(:+56',

Page 27: Lecture 24 - dgp.toronto.edujdeber/aps105h/201201/lectures/Lecture24/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 24 March 16, 2012

Three JE;;&' bugs

• “Use after JE;;&'”

• “Double JE;;&'”

• JE;;&'ing non-!"##$%&'ed memory

14

Page 28: Lecture 24 - dgp.toronto.edujdeber/aps105h/201201/lectures/Lecture24/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 24 March 16, 2012

• Once memory has been &0$$'(ed, you can’t use it again

“Use After Free”

15

()*+56+-+!"##$%&=(C;$J&()*'',#"

Page 29: Lecture 24 - dgp.toronto.edujdeber/aps105h/201201/lectures/Lecture24/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 24 March 16, 2012

• Once memory has been &0$$'(ed, you can’t use it again

“Use After Free”

15

()*+56+-+!"##$%&=(C;$J&()*'',???JE;;&6',

#"

This doesn’t change p itself

Page 30: Lecture 24 - dgp.toronto.edujdeber/aps105h/201201/lectures/Lecture24/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 24 March 16, 2012

• Once memory has been &0$$'(ed, you can’t use it again

“Use After Free”

15

()*+56+-+!"##$%&=(C;$J&()*'',???JE;;&6',

56+-+0,

#"

0This doesn’t change p itself

Wrong

Page 31: Lecture 24 - dgp.toronto.edujdeber/aps105h/201201/lectures/Lecture24/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 24 March 16, 2012

16

#"

()*+56+-+!"##$%&=(C;$J&()*'',()*+5L+-+6,

'"

Page 32: Lecture 24 - dgp.toronto.edujdeber/aps105h/201201/lectures/Lecture24/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 24 March 16, 2012

16

!"

!"#$%&$'$()**+,-.!/0+1-!"#223!"#$%4$'$&35551600-&23

#"

77$8)90$.:60$"+#$#+$:.0$&$)";(+60<

Page 33: Lecture 24 - dgp.toronto.edujdeber/aps105h/201201/lectures/Lecture24/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 24 March 16, 2012

16

#"

0

()*+56+-+!"##$%&=(C;$J&()*'',()*+5L+-+6,???JE;;&6',

'"

RR+S"T;+=9E;+)$*+*$+9=;+6+")U!$E;V

5L+-+0, Wrong

Page 34: Lecture 24 - dgp.toronto.edujdeber/aps105h/201201/lectures/Lecture24/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 24 March 16, 2012

“Double Free”

• You can only JE;;&' memory once

17

()*+56+-+!"##$%&=(C;$J&()*'',

???

JE;;&6',

JE;;&6', Wrong

Page 35: Lecture 24 - dgp.toronto.edujdeber/aps105h/201201/lectures/Lecture24/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 24 March 16, 2012

“Double Free”

• You can only JE;;&' memory once

17

()*+56+-+!"##$%&=(C;$J&()*'',

???

JE;;&6',

()*+5L+-+6,

JE;;&6',

JE;;&L',

Wrong

Wrong

Page 36: Lecture 24 - dgp.toronto.edujdeber/aps105h/201201/lectures/Lecture24/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 24 March 16, 2012

Non-)*++%,'(ed Memory

• You can only JE;;&' memory obtained from !"##$%&'

18

()*+"12+-+KWO+XO+YO+ZP,

JE;;&"', Wrong

()*+(+-+43,

()*+56+-+7(,

JE;;&6', Wrong

Page 37: Lecture 24 - dgp.toronto.edujdeber/aps105h/201201/lectures/Lecture24/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 24 March 16, 2012

!"##$%&' Summary

• !"##$%&' gives you memory from a separate pool of memory called the heap

• This memory exists outside of functions

• Returns a generic B$(:+5 pointer, must be stored in a pointer variable

• Parameter is the number of bytes to allocate (=(C;$J&' is your friend)

19

Page 38: Lecture 24 - dgp.toronto.edujdeber/aps105h/201201/lectures/Lecture24/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 24 March 16, 2012

JE;;&' Summary

• All memory obtained from )*++%,'( must eventually get passed to &0$$'(

• You can’t use that memory after it’s been &0$$'(ed

• You can’t call &0$$'( more than once on the same chunk of memory

• You can’t &0$$'( memory you didn’t get from )*++%,'(

• C being C, all of the above will compile, but are undefined behaviours, and may crash horribly

20

Page 39: Lecture 24 - dgp.toronto.edujdeber/aps105h/201201/lectures/Lecture24/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 24 March 16, 2012

[\]]• A special pointer value that doesn’t point at anything

• Not the same as being uninitialized!

• Need to 8()%#9:; one of the common libraries (e.g., =*:($?@ or =*:#(>?@)

21

()*+(+-+W,()*+56,

#"

(!"

(6 is uninitialized)

Page 40: Lecture 24 - dgp.toronto.edujdeber/aps105h/201201/lectures/Lecture24/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 24 March 16, 2012

[\]]• A special pointer value that doesn’t point at anything

• Not the same as being uninitialized!

• Need to 8()%#9:; one of the common libraries (e.g., =*:($?@ or =*:#(>?@)

21

()*+(+-+W,()*+56,

#"

(!"

(6 is uninitialized)^

Page 41: Lecture 24 - dgp.toronto.edujdeber/aps105h/201201/lectures/Lecture24/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 24 March 16, 2012

[\]]• A special pointer value that doesn’t point at anything

• Not the same as being uninitialized!

• Need to 8()%#9:; one of the common libraries (e.g., =*:($?@ or =*:#(>?@)

21

()*+(+-+W,()*+56,

#"

(!"

(6 is uninitialized)

Page 42: Lecture 24 - dgp.toronto.edujdeber/aps105h/201201/lectures/Lecture24/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 24 March 16, 2012

[\]]• A special pointer value that doesn’t point at anything

• Not the same as being uninitialized!

• Need to 8()%#9:; one of the common libraries (e.g., =*:($?@ or =*:#(>?@)

21

()*+(+-+W,()*+56,

6+-+7(,

#"

(!"

(6 is uninitialized)

(6 has the value 7()

Page 43: Lecture 24 - dgp.toronto.edujdeber/aps105h/201201/lectures/Lecture24/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 24 March 16, 2012

[\]]• A special pointer value that doesn’t point at anything

• Not the same as being uninitialized!

• Need to 8()%#9:; one of the common libraries (e.g., =*:($?@ or =*:#(>?@)

21

()*+(+-+W,()*+56,

6+-+7(,6+-+[\]],

#"

(!"

(6 is uninitialized)

(6 has the value 7()(6 has the value [\]])

Page 44: Lecture 24 - dgp.toronto.edujdeber/aps105h/201201/lectures/Lecture24/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 24 March 16, 2012

[\]]• A special pointer value that doesn’t point at anything

• Not the same as being uninitialized!

• Need to 8()%#9:; one of the common libraries (e.g., =*:($?@ or =*:#(>?@)

21

()*+(+-+W,()*+56,

6+-+7(,6+-+[\]],

#"

(!"

(6 is uninitialized)

(6 has the value 7()(6 has the value [\]])

Page 45: Lecture 24 - dgp.toronto.edujdeber/aps105h/201201/lectures/Lecture24/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 24 March 16, 2012

[\]]• A special pointer value that doesn’t point at anything

• Not the same as being uninitialized!

• Need to 8()%#9:; one of the common libraries (e.g., =*:($?@ or =*:#(>?@)

21

()*+(+-+W,()*+56,

6+-+7(,6+-+[\]],

#"

(!"

(6 is uninitialized)

(6 has the value 7()(6 has the value [\]])

Page 46: Lecture 24 - dgp.toronto.edujdeber/aps105h/201201/lectures/Lecture24/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 24 March 16, 2012

[\]] vs. G_3G• A pointer to 6788 is called a null pointer

• The null pointer (6788) is very different than the null character (9:59)

• One is a pointer that doesn’t point to anything, the other is the character with ;<=>> character code 5

22

6`%@"E+56+-+[\]],

%@"E+%+-+9:59, )*+)%`

Page 47: Lecture 24 - dgp.toronto.edujdeber/aps105h/201201/lectures/Lecture24/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 24 March 16, 2012

Following [\]]

• You can’t follow the null pointer

• Well, it’s C, so of course you can, but it’s undefined behaviour

23

()*+56+-+[\]],56+-+X,

#"Wrong

Page 48: Lecture 24 - dgp.toronto.edujdeber/aps105h/201201/lectures/Lecture24/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 24 March 16, 2012

!"##$%&' and [\]]• If !"##$%&' can’t allocate memory, it returns

the null pointer

• Technically, you should always check for this

• In this course, we won’t

24

!,-./$.0.1$223%45./.6!78394!,-::;!9.4$.00.5<==:>..#?!,[email protected]?CD@:;..EE.F$,G28.8??3?.6318F3HI8268>..EE.G3.6318-F!,JI

Page 49: Lecture 24 - dgp.toronto.edujdeber/aps105h/201201/lectures/Lecture24/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 24 March 16, 2012

JE;;&' and [\]]• JE;;&' doesn’t change the pointer it frees

25

()*+56+-+!"##$%&=(C;$J&()*'',56+-+0,

0

*p = 8;

6`

Page 50: Lecture 24 - dgp.toronto.edujdeber/aps105h/201201/lectures/Lecture24/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 24 March 16, 2012

!"##$% and &'((• !"##$% doesn’t change the pointer it frees

25

)*+,-.,/,012234$5)6#3!$)*+%%7-.,/,87999!"##$.%7

*p = 8;

.:

Page 51: Lecture 24 - dgp.toronto.edujdeber/aps105h/201201/lectures/Lecture24/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 24 March 16, 2012

JE;;&' and [\]]• JE;;&' doesn’t change the pointer it frees

• It’s often a good idea to set JE;;&'ed pointers to [\]]

25

()*+56+-+!"##$%&=(C;$J&()*'',56+-+0,???JE;;&6',

*p = 8;

6+-+[\]],

6`

Page 52: Lecture 24 - dgp.toronto.edujdeber/aps105h/201201/lectures/Lecture24/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 24 March 16, 2012

26

JE;;&6',

(J+&6+has not been+JE;;&'ed'K

+++do somethingP

(J+&^^^'K

+++do somethingP

JE;;&6',6+-+[\]],

(J+&6+V-+[\]]'K

+++do somethingP

Page 53: Lecture 24 - dgp.toronto.edujdeber/aps105h/201201/lectures/Lecture24/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 24 March 16, 2012

• JE;;&[\]]' is legal (and does nothing)

27

JE;;&6',???JE;;&6',

JE;;&6',6+-+[\]],???JE;;&6',

JE;;&' and [\]]

L+-+6,???JE;;&6',6+-+[\]],???JE;;&L',

Wrong

Wrong

Not a Problem

Page 54: Lecture 24 - dgp.toronto.edujdeber/aps105h/201201/lectures/Lecture24/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 24 March 16, 2012

Fixed Sizes

• Automatically allocated variables can’t change size

• Until C99, array sizes needed to be fixed at compile time

28

$"()*+"102,

()*+),=%")J&MN:MO+7)',()*+"1)2,

Pre-

C99C99

Page 55: Lecture 24 - dgp.toronto.edujdeber/aps105h/201201/lectures/Lecture24/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 24 March 16, 2012

Fixed Sizes

• Automatically allocated variables can’t change size

• Until C99, array sizes needed to be fixed at compile time

28

$"()*+"102,

()*+),=%")J&MN:MO+7)',()*+"1)2,

Pre-

C99C99

Page 56: Lecture 24 - dgp.toronto.edujdeber/aps105h/201201/lectures/Lecture24/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 24 March 16, 2012

E;"##$%&'

• Resizes a !"##$%&'ed chunk of memory

• Can increase or decrease the size

• Returns a pointer to the resized memory

• 6+-+[\]] is equivalent to !"##$%&'

• =(C;+-+3 is equivalent to JE;;&'

29

B$(:+5E;"##$%&B$(:+56O+=(C;D*+=(C;',

()*+5"+-+!"##$%&0+5+=(C;$J&()*'',

for now think of it as ()*

"+-+E;"##$%&"O+.+5+=(C;$J&()*'',