CS240: Programming in C

Similar documents
CS240: Programming in C

Structures, Unions Alignment, Padding, Bit Fields Access, Initialization Compound Literals Opaque Structures Summary. Structures

Lecture 8 Dynamic Memory Allocation

Computer System and programming in C

Data Storage. August 9, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 August 9, / 19

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

CS C Primer. Tyler Szepesi. January 16, 2013

Data Representation and Storage. Some definitions (in C)

Final CSE 131B Spring 2004

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

Data Representation and Storage

Programming in C - Part 2

Subject: Fundamental of Computer Programming 2068

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Programming. Structures, enums and unions

Memory. What is memory? How is memory organized? Storage for variables, data, code etc. Text (Code) Data (Constants) BSS (Global and static variables)

Lecture 5: Outline. I. Multi- dimensional arrays II. Multi- level arrays III. Structures IV. Data alignment V. Linked Lists

C Programming Review CSC 4320/6320

COMP26120: Linked List in C (2018/19) Lucas Cordeiro

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

(13-2) Dynamic Data Structures I H&K Chapter 13. Instructor - Andrew S. O Fallon CptS 121 (November 17, 2017) Washington State University

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

Lecture 3: C Programm

Procedures, Parameters, Values and Variables. Steven R. Bagley

CSE 230 Intermediate Programming in C and C++

Parameter passing. Programming in C. Important. Parameter passing... C implements call-by-value parameter passing. UVic SEng 265

CS349/SE382 A1 C Programming Tutorial

Roadmap. Java: Assembly language: OS: Machine code: Computer system:

This is CS50. Harvard University Fall Quiz 0 Answer Key

Structures. Basics of Structures (6.1) EECS l Now struct point is a valid type. l Defining struct variables: struct point { int x; int y; };

Computer Programming

CS240: Programming in C

CS429: Computer Organization and Architecture

Kurt Schmidt. October 30, 2018

Final CSE 131B Spring 2005

CS16 Exam #1 7/17/ Minutes 100 Points total

CS 61c: Great Ideas in Computer Architecture

Principles of C and Memory Management

DECLARAING AND INITIALIZING POINTERS

Week 7: Pointers and Arrays. BJ Furman 02OCT2009

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

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Procedural programming with C

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

Cpt S 122 Data Structures. Data Structures

EL2310 Scientific Programming

CSCI 171 Chapter Outlines

Operating Systems 2INC0 C course Pointer Advanced. Dr. Ir. Ion Barosan

Fundamentals of Programming. Lecture 12: C Structures, Unions, Bit Manipulations and Enumerations

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

CS61C Machine Structures. Lecture 4 C Structs & Memory Management. 9/5/2007 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

Computer Programming Lecture 12 Pointers

Pointers. Part VI. 1) Introduction. 2) Declaring Pointer Variables. 3) Using Pointers. 4) Pointer Arithmetic. 5) Pointers and Arrays

M.EC201 Programming language

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

EM108 Software Development for Engineers

M.CS201 Programming language

Arrays and Pointers in C. Alan L. Cox

More in-class activities / questions

Lecture 2: C Programming Basic

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

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

COMP26120: Pointers in C (2018/19) Lucas Cordeiro

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

12 CREATING NEW TYPES

Outline. Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary

Character Strings. String-copy Example

Creating, Compiling and Executing

Midterm CSE 131 Winter 2012

High Performance Computing in C and C++

CS241 Computer Organization Spring Data Alignment

CS561 Manju Muralidharan Priya Structures in C CS200 STRUCTURES. Manju Muralidharan Priya

C Programming Language: C ADTs, 2d Dynamic Allocation. Math 230 Assembly Language Programming (Computer Organization) Thursday Jan 31, 2008

ECE 15B COMPUTER ORGANIZATION

Computer Science & Engineering 150A Problem Solving Using Computers

ELEC 377 C Programming Tutorial. ELEC Operating Systems

BSM540 Basics of C Language

CSE 374 Programming Concepts & Tools

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

AGENDA Binary Operations CS 3330 Samira Khan

Compiling and Running a C Program in Unix

Programming for Electrical and Computer Engineers. Pointers and Arrays

CS61C Machine Structures. Lecture 5 C Structs & Memory Mangement. 1/27/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

Bit Manipulation in C

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

Type Checking. Prof. James L. Frankel Harvard University

Machine-Level Programming V: Unions and Memory layout

Class Information ANNOUCEMENTS

just a ((somewhat) safer) dialect.

Lecture06: Pointers 4/1/2013

BLM2031 Structured Programming. Zeyneb KURT

Self-referential Structures and Linked List. Programming and Data Structure 1

CS 61C: Great Ideas in Computer Architecture C Pointers. Instructors: Vladimir Stojanovic & Nicholas Weaver

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

Introduction to C: Pointers

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

Review: C Strings. A string in C is just an array of characters. Lecture #4 C Strings, Arrays, & Malloc

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

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

Transcription:

CS240: Programming in C Lecture 10: Review - Structures and Memory Allocation Unions

Recap: Structures Holds multiple items as a unit Treated as scalar in C: can be returned from functions, passed to functions They can not be compared A structure can include a pointer to itself, but not a member of the same structure a member of another structure, the latter has to have the prototype declared before Allocation of memory for structure s fields must respect alignment as dictated by the underlying architecture

Structure recap Member access Direct: s.member Indirect: s_ptr->member Dot operator. has precedence over indirect access operator -> What does s.t->u mean? How about (s.t) -> u? Or, s.(t->u)? Is there a difference between (*s).t and s->t?

Memory layout for a structure Data alignment: when the processor accesses the memory reads more than one byte, usually 4 bytes on a 32-bit platform. What if the data structure is not a multiple of 4? Padding. Implementations must typically handle alignment.

Manipulating Structures What happens when a structure is passed as an argument? #include <stdio.h> struct Foo { int a; ; struct Foo foo (struct Foo b) { b.a = 13; return b; What gets printed? int main () { struct Foo f; f.a = 100; foo(f); printf("value of structure parameter is %d\n", f.a); 5

Example (cont) #include <stdio.h> struct Foo { int a; ; struct Foo foo (struct Foo b) { b.a = 13; return b; int main () { struct Foo f; f.a = 100; f = foo(f); printf("value of structure parameter is %d\n", f.a); 6

Example (cont) #include <stdio.h> typedef struct { int a; Foo; void foo (Foo * b) { b->a = 13; Recall that C uses a call-byvalue discipline Use indirection to implicitly propagate effects int main () { Foo f; foo(&f); printf("value of structure parameter is %d\n", f.a); 7

Bit fields Structure member variables can be defined in bits Everything about bit fields is machinedependent struct { unsigned int is_down : 1; unsigned int is_red : 1; flags; flags.is_down = 1; if (flags.is_red == 0) {.

Memory Management int main() { int* x; int* start; double* y; start = (int*) malloc(5); x = start; *x = -42; x++; y=(double*) x; *y = 2.1; y++; x=(int*) y; *x = 42; printf("%i\n", *start); printf("%i\n", start[0]); printf("%i\n", start[1]); printf("%i\n", start[2]); printf("%i\n", start[3]); printf("%i\n", start[4]); printf("%i\n", start[5]); printf("%f\n", *(double*)(start+1)); What does start represent? -42 2.1 x y x 42 9

Memory Management char *mess = NULL; mess = (char*) malloc(100); free(mess); *mess = 7; What is the state of the memory pointed by mess after free? What happens if mess is accessed after free? 10

Unions They can hold different type of values at different times Definition is similar to a structure BUT STORAGE IS SHARED between the members Only one field type stored at a time Programmer s responsibility to keep track of what it is stored. Useful for defining values that range over different types Critically, the memory allocated for these types is shared

Unions memory layout All members have offset zero from the base Size is big enough to hold the widest member The alignment is appropriate for all the types in the union

Union operations Same as structures: The same operations as the ones permitted on structures are permitted on unions: Assignment, Coping as a unit Taking the address Accessing a member Initialize: can be initialized with a value of the type of its first member.

Example typedef union { int units; float kgs; amount ; typedef struct { char selling[15]; float unit_price; int unit_type; amount how_much; product; The variable howmuch can be either an int or a float depending on the kind of product The compiler allocates the memory necessary to store the largest sized type in the union (here float) 14

Safety C provides no safety guarantees that components within unions are correctly accessed void foo(amount x) { printf("... %d\n", x.units); int main () { product p[10]; p[0].selling = "toys"; p[0].unit_price = 2.0; p[0].unit_type = 10; p[0].how_much.kgs = 3.0; foo(p[0].how_much) What gets printed? 15

Example (cont) void checkunits(int nitems, product* store[]) { int i; for (i=0; i<nitems; i++) { printf("\n%s\n",store[i]->selling); switch (store[i]->unit_type) { case 1: printf("we have %d units for sale\n",store[i]->how_much.units); break; case 2: printf("we have %f kgs for sale\n", store[i]->how_much.kgs); break; Create an array that points to different products: product * store[n] and supply this array as the argument to checkunits 16