Programming in C. 4. Misc. Library Features, Gotchas, Hints and Tips. Dr. Neel Krishnaswami University of Cambridge

Similar documents
Programming in C and C++

Programming in C. 3. Pointers and Structures. Dr. Neel Krishnaswami University of Cambridge

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

Programming in C and C++

This code has a bug that allows a hacker to take control of its execution and run evilfunc().

Chapter 5, Standard I/O. Not UNIX... C standard (library) Why? UNIX programmed in C stdio is very UNIX based

Jacobs University Bremen February 7 th, 2017 Dr. Kinga Lipskoch. Practice Sheet. First Name:... Last Name:... Matriculation Number:...

Getting Started. Project 1

Input/Output and the Operating Systems

Programming in C and C++

C and C++ 1. Types Variables Expressions & Statements. Andrew W. Moore

Understanding Pointers

Programming in C. Lecture 5: Aliasing, Graphs, and Deallocation. Dr Neel Krishnaswami. Michaelmas Term University of Cambridge

Lecture 8 Dynamic Memory Allocation

C and C++ 1. Types Variables Expressions & Statements. Andrew W. Moore

Stream Model of I/O. Basic I/O in C

Slide Set 8. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

[6 marks] All parts of this question assume the following C statement. Parts (b) through (e) assume a variable called ptrs.

Introduction to C Programming (Part C) Copyright 2008 W. W. Norton & Company. All rights Reserved

Student Number: Instructor: Reid Section: L5101 (6:10-7:00pm)

Content. Input Output Devices File access Function of File I/O Redirection Command-line arguments

Exercise Session 3 Systems Programming and Computer Architecture

Memory (Stack and Heap)

Standard I/O in C, Computer System and programming in C

C and C++ 2. Functions Preprocessor. Alan Mycroft

CS 261 Fall Mike Lam, Professor. Structs and I/O

o Code, executable, and process o Main memory vs. virtual memory

Midterm Review. What makes a file executable? Dr. Jack Lange 2/10/16. In UNIX: Everything is a file

Programming in C and C++

Programming in C and C++

Chapter 14. Dynamic Data Structures. Instructor: Öğr. Gör. Okan Vardarlı. Copyright 2004 Pearson Addison-Wesley. All rights reserved.

Kurt Schmidt. October 30, 2018

COMP 2355 Introduction to Systems Programming

25.2 Opening and Closing a File

Variation of Pointers

File Access. FILE * fopen(const char *name, const char * mode);

Memory Allocation in C C Programming and Software Tools. N.C. State Department of Computer Science

ECE551 Midterm Version 1

Memory Management. CSC215 Lecture

Advanced Pointer Topics

ECE551 Midterm Version 2

C for Java Programmers 1. Last Week. Overview of the differences between C and Java. The C language (keywords, types, functies, etc.

Dynamic memory. EECS 211 Winter 2019

Dynamic Data Structures. CSCI 112: Programming in C

Dynamic memory allocation

Class Information ANNOUCEMENTS

Content. In this chapter, you will learn:

CS C Primer. Tyler Szepesi. January 16, 2013

ECE 264 Exam 2. 6:30-7:30PM, March 9, You must sign here. Otherwise you will receive a 1-point penalty.

Chapter 2 (Dynamic variable (i.e. pointer), Static variable)

CMPS 105 Systems Programming. Prof. Darrell Long E2.371

C-Refresher: Session 10 Disk IO

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

CMPE-013/L. File I/O. File Processing. Gabriel Hugh Elkaim Winter File Processing. Files and Streams. Outline.

C for C++ Programmers

Structures and Pointers

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

Heap Arrays. Steven R. Bagley

System Programming. Standard Input/Output Library (Cont d)

C Structures & Dynamic Memory Management

Quick review of previous lecture Ch6 Structure Ch7 I/O. EECS2031 Software Tools. C - Structures, Unions, Enums & Typedef (K&R Ch.

Lab # 4. Files & Queues in C

ECE551 Midterm. There are 7 questions, with the point values as shown below. You have 75 minutes with a total of 75 points. Pace yourself accordingly.

High Performance Programming Programming in C part 1

C++ C and C++ C++ fundamental types. C++ enumeration. To quote Bjarne Stroustrup: 5. Overloading Namespaces Classes

Student Number: Instructor: Reid Section: L0201 (12:10-1:00pm)

Review: Constants. Modules and Interfaces. Modules. Clients, Interfaces, Implementations. Client. Interface. Implementation

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Computers Programming Course 5. Iulian Năstac

Deep C. Multifile projects Getting it running Data types Typecasting Memory management Pointers. CS-343 Operating Systems

(heavily based on last year s notes (Andrew Moore) with thanks to Alastair R. Beresford. 1. Types Variables Expressions & Statements 2/23

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

THE C STANDARD LIBRARY & MAKING YOUR OWN LIBRARY. ISA 563: Fundamentals of Systems Programming

Character Strings. String-copy Example

UNIT-V CONSOLE I/O. This section examines in detail the console I/O functions.

CS360 Midterm 2 Spring, 2016 James S. Plank March 10, 2016

CS24 Week 2 Lecture 1

Signature: ECE 551 Midterm Exam

C FILE Type. Basic I/O in C. Accessing a stream requires a pointer variable of type FILE.

Student Number: Instructor: Reid Section: L0101 (10:10-11:00am)

CSC 1600 Memory Layout for Unix Processes"

ECE551 Midterm Version 1

PROGRAMMAZIONE I A.A. 2017/2018

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Dynamic Memory Allocation (and Multi-Dimensional Arrays)

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

Processes. Johan Montelius KTH

C PROGRAMMING Lecture 6. 1st semester

Dynamic Memory Management. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36

Administrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6

ch = argv[i][++j]; /* why does ++j but j++ does not? */

A process. the stack

File IO and command line input CSE 2451

ANITA S SUPER AWESOME RECITATION SLIDES

C Pointers. Abdelghani Bellaachia, CSCI 1121 Page: 1

Fundamental of Programming (C)

Lecture 03 Bits, Bytes and Data Types

Final exam. Scores. Fall term 2012 KAIST EE209 Programming Structures for EE. Thursday Dec 20, Student's name: Student ID:

Transcription:

Programming in C 4. Misc. Library Features, Gotchas, Hints and Tips Dr. Neel Krishnaswami University of Cambridge (based on notes from and with thanks to Anil Madhavapeddy, Alan Mycroft, Alastair Beresford and Andrew Moore) Michaelmas Term 2017-2018

Example 52 00... 00 00... 1c 00 41 Little... 00 00 42 00 41 Big 05 62 0x2c 0x30 0x34 0x38 0x4c 0x50 0x60 int **ppi int *pi int i char *pc char c 00 00 38 4c 2 / 24

Uses of const and volatile Any declaration can be prefixed with const or volatile A const variable can only be assigned a value when it is defined The const declaration can also be used for parameters in a function definition The volatile keyword can be used to state that a variable may be changed by hardware or the kernel. For example, the volatile keyword may prevent unsafe compiler optimisations for memory-mapped input/output The use of pointers and the const keyword is quite subtle: const int *p is a pointer to a const int int const *p is also a pointer to a const int int *const p is a const pointer to an int const int *const p is a const pointer to a const int 3 / 24

Example 1 int main(void) { 2 int i = 42; 3 int j = 28; 4 5 const int *pc = &i; //Also: "int const *pc" 6 *pc = 41; //Wrong 7 pc = &j; 8 9 int *const cp = &i; 10 *cp = 41; 11 cp = &j; //Wrong 12 13 const int *const cpc = &i; 14 *cpc = 41; //Wrong 15 cpc = &j; //Wrong 16 return 0; 17 } 4 / 24

Typedefs The typedef operator, creates a synonym for a data type; for example, typedef unsigned int Radius; Once a new data type has been created, it can be used in place of the usual type name in declarations and casts; for example, Radius r = 5;...; r = (Radius) rshort; A typedef declaration does not create a new type It just creates a synonym for an existing type A typedef is particularly useful with structures and unions: 1 typedef struct llist *llptr; 2 typedef struct llist { 3 int val; 4 llptr next; 5 } linklist; 5 / 24

Inline functions A function in C can be declared inline; for example: 1 inline int fact(unsigned int n) { 2 return n? n*fact(n-1) : 1; 3 } The compiler will then try to inline the function A clever compiler might generate 120 for fact(5) A compiler might not always be able to inline a function An inline function must be defined in the same execution unit as it is used The inline operator does not change function semantics the inline function itself still has a unique address static variables of an inline function still have a unique address Both inline and register are largely unnecessary with modern compilers and hardware 6 / 24

That s it! We have now explored most of the C language The language is quite subtle in places; in particular watch out for: operator precedence pointer assignment (particularly function pointers) implicit casts between ints of different sizes and chars There is also extensive standard library support, including: shell and file I/O (stdio.h) dynamic memory allocation (stdlib.h) string manipulation (string.h) character class tests (ctype.h)... (Read, for example, K&R Appendix B for a quick introduction) (Or type man function at a Unix shell for details) 7 / 24

Library support: I/O I/O is not managed directly by the compiler; support in stdio.h: FILE *stdin, *stdout, *stderr; int printf(const char *format,...); int sprintf(char *str, const char *format,...); int fprintf(file *stream, const char *format,...); int scanf(const char *format,...); // sscanf,fscanf FILE *fopen(const char *path, const char *mode); int fclose(file *fp); size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream); size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream); 8 / 24

1 #include<stdio.h> 2 #define BUFSIZE 1024 3 4 int main(void) { 5 FILE *fp; 6 char buffer[bufsize]; 7 8 if ((fp=fopen("somefile.txt","rb")) == 0) { 9 perror("fopen error:"); 10 return 1; 11 } 12 13 while(!feof(fp)) { 14 int r = fread(buffer,sizeof(char),bufsize,fp); 15 fwrite(buffer,sizeof(char),r,stdout); 16 } 17 18 fclose(fp); 19 return 0; 20 } 9 / 24

Library support: dynamic memory allocation Dynamic memory allocation is not managed directly by the C compiler Support is available in stdlib.h: void *malloc(size_t size) void *calloc(size_t nobj, size_t size) void *realloc(void *p, size_t size) void free(void *p) The C sizeof unary operator is handy when using malloc: p = (char *) malloc(sizeof(char)*1000) Any successfully allocated memory must be deallocated manually Note: free() needs the pointer to the allocated memory Failure to deallocate will result in a memory leak 10 / 24

Gotchas: operator precedence 1 #include<stdio.h> 2 3 struct test {int i;}; 4 typedef struct test test_t; 5 6 int main(void) { 7 8 test_t a,b; 9 test_t *p[] = {&a,&b}; 10 p[0]->i=0; 11 p[1]->i=0; 12 test_t *q = p[0]; 13 14 printf("%d\n",++q->i); //What does this do? 15 16 return 0; 17 } 11 / 24

Gotchas: i++ 1 #include <stdio.h> 2 3 int main(void) { 4 5 int i=2; 6 int j=i++ + ++i; 7 printf("%d %d\n",i,j); //What does this print? 8 9 return 0; 10 } Expressions like i++ + ++i are known as grey (or gray) expressions in that their meaning is compiler dependent in C (even if they are defined in Java) 12 / 24

Gotchas: local stack 1 #include <stdio.h> 2 3 char *unary(unsigned short s) { 4 char local[s+1]; 5 int i; 6 for (i=0;i<s;i++) local[i]= 1 ; 7 local[s]= \0 ; 8 return local; 9 } 10 11 int main(void) { 12 printf("%s\n",unary(6)); //What does this print? 13 return 0; 14 } 13 / 24

Gotchas: local stack (contd.) 1 #include <stdio.h> 2 3 char global[10]; 4 5 char *unary(unsigned short s) { 6 char local[s+1]; 7 char *p = s%2? global : local; 8 int i; 9 for (i=0;i<s;i++) p[i]= 1 ; 10 p[s]= \0 ; 11 return p; 12 } 13 14 int main(void) { 15 printf("%s\n",unary(6)); //What does this print? 16 return 0; 17 } 14 / 24

Gotchas: careful with pointers 1 #include <stdio.h> 2 3 struct values { int a; int b; }; 4 5 int main(void) { 6 struct values test2 = {2,3}; 7 struct values test1 = {0,1}; 8 9 int *pi = &(test1.a); 10 pi += 1; //Is this sensible? 11 printf("%d\n",*pi); 12 pi += 2; //What could this point at? 13 printf("%d\n",*pi); 14 15 return 0; 16 } 15 / 24

Gotchas: XKCD pointers 16 / 24

Tricks: Duff s device 1 boring_send(int *to, int *from, int count) { 2 do { 3 *to = *from++; 4 } while(--count > 0); 5 } 6 7 send(int *to, int *from, int count) { 8 int n = (count+7)/8; 9 switch (count%8) { 10 case 0: do{ *to = *from++; 11 case 7: *to = *from++; 12 case 6: *to = *from++; 13 case 5: *to = *from++; 14 case 4: *to = *from++; 15 case 3: *to = *from++; 16 case 2: *to = *from++; 17 case 1: *to = *from++; 18 } while(--n>0); 19 } 17 / 24