0x0d2C May your signals all trap May your references be bounded All memory aligned Floats to ints round. remember...

Similar documents
So far, system calls have had easy syntax. Integer, character string, and structure arguments.

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

just a ((somewhat) safer) dialect.

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

Lectures 5-6: Introduction to C

Rule 1-3: Use white space to break a function into paragraphs. Rule 1-5: Avoid very long statements. Use multiple shorter statements instead.

CSci 4061 Introduction to Operating Systems. Programs in C/Unix

Slide Set 5. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

CSE 374 Programming Concepts & Tools. Brandon Myers Winter 2015 C: Linked list, casts, the rest of the preprocessor (Thanks to Hal Perkins)

This resource describes how to program the myrio in C to perform timer interrupts.

A Fast Review of C Essentials Part II

Q1: /8 Q2: /30 Q3: /30 Q4: /32. Total: /100

EL6483: Brief Overview of C Programming Language

CSE 374 Programming Concepts & Tools

A Fast Review of C Essentials Part I

ELEC 377 C Programming Tutorial. ELEC Operating Systems

Supporting Class / C++ Lecture Notes

Kurt Schmidt. October 30, 2018

QUIZ. What are 3 differences between C and C++ const variables?

pthreads CS449 Fall 2017

The C Preprocessor (and more)!

QUIZ. Source:

Introduction to pthreads

BLM2031 Structured Programming. Zeyneb KURT

CSCI 171 Chapter Outlines

Design and development of embedded systems for the Internet of Things (IoT) Fabio Angeletti Fabrizio Gattuso

CSE 374 Programming Concepts & Tools

CS Programming In C

Lectures 5-6: Introduction to C

G52CPP C++ Programming Lecture 6. Dr Jason Atkin

Important From Last Time

Memory Management: The process by which memory is shared, allocated, and released. Not applicable to cache memory.

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

Important From Last Time

#include <stdio.h> int main() { printf ("hello class\n"); return 0; }

Lecture 03 Bits, Bytes and Data Types

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

CSE 333 Autumn 2014 Midterm Key

Topic 6: A Quick Intro To C

CS 3305 Intro to Threads. Lecture 6

Programs. Function main. C Refresher. CSCI 4061 Introduction to Operating Systems

Page 1. Today. Important From Last Time. Is the assembly code right? Is the assembly code right? Which compiler is right?

Tutorial 1: Introduction to C Computer Architecture and Systems Programming ( )

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #54. Organizing Code in multiple files

COMP322 - Introduction to C++

CS349/SE382 A1 C Programming Tutorial

A brief introduction to C programming for Java programmers

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

MPATE-GE 2618: C Programming for Music Technology. Unit 4.1

Have examined process Creating program Have developed program Written in C Source code

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

just a ((somewhat) safer) dialect.

More on C programming

Programming in C - Part 2

Motivation was to facilitate development of systems software, especially OS development.

Chapter 1 Getting Started

Processes. Johan Montelius KTH

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

Concurrency. Johan Montelius KTH

Section 3: File I/O, JSON, Generics. Meghan Cowan

CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays

Pointer Arithmetic and Lexical Scoping. CS449 Spring 2016

CS 61C: Great Ideas in Computer Architecture Introduction to C

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

A process. the stack

Chapter IV Introduction to C for Java programmers

Functions in C C Programming and Software Tools

Lecture 10: building large projects, beginning C++, C++ and structs

C Introduction. Comparison w/ Java, Memory Model, and Pointers

Basic C Programming. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

What is concurrency? Concurrency. What is parallelism? concurrency vs parallelism. Concurrency: (the illusion of) happening at the same time.

1 You seek performance 3

Threads Tuesday, September 28, :37 AM

Practical C Issues:! Preprocessor Directives, Multi-file Development, Makefiles. CS449 Fall 2017

CS 326: Operating Systems. Process Execution. Lecture 5

We do not teach programming

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

Advanced use of the C language

Q1: /20 Q2: /30 Q3: /24 Q4: /26. Total: /100

CSE 333 SECTION 9. Threads

Embedded Systems Programming

CSE 333 Midterm Exam 5/9/14 Sample Solution

QUIZ. What is wrong with this code that uses default arguments?

211: Computer Architecture Summer 2016

ENCM 501 Winter 2019 Assignment 9

Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: C and Unix Overview

CSCI 350: Getting Started with C Written by: Stephen Tsung-Han Sher June 12, 2016

CSE 333 Midterm Exam Sample Solution 5/10/13

CSE 333 Lecture 2 Memory

Motivation was to facilitate development of systems software, especially OS development.

Programs in memory. The layout of memory is roughly:

printf( Please enter another number: ); scanf( %d, &num2);

C Compilation Model. Comp-206 : Introduction to Software Systems Lecture 9. Alexandre Denault Computer Science McGill University Fall 2006

ECEN 449 Microprocessor System Design. Review of C Programming

III. Classes (Chap. 3)

Contents Lecture 3. C Preprocessor, Chapter 11 Declarations, Chapter 8. Jonas Skeppstedt Lecture / 44

Hello, World! in C. Johann Myrkraverk Oskarsson October 23, The Quintessential Example Program 1. I Printing Text 2. II The Main Function 3

CSE 333 Autumn 2013 Midterm

Transcription:

Types Page 1 "ode to C" Monday, September 18, 2006 4:09 PM 0x0d2C ------ May your signals all trap May your references be bounded All memory aligned Floats to ints round remember... Non -zero is true ++ adds one Arrays start with zero and, NULL is for none For octal, use zero Ox means hex = will set ==means test use -> for a pointer a dot if its not? : is confusing use them a lot a.out is your program there's no U in foobar

and, char (*(*x())[])() is a function returning a pointer to an array of pointers to functions returning char. Types Page 2

Reading complex C types Monday, September 18, 2006 4:05 PM There is an easy trick for reading complex C types. Start by peeling back one layer at a time. Use the meaning of *, and the fact that * and & cancel. Use the meaning of (*x)(...) and the fact that x(...) becomes the outer type. Example: int ***c; means ***c is an int which means **c is of type int * which means *c is of type int ** which means c is of type int *** (in each step, we apply & to both sides) Example: int ** (*foo) (int x, int y); foo(5,7) is of type int ** *foo(5,7) is of type int * **foo(5,7) is of type int Types Page 3

Types Page 4 Definitions and Declarations Tuesday, September 18, 2012 3:06 PM Header files Declarations, not definitions. The C preprocessor and #defines C types Typedefs Portable primitive types Pointers to function Storage classes: extern, static.

Types Page 5 Header files Tuesday, September 20, 2011 1:16 PM Header files So far, we've concentrated on facts needed to read manual pages. Today, we consider the larger issue of reading C header files (.h) Two issues: What can we write? Why do we write it?

Types Page 6 Why have header files? Wednesday, September 17, 2014 4:15 PM Why have header files? Declare things that exist elsewhere (e.g., in the C library or system calls) that you can use. Declare types needed in order to interact with them. Allow multiple programs to #include the same header to use the same external functions or types. Example: stdio.h: declares printf, fopen, fclose,...

Types Page 7 Kinds of C source files Tuesday, September 21, 2010 12:59 PM There are two kinds of C files Header files: declare things to be used by others (.h). Source files: define things that take up memory (.c). Reason for the split: one should only define something once, but one can declare it in every file that uses it. Definitions consume memory. Declarations do not.

Header files Monday, September 18, 2006 3:39 PM Header files end in.h mostly kept in /usr/include describe the types of arguments needed for subroutine calls. Some apparent functions are macros, substituted inline wherever calls appear. (Reason: speed! Function calls take time!) What's in a header: A header shouldn't define anything. (in other words, it should not create functions or data that use storage) A header should declare everything. (in other words, it describes the structure of already created things) Exception: macros aren't defined until used, so they can be declared in headers. General contents of header file: #defines: macros structure and union declarations enumerated type declarations declarations of (global) variables and functions typedefs (compiler macros) inline functions (compiler macros) static functions (redefined in every file that includes them) Types Page 8

A subtle distinction: inline int foo() {... } should be in a header file, while int foo() {... } should not be there. Reason: an inline function is a macro in disguise. It is actually a declaration. Another subtle distinction extern int foo; /* declaration */ versus int foo; /* definition */ Types Page 9

Types Page 10 A source file Tuesday, September 21, 2010 1:01 PM A typical source file foo.c #include <stdio.h> void foo(const char *s) { printf("hello %s\n",s); } A header file foo.h that declares that: void foo(const char *); or extern void foo(const char *); Any program that wishes to use foo: #include "foo.h" foo("harry"); Watch out: If a subroutine is undeclared at time of use, undeclared types default to int! Caveats: <> means search include path: /usr/include... "" means search current directory or absolute path.

Types Page 11 What is in a header file? Tuesday, September 20, 2011 1:19 PM A header file contains: Preprocessor directives: #define TRUE 1 #include "foo.h" #ifdef TRUE... #endif /* TRUE */ declarations: extern int k; extern void foo(int); static double d; inline void foo(int i) { printf("%d\n",i); } type definitions struct foo { int k; double d; }; typedef struct foo bar[5];

Types Page 12 Preprocessor directives Tuesday, September 20, 2011 2:22 PM #define FOO 10 #define BAR(X) int X[FOO]; BAR(goo); // means int goo[10]; #ifdef FOO // this part is only interpreted if FOO is defined #else // this part is only interpreted if FOO is not defined #endif Example use of preprocessor directives: #define DEBUG 1 #ifdef DEBUG some awful debugging write #endif /* DEBUG */ Then gcc -DDEBUG=1 foo.c # includes debugging gcc -UDEBUG foo.c # doesn't include debugging

Types Page 13 The storage classes of variables Tuesday, September 21, 2010 12:51 PM const: the thing I am declaring can't be changed. static: the thing I am declaring is local to this file, but has a global lifetime in this context (which can be a function). extern: the thing I am declaring is defined elsewhere. static int foo(int i) { return i*2; } - In a.c file, creates something that is not exposed to the linker, and thus not available in other c files. - In a.h file, creates a non-shared copy in every.c file that includes it. This is allowed.

Types Page 14 static Tuesday, September 21, 2010 12:53 PM A thing that is static is a global variable with a local scope. It applies to the variable being declared, and must be positioned before it. In subroutines: int foo() { static int count=0; // global lifetime, local scope count++; return count; } returns the count of the number of times foo is called. The opposite of static is dynamic, i.e., created on the stack and deleted when the function ends. in a file, outside a function: static int k; // only accessible in this file. static void foo(); // don't let other files use it. Rudeness with static: can expose via a pointer that is not static int *p = &k; // not static, so its contents are exposed.

Types Page 15 Primer on sanity with global variables Monday, October 5, 2015 4:50 PM Declare all variables static if possible -- limit scope. Be polite about static and const: you can override them but don't.

Types Page 16 extern Tuesday, September 21, 2010 12:55 PM extern allows one to specify that something is available elsewhere. extern int foo(); // someone else defined foo extern double timer; // someone else defined timer Used to give the type of something that will be found elsewhere (in another file) In one file: int timer; In another file extern double timer; In one file: int timer; In another file: extern int timer(); The compiler checks type conformality in each file, but not between files! -> need to use.h files so that in-file checks are enough.

Pointers to function Wednesday, September 14, 2005 4:33 PM Pointers to function Function names are pointers to their addresses. Can declare a "pointer to function" that describes where a function is. If bar is a function, Then bar; // computes where the function is, and discards. But bar(); // calls bar. Examples void bar() { printf("hi"); } void pan(int i) { printf("%d\n",i); } void (*foo)(); // foo is a pointer to a function of zero args foo=bar; // no &: bar is already a pointer! foo(); // exactly equivalent to bar() foo = (void (*)()) pan; // ignore the arguments! ((void (*)(int))foo)(1); // call correctly via another cast! ^^^^^^^^^^ think of thing as a function of one argument: called a method cast. foo is a function of 0 arguments. override that (and waste space!) Note: if a function is called with extra arguments, they are in general ignored. If a function is called with fewer arguments, unpredictable stack (and register) data get interpreted as arguments (whether they are valid or not!) Types Page 17

Types Page 18 Common programming problem int i; int *goo() { return &i; }... if (goo!=null) {... } /* ALWAYS true */ if (goo()!=null) {... } /* correct */ Perfectly legal printf("first byte of foo is %c\n", *((const char *)foo));

Types Page 19 Device drivers Wednesday, September 14, 2005 4:33 PM Example: device drivers: a really simple operating system might have: struct device_info { void *device_data; int (*write)(void *data, const char *buf, int size); int (*read)(void *data, char *buf, int size); } devices[ndevices]; write is a pointer to function that writes something to a specific device. read is a pointer to function that reads something from a specific device. The kernel initializes this table so that write and read point to the actual functions. So, to write "hello" to device n, you'd call devices[n].write(devices[n].device_data,"hello",5); while to read 20 bytes from device n, you'd call char buf[20]; devices[n].read(devices[n].device_data,buf,20); Very important: there is a huge difference between

Types Page 20 devices[n].reset; // computes where a function is, // and then loses it "j;" and devices[n].reset(); with the ()'s, it is a function call; without the ()'s, it is a function pointer.

Typedefs Monday, September 18, 2006 4:03 PM It gets really difficult, really fast, to notate complex C types. One can have an array of functions returning arrays of functions! typedefs provide one way to simplify notation. Types Page 21

Types Page 22 Basic idea of a typedef Monday, September 18, 2006 4:03 PM The basic idea of a typedef: define what is unknown in terms of what is known. Every typedef contains an "unknown symbol" The meaning of a typedef is a substitution of a symbol in a formula. Example typedef int (*fint)(); unknown symbol: fint usage: fint x; means int (*x)(); typedef caveats can only have one unknown symbol. if the symbol has a known type, the typedef has a compiler syntax error. repeating a typedef twice always causes a compiler error.

Types Page 23 Big warning about typedefs Tuesday, September 21, 2010 10:51 AM big warning typedefs are "like" classes in C++, but they are not identical to classes. Problem is with "type equivalence" If two classes have different names, C++ considers them to be differing types. If two typedefs resolve to the same base definition, C considers them to be the same type. Example: typedef long pid_t; typedef pid_t bigarr[1000]; bigarr x; long y[1000]; Then x and y are considered to have the same type, regardless of how that type is defined!

Types and function prototypes Monday, September 18, 2006 4:36 PM Types and function prototypes In C++, functions with the same name and different arguments are considered to be different. In C, the function name defines the function and is independent of the types of arguments. It is possible to define a single function that takes many types of arguments (e.g., printf). C function calling the function's prototype (argument types) determines how data in the stack frame will be referenced, but not its actual content. The function's call determines how the data in the stack frame will be laid out in memory. It is legal for these to differ! If these differ, havoc can result! Types Page 24

Header file conventions Monday, September 18, 2006 3:39 PM General header convention: Header files containing typedefs can't be included twice or havoc results. Why? Because typedefs define the unknown symbols in them. It is an error to define something twice. General trick: #ifndef STDIO_H_DEFINED #define STDIO_H_DEFINED 1... rest of header... #endif /* STDIO_H_DEFINED */ If a header file consists of this code, then including it twice has no effect. Almost all header files are "proofed" against being included twice in this way. #ifdef X == #if defined(x) Types Page 25

Types Page 26 The "why" of typedefs Tuesday, September 20, 2011 1:42 PM The "why" of it all Naively, typedefs reduce the length of a declaration: typedef struct _IO_FILE FILE; means that you can type: FILE *s; rather than struct _IO_FILE *s; But there is another use of typedefs: portability. Suppose you want something to run on both ia32 and ia64. On an ia32, ints are 32 bits long. On an ia64, ints are 64 bits long. But you want something that doesn't change in size. So, we write (on ia64): typedef long int clock_t; // define a base type typedef clock_t clock_t; // define an exposed type Why so many layers? Each layer addresses a different form of portability First layer clock_t: the kernel's idea of time. Second layer clock_t: the application's idea of time. You will almost never see int in an OS.

You will see int32_t, int64_t, uint32_t, and uint64_t. Types Page 27

Putting this to use Monday, October 5, 2015 2:02 PM The declaration of sigaction is : int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact); Where struct sigaction { void (*sa_handler)(int); void (*sa_sigaction)(int, siginfo_t *, void *); sigset_t sa_mask; int sa_flags; }; void (*sa_restorer)(void); This says that one can define an action via either void my_sigaction(int signal, siginfo_t *info, void *data) {... } or void my_handler(int signal) {... } (but not both). (In most architectures, you can't assign both to sigaction and handler; they're stored as a union) The extra arguments of a sigaction: info: detailed information about the signal. data: a ucontext_t object (if available), which represents the execution context in which the signal occurred. Very subtle question: why is data of type void *? Answer: different systems define this information differently. Types Page 28

Types Page 29 Putting this to use: pthread_create Monday, October 5, 2015 2:36 PM To create a thread, one has to jump through some rather complex hoops: Define the data to be passed to the thread. that the thread returns when done. Both of these types are determined by the implementation of the thread. Hide the types of this data from the OS To create a POSIX thread, we use the very cryptic: int pthread_create( pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg ); where thread is the thread descriptor attr describes how the thread should behave start_routine is the routine to call takes "arg" as argument, returns a return type. arg is the argument to send to start_routine (thread input) Some mysteries:

Types Page 30 Q: Why so many "void *" declarations? A: the actual types are determined by the thread and its containing process, not the operating system. Q: Why return a pointer rather than an object? A: Pointers take constant space; objects take variable space on the stack! For example: http://www.cs.tufts.edu/comp/111/examples/ Threads/pthreads1.c