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

Similar documents
CSE 374 Programming Concepts & Tools

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

CSE 374 Programming Concepts & Tools

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

Macros and Preprocessor. CGS 3460, Lecture 39 Apr 17, 2006 Hen-I Yang

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

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

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

COMP322 - Introduction to C++

Appendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things.

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

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

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

EL6483: Brief Overview of C Programming Language

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

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

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

C Programming Review CSC 4320/6320

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

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

COSC 2P91. Introduction Part Deux. Week 1b. Brock University. Brock University (Week 1b) Introduction Part Deux 1 / 14

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

Lectures 5-6: Introduction to C

CSE 303: Concepts and Tools for Software Development

Topic 6: A Quick Intro 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.

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

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

CSCI 171 Chapter Outlines


G52CPP C++ Programming Lecture 6. Dr Jason Atkin

Kurt Schmidt. October 30, 2018

A Fast Review of C Essentials Part I

CSE 303: Concepts and Tools for Software Development

#include. Practical C Issues: #define. #define Macros. Example. #if

CS3157: Advanced Programming. Outline

Lectures 5-6: Introduction to C

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

Better variadic functions in C

Practical C++ Programming

Preface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS

CSE 303: Concepts and Tools for Software Development

CS3157: Advanced Programming. Outline

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

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


IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

C for C++ Programmers

CSE 374 Programming Concepts & Tools. Brandon Myers Winter 2015 Lecture 11 gdb and Debugging (Thanks to Hal Perkins)

Ch. 3: The C in C++ - Continued -

DDMD AND AUTOMATED CONVERSION FROM C++ TO D

C PROGRAMMING LANGUAGE. POINTERS, ARRAYS, OPERATORS AND LOOP. CAAM 519, CHAPTER5

Oregon State University School of Electrical Engineering and Computer Science. CS 261 Recitation 2. Spring 2016

OBJECT ORIENTED PROGRAMMING USING C++

Lecture Notes on Generic Data Structures

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

Chapter 7: Preprocessing Directives

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

APPENDIX A : Example Standard <--Prev page Next page -->

G52CPP C++ Programming Lecture 9

Important From Last Time

The C Preprocessor (and more)!

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

EL2310 Scientific Programming

Page 1. Today. Last Time. Is the assembly code right? Is the assembly code right? Which compiler is right? Compiler requirements CPP Volatile

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).

CIS 190: C/C++ Programming. Classes in C++

Annotation Annotation or block comments Provide high-level description and documentation of section of code More detail than simple comments

G52CPP C++ Programming Lecture 8. Dr Jason Atkin

Embedded Controller Programming 2

ECEN 449 Microprocessor System Design. Review of C Programming

COMsW Introduction to Computer Programming in C

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

Static Semantics. Winter /3/ Hal Perkins & UW CSE I-1

Operator overloading

COMP 2355 Introduction to Systems Programming

C Programming SYLLABUS COVERAGE SYLLABUS IN DETAILS

INDEX. Figure I-0. Listing I-0. Table I-0. Symbols.DIRECTIVE (see Assembler directives)? preprocessor operator 3-34

C++ Coding Standards and Practices. Tim Beaudet March 23rd 2015

Modifiers. int foo(int x) { static int y=0; /* value of y is saved */ y = x + y + 7; /* across invocations of foo */ return y; }

BLM2031 Structured Programming. Zeyneb KURT

COMP26120: Algorithms and Imperative Programming. Lecture 5: Program structuring, Java vs. C, and common mistakes

Compiler Theory. (GCC the GNU Compiler Collection) Sandro Spina 2009

C Syntax Out: 15 September, 1995

Final CSE 131B Spring 2004

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Software Engineering /48

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

CSE 374 Programming Concepts & Tools

Starting to Program in C++ (Basics & I/O)

Computational Methods of Scientific Programming Fall 2007

Lecture Notes on Generic Data Structures

Lecture 13: more class, C++ memory management

Page 1. Agenda. Programming Languages. C Compilation Process

CS 314 Principles of Programming Languages. Lecture 11

Macros in C/C++ Computer Science and Engineering College of Engineering The Ohio State University. Lecture 33

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

A Fast Review of C Essentials Part II

CS558 Programming Languages

Transcription:

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

Linked lists, trees, and friends Very, very common data structures Building them in C Use malloc to create nodes Need to use casts for generic types Memory management issues if shared nodes Usually need to explicitly free entire thing when done Shows tradeoffs between lists and arrays Look at the sample code and understand what it does/how it does it 2

Linked list 3

Generic data structures Java: class ListNode<V> { private V value; } private ListNode<V> next; No one best solution in C. Possibilities include a) casts and void* pointer to data (style of malloc) b) casts and fixed size data (i.e., not fully generic) c) macros to substitute in types (generate type-specific code) C++ uses (c) for generic programming but has a better/type-safe tool called templates 4

Generic List, example struct GenericListNode { } void* data; GenericListNode* next; struct List { int data_size; GenericListNode* head; } void insert_copy(list* li, void* value) { } tail = /* find tail */ GenericListNode* newtail = (GenericListNode*) newtail->data = malloc(li->data_size); malloc(sizeof(genericlistnode)); memcpy(newtail->data, tail->data, li->data_size); newtail->next = NULL; 5

C types There are an infinite number of types in C, but only a few ways to make them: char, int, double, etc. (many variations like unsigned int, long, short, ; mostly implementation-defined ) void (placeholder; a type no expression can have) struct T where there is already a declaration for that struct type Array types (basically only for stack arrays and struct fields, every use is automatically converted to a pointer type) t* where t is a type union T, enum E (later, maybe) function-pointer types (later) typedefs (just expand to their definition; type synonym) 6

Typedef Defines a synonym for a type does not declare a new type Syntax typedef type name; After this declaration, writing name is the same as writing type Caution: array typedef syntax is weirder Examples: typedef int int32; // use int32 for portability typedef struct point { int32 x, y; } Point2d; // Point2d is synonym for struct Point typedef Point2d * ptptr; // pointer to Point2D Point2d p; // var declaration ptptr ptlist; // declares pointer 7

Casts, part 1 Syntax: (t)e where t is a type and e is an expression (same as Java) Semantics: It depends If e is a numeric type and t is a numeric type, this is a conversion To wider type, get same value To narrower type, may not (will get mod) From floating-point to integral, will round (may overflow) From integral to floating-point, may round (but int to double is exact on most machines) Note: Java is the same without the most machines part Note: Lots of implicit conversions such as in function calls Bottom Line: Conversions involve actual operations; (double)3 is a very different bit pattern than (int)3 8

Casts, part 2 If e has type t1*, then (t2*)e is a (pointer) cast. You still have the same pointer (index into the address space). Nothing happens at run-time. You are just getting around the type system, making it easy to write any bits anywhere you want. Old example: malloc has return type void*; we cast to required pointer type void evil(int **p, int x) { int * q = (int*)p; *q = x; } void f(int **p) { evil(p,345); **p = 17; // writes 17 to address 345 (HYCSBWK) } Note: The C standard is more picky than we suggest, but few people know that and little code obeys the official rules. 9

C pointer casts, continued Questions worth answering: How does this compare to Java s casts? Unsafe, unchecked (no type fields in objects) Otherwise more similar than it seems When should you use pointer casts in C? For generic libraries (malloc, linked lists, operations on arbitrary (generic) pointers, etc.) For subtyping (later) What about other casts? Casts to/from struct types (not struct pointer casts) are compile-time errors. 10

Course feedback We re half way done Anonymous course feedback on website will be up later today. Optional. Take it by Sunday night How to improve the utility of lectures, homeworks, office hours Compliments and suggestions Complaints and suggestions 11

Preprocessor: The story so far We ve looked at the basics of the preprocessor #include to access declarations in header files #define for symbolic constants Now: More details; where it fits Multiple source and header files A bit about macros (somewhat useful, somewhat a warning) 12

The compilation picture gcc does all this for you (reminder) -E to only preprocess; result on stdout (rare) -c to stop with.o (common for individual files in larger program) 13

More about multiple files Typical usage: Preprocessor #include to read file containing declarations describing code Linker handles your.o files and other code By default, the standard C library Other.o and.a files Whole lecture on linking and libraries later 14

The preprocessor Rewrites your.c file before the compiler gets at the code. Lines starting with # tell it what to do Can do crazy things (please don t); uncrazy things are: 1. Including contents of header files 2. Defining constants and parameterized macros Token-based, but basically textual replacement Easy to mis-define and misuse 3. Conditional compilation Include/exclude part of a file Example uses: code for debugging, code for particular computers (handling portability issues), the trick for including header files only once 15

File inclusion (review) #include <hdr.h> Search for file hdr.h in standard include directories and include its contents in this place Typically lots of nested includes, result not fit for human consumption Idea is simple: declaration of standard library routines are in headers; allows correct use after declaration #include hdr.h Same, but first look in current directory How to break your program into smaller files that can call routines in other files gcc -I option: look first in specified directories for headers (keep paths out of your code files) (not needed for 374) 16

Header file conventions Conventions: always follow these when writing a header file 1. Give included files names ending in.h; only include these header files. Never #include a.c source file 2. Do not put functions definitions in a header file; only struct definitions, prototypes (declarations), and other includes 3. Do all your #includes at the beginning of a file 4. For header file foo.h start it with: #ifndef FOO_H #define FOO_H and end it with: #endif (We will learn why very soon) 17

Simple macros (review) Symbolic constants and other text #define NOT_PI 22/7 #define VERSION 3.14 #define FEET_PER_MILE 5280 #define MAX_LINE_SIZE 5000 Replaces all matching tokens in rest of file Knows where words start and end (unlike sed) Has no notion of scope (unlike C compiler) (Rare: can shadow with another #define or use #undef to remove) 18

Some predefined macros e.g., LINE : source file line, FILE source file name e.g., log message that has source code information printf("%s:%d %s\n", FILE, LINE, x) 19

Macros with parameters #define TWICE_AWFUL(x) x*2 #define TWICE_BAD(x) ((x)+(x)) #define TWICE_OK(x) ((x)*2) double twice(double x) { return x+x; } // best (editorial opinion) Replace all matching calls with body but with text of arguments where the parameters are (just string substitution) Gotchas (understand why!): y=3; z=4; w=twice_awful(y+z); y=7; z=twice_bad(++y); z=twice_bad(y++); Common misperception: Macros avoid performance overhead of a function call (maybe true in 1975, not now) Macros can be more flexible though (TWICE_OK works on ints and doubles without conversions (which could round)) 20

Justifiable uses Parameterized macros are generally to be avoided (use functions), but there are things functions cannot do: generating code use type names (or other code) as arguments #define NEW_T(t,howmany) ((t*)malloc((howmany)*sizeof(t)) create new identifiers and write generic definitions #define SCHEMA(t1, t2) \ typedef struct schema_##t1_##t2 { \ t1 field1; \ t2 field2; \ } schema_##t1_##t2; 21

Conditional compilation #ifdef FOO (matching #endif later in file) #ifndef FOO (matching #endif later in file) #if FOO > 2 (matching #endif later in file) (You can also have a #else inbetween somewhere.) Simple use: #ifdef DEBUG // do following only when debugging printf(...); #endif Fancier: (and another use of parameterized macros) #ifdef DEBUG // use DBG_PRINT for debug-printing #define DBG_PRINT(x) printf("%s",x) #else #define DBG_PRINT(x) // replace with nothing #endif gcc -D FOO makes FOO defined 22

Back to header files Now we know what this means: #ifndef SOME_HEADER_H_ #define SOME_HEADER_H_... rest of some_header.h... #endif // SOME_HEADER_H_ Assuming nobody else defines SOME_HEADER_H_ (convention), the first #include "some_header.h" will do the define and include the rest of the file, but the second and later will skip everything More efficient than copying the prototypes over and over again In presence of circular includes, necessary to avoid creating an infinitely large result of preprocessing So we always do this nicer alternative is to put the following at the top of the header: #pragma once (not in the language standard but is supported by most C compilers) 23

C preprocessor summary A few easy to abuse features and a bunch of conventions (for overcoming C s limitations). #include (the way you say what other definitions you need; cycles are fine with the trick ) #define (parameterized macros have a few justifiable uses; token-based text replacement) #if... (for showing the compiler less code) 24