xvi Contents 9.11 Common Memory-Related Bugs in C Progiams 870. 9.11.1 Dereferencing Bad Pointers 870 9.11.2 Reading Uninitialized Memory 871 9.11.3 Allowing Stack Buffer Overflows 871 9.11.4 Assuming That Pointers and the Objects They Point to Are the Same Size 872 9.11.5 Making Off-by-One Errors 872 9.11.6 Referencing a Pointer Instead of the Object It Points To 873 9.11.7 Misunderstanding Pointer Arithmetic 873 9.11.8 Referencing Nonexistent Variables 874 9.11.9 Referencing Data in Free Heap Blocks 874 9.11.10 Introducing Memory Leaks 875 9.12 Summary 875 Bibliographic Notes 876 Homework Problems 876 Solutions to Practice Problems 880 Part Ill Interaction and Communication between Programs 10 System-Level I/O 889 10.1Unix/O890 10.2 Files 891 10.3 Opening and Closing Files 893 10.4 Reading and Writing Files 895 10.5 Robust Reading and Writing with the Rio Package 897 10.5.1 RIo Unbuffered Input and Output Functions 897 10.5.2 RIo Buffered Input Functions 898 10.6 Reading File Metadata 903 10.7 Reading Directory Contents 905 10.8 Sharing Files 906 10.9 1/O Redirection 909 10.10 Standard I/O 911 10.11 Putting It Together:Which I/O Functions Should I Use?911 10.12 Summary 913 Bibliographic Notes 914 Homework Problems 914 Solutions to Practice Problems 915xvi Conterits 9.11 Common Memory-Related Bugs in-CPrqgfams 870. 9.11.1 Dereferencing Bad Pointers 870 9.11.2 Reading Uninitialized Memory 871 9.11.3 Allowing Stack Buffer Overflows 871 9.11.4 Assuming That Pointers and the Objects They Point to Are the Same Size 872 9.11.5 Making Off-by-One Errors 872 9.11.6 Referencing a Pointer Instead of the Object It Points f'o 873 9.11.7 'Misunderstanding Pointer Arithmetic &73 9.11.8 Referencing Nonexistent Variables 874 9.11.9 Referencing Data in Free Heap Blocks 874 9.11.10 Introducing Memory Leaks 875. 9.12 Summary 875 Bibliographic Notes 876 Homework Problems 876 Solutions to Practice Pr6blems 880' Part Ill Interaction and Communicatiqn between Programs 10 System-Level I/O 889 10.1 Unix I/O S90 10.2 Files 891 10.3 Opening and Closing Files 893 10.4 Reading and Writing Files 895 10.5 Robust Reading and Writing with the Rm Pack~ge 897_ 10.5.1 Rm Unbuffered Input and Output Functions 897 10.5.2 Rio Buffered Input Function§ 898 10.6 Reading File Metadata 903 10.7 Reading Directory Contents 905 10.8 Sharing Files 206 10.9 I/O Redirection 909 10.10 Standard I/O 911 10.11 Putting It Together: Which I/O Functions Shoul,d I Use? 911 10.12 Summary 913 Bibliographic Notes 914 Homework Problems 914 Solutions to Practice Problems 915