CSE 374 Programming Concepts & Tools

Slides:



Advertisements
Similar presentations
CS 11 C track: lecture 7 Last week: structs, typedef, linked lists This week: hash tables more on the C preprocessor extern const.
Advertisements

C Language.
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
C and Data Structures Baojian Hua
Miscellaneous topicsCS-2301 B-term Miscellaneous Topics CS-2301, System Programming for Non-majors (Slides include materials from The C Programming.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
Variables and Objects, pointers and addresses: Chapter 3, Slide 1 variables and data objects are data containers with names the value of the variable is.
Introduction to Java University of Sunderland CSE301 Harry R. Erwin, PhD.
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
1 Pointers to structs. 2 A pointer to a struct is used in the same way as a pointer to a simple type, such as an int. Pointers to structs were introduced.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
Topic 3: C Basics CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
Topics memory alignment and structures typedef for struct names bitwise & for viewing bits malloc and free (dynamic storage in C) new and delete (dynamic.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 10 – C: the heap and manual memory management.
CSE 1341 Honors Note Set 2 1. Overview  Java vs. C++  Functions in C++  First Programming Packet  Development Environment 2.
Weekly C-minar Week 0. Today: Steps of the compile Basic inclusion/syntax rules Low-cost containment Debugging.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 12 – C: structs, linked lists, and casts.
CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 10/11/2006 Lecture 7 – Introduction to C.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 20 – C++ Subclasses and Inheritance.
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 18 – Linking and Libraries.
Variables Bryce Boe 2012/09/05 CS32, Summer 2012 B.
1 Binghamton University Exam 1 Review CS Binghamton University Birds eye view -- Topics Information Representation Bit-level manipulations Integer.
Run-Time Environments Presented By: Seema Gupta 09MCA102.
Lecture 9 Symbol Table and Attributed Grammars
CSE 374 Programming Concepts & Tools
Generic Programming in C
Basic Data Types & Memory & Representation
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
User-Written Functions
CSE 374 Programming Concepts & Tools
CSE 374 Programming Concepts & Tools
Introduction to Linked Lists
Lectures linked lists Chapter 6 of textbook
Chapter 10-1: Structure.
Java Primer 1: Types, Classes and Operators
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Winter 2013.
CSE 303 Concepts and Tools for Software Development
Functions Separate Compilation
CISC/CMPE320 - Prof. McLeod
Checking Memory Management
C Basics.
Type Systems Terms to learn about types: Related concepts: Type
Introduction to Linked Lists
Lecture 9 Structure 1. Concepts of structure Pointers of structures
Strings, Line-by-line I/O, Functions, Call-by-Reference, Call-by-Value
C Structures, Unions, Bit Manipulations and Enumerations
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
CS 240 – Lecture 18 Command-line Arguments, Typedef, Union, Bit Fields, Pointers to Functions.
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Winter 2018.
Pointers C#, pointers can only be declared to hold the memory addresses of value types int i = 5; int *p; p = &i; *p = 10; // changes the value of i to.
Structures, Unions, and Typedefs
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
Java Programming Language
Data Structures & Algorithms
Course Overview PART I: overview material PART II: inside a compiler
Java Basics Data Types in Java.
Standard Version of Starting Out with C++, 4th Edition
C Programming - Lecture 5
(1 - 2) Introduction to C Data Structures & Abstract Data Types
C Structures and Memory Allocation
Classes and Objects Object Creation
CSE 303 Concepts and Tools for Software Development
Lecture 7: Types (Revised based on the Tucker’s slides) 10/4/2019
SPL – PS2 C++ Memory Handling.
CSE 303 Concepts and Tools for Software Development
Presentation transcript:

CSE 374 Programming Concepts & Tools Hal Perkins Winter 2017 Lecture 12 – C: structs, linked lists, and casts UW CSE 374 Winter 2017

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 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 Which will start right after Monday’s midterm UW CSE 374 Winter 2017

structs A struct is a record (i.e., a collection of data fields) 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).f in C is like x.f in 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 can be whole struct locations or just a field’s location) Again, right-expressions evaluate to values (which can be whole structs or just a field’s contents) UW CSE 374 Winter 2017

struct tags A typical struct definition looks like: struct person_info { char * name; int age; } The identifier person_info after “struct” is not a type name, it is a struct tag. The “type” of this struct is struct person_info. So type names (int, char) and struct tags are not the same kind of names UW CSE 374 Winter 2017

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! – 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 (we already knew this) UW CSE 374 Winter 2017

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, geometric 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? UW CSE 374 Winter 2017

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 UW CSE 374 Winter 2017

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) UW CSE 374 Winter 2017

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 { // type tag optional (sortof) int32 x, y; } Point2d; // Point2d is synonym for struct typedef Point2d * ptptr; // pointer to Point2D Point2d p; // var declaration ptptr ptlist; // declares pointer UW CSE 374 Winter 2017

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 UW CSE 374 Winter 2017

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. UW CSE 374 Winter 2017

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. UW CSE 374 Winter 2017