CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

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

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

CSE 431S Type Checking. Washington University Spring 2013

A3-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH 'C' LANGUAGE

Week 5, continued. This is CS50. Harvard University. Fall Cheng Gong

CSE 374 Programming Concepts & Tools

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

Important From Last Time

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

CSE 303: Concepts and Tools for Software Development

Intro to C: Pointers and Arrays

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

A Fast Review of C Essentials Part I

Pointers, Pointers, Pointers!

Java and C CSE 351 Spring

Final CSE 131B Spring 2004

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

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011

Midterm CSE 131B Spring 2005

Data Types (cont.) Administrative Issues. Academic Dishonesty. How do we detect plagiarism? Strongly Typed Languages. Type System

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

EL2310 Scientific Programming

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

ECEN 449 Microprocessor System Design. Review of C Programming

Java and C I. CSE 351 Spring Instructor: Ruth Anderson

Important From Last Time

Contents of Lecture 3

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, SPRING 2013

BLM2031 Structured Programming. Zeyneb KURT

Tokens, Expressions and Control Structures

Embedded Controller Programming 2

Java and C. CSE 351 Autumn 2018

12 CREATING NEW TYPES

Computer Organization & Systems Exam I Example Questions

Structures and Pointers

G Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University

CSE 303: Concepts and Tools for Software Development

CSE351 Spring 2010 Final Exam (9 June 2010)

Types. What is a type?

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

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

Compiler Construction I

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

The New C Standard (Excerpted material)

Pointers, Dynamic Data, and Reference Types

POINTER AND ARRAY SUNU WIBIRAMA

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

Language Design COMS W4115. Prof. Stephen A. Edwards Spring 2003 Columbia University Department of Computer Science

Algorithms & Data Structures

Lectures 5-6: Introduction to C


Lectures 5-6: Introduction to C

EL6483: Brief Overview of C Programming Language

CS201- Introduction to Programming Current Quizzes

Structures. Dr. Donald Davendra Ph.D. (Department of Computing Science, Structures FEI VSB-TU Ostrava)

Review. CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Let bindings (CBV) Adding Stuff. Booleans and Conditionals

CSE 333. Lecture 11 - constructor insanity. Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington

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

Structures. Today s Goals. Structure Operations. Structure Type Declaration. Struct Instance. typedef. CS246 Lec12. Structures

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

CS3157: Advanced Programming. Outline

C Programming Review CSC 4320/6320

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

CS 430 Spring Mike Lam, Professor. Data Types and Type Checking

Question No: 1 ( Marks: 1 ) - Please choose one One difference LISP and PROLOG is. AI Puzzle Game All f the given

Inheritance & Polymorphism. Object-Oriented Programming Spring 2015

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

Class Information ANNOUCEMENTS

Structures, Unions, and Typedefs

COMP 181. Agenda. Midterm topics. Today: type checking. Purpose of types. Type errors. Type checking

CSC324 Principles of Programming Languages

CSE 12 Week Eight, Lecture One

C Programming Basics II

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

2. First Program Stuff

In Java we have the keyword null, which is the value of an uninitialized reference type

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

CSE 374 Programming Concepts & Tools

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

Short Notes of CS201

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

These are notes for the third lecture; if statements and loops.

Computer Systems Principles. C Pointers

CS349/SE382 A1 C Programming Tutorial

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

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

CS201 - Introduction to Programming Glossary By

G Programming Languages - Fall 2012

Two s Complement Review. Two s Complement Review. Agenda. Agenda 6/21/2011

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

September 10,

CSE 401/M501 Compilers

COMP26912: Algorithms and Imperative Programming

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

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

ECEN 449 Microprocessor System Design. Review of C Programming. Texas A&M University

C Syntax Out: 15 September, 1995

378: Machine Organization and Assembly Language

Transcription:

CSE 374 Programming Concepts & Tools Hal Perkins Spring 2010 Lecture 12 C: structs, t linked lists, and casts

Where we are We ve seen most of the basic stuff about C, but we still need to look at structs (aka records or objects without methods) and linked data structures Understand d the code posted with today s lecture we won t have time to walk through all the details Next: Rest of the C preprocessor (# stuff, macros), building multi-file programs Then: more programming tools (make) That will set us up for the next programming project Meanwhile: Midterm exam this Friday Closed book, optional page of handwritten notes 2

structs A struct is a record A pointer to a struct is like a Java object with no methods x.f is for field access. (if is x not a pointer new!) (*x) (x).f in C is like xfin x.f Java. (if x is a pointer) x->f is an abbreviation for (*x).f There is a huge difference between a struct (value) parameter and a pointer to a struct There is a huge difference between local variables that are structs and those that are pointers to structs Again, left-expressions evaluate to locations (which h can be whole struct locations or just a field s location) Again, right-expressions evaluate to values (which can be whole structs t or just a field s contents) t 3

C parameters - revisited C has a uniform rule for parameters (almost): When a function is called, each parameter is initialized with a copy of the corresponding argument (int, char, ptr, ) This holds even for structs! t a copy is created There is no further connection between the argument and the parameter value in the function But they can point to the same thing, of course But: if the argument is an array name, the function parameter is initialized with a pointer to the array argument instead of a copy of the entire array Implicit array promotion 4

struct parameters A struct argument is copied (call-by-value) It is far more common to use a pointer to a struct as an argument instead of copying an entire struct Gives same semantics as Java object references Usually what you want pointer to data that lives outside the function Also avoids cost of copying a possibly large object But occasionally you want call-by value (small things like complex numbers, points, ) Puzzle: if an argument is an array containing a single struct, is it copied or is it promoted to a pointer? What if it s a struct containing only a single array? 5

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 6

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 type; 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) y 7

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 Examples: typedef int int32; // use int32 for portability typedef struct point { // type tag optional (sortof) int32 x, y; } Point2d; // Point2d is synonym for struct typedef Point2d * ptptr; pp // pointer to Point2D Point2d p; ptptr ptlist; // var declaration // declares pointer 8

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 i t to integral, will round (may overflow) From integral to floating-point, may round (but int to double won t round on most machines) Note: Java is the same without t the most machines part Note: Lots of implicit conversions such as in function calls Bottom Line: Conversions involve real operations; (double)3 is a very different bit pattern than (int)3 9

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* 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. 10

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. 11