CS240: Programming in C

Similar documents
CS240: Programming in C

CS240: Programming in C

CS240: Programming in C

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

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

Procedural programming with C

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

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

Computer System and programming in C

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

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

Lecture 8 Dynamic Memory Allocation

Pointers, Dynamic Data, and Reference Types

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) Allocating Space

Programming. Structures, enums and unions

High Performance Computing in C and C++

CSCI 171 Chapter Outlines

Machine-Level Programming V: Unions and Memory layout

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

Kurt Schmidt. October 30, 2018

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

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; };

Dynamic memory allocation

Cpt S 122 Data Structures. Data Structures

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

Structures, Unions, and Typedefs

C Programming Review CSC 4320/6320

Dynamic Memory Allocation

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

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

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

MPATE-GE 2618: C Programming for Music Technology. Syllabus

CS240: Programming in C

CS 5513 Entry Quiz. Systems Programming (CS2213/CS3423))

COMP 2355 Introduction to Systems Programming

ECE 2400 Computer Systems Programming Fall 2017 Topic 4: C Pointers

Run-Time Data Structures

Dynamic Memory: Alignment and Fragmentation

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

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

CS107, Lecture 9 C Generics Function Pointers

CS61, Fall 2012 Section 2 Notes

Class Information ANNOUCEMENTS

CS240: Programming in C

CS113: Lecture 9. Topics: Dynamic Allocation. Dynamic Data Structures

Advanced Pointer & Data Storage

Compiling and Running a C Program in Unix

Lecture 3: C Programm

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

Arrays and Pointers in C & C++

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

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

Principles of C and Memory Management

Final CSE 131B Spring 2004

CS201 Some Important Definitions

Functions in C C Programming and Software Tools

CS240: Programming in C

Subject: Fundamental of Computer Programming 2068

POINTER AND ARRAY SUNU WIBIRAMA

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

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

Assembler Programming. Lecture 10

Understanding Pointers

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

Malloc Lab & Midterm Solutions. Recitation 11: Tuesday: 11/08/2016

11 'e' 'x' 'e' 'm' 'p' 'l' 'i' 'f' 'i' 'e' 'd' bool equal(const unsigned char pstr[], const char *cstr) {

CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language. Krste Asanović & Randy Katz

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

Cpt S 122 Data Structures. Course Review Midterm Exam # 1

19-Nov CSCI 2132 Software Development Lecture 29: Linked Lists. Faculty of Computer Science, Dalhousie University Heap (Free Store)

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

Computer Systems Lecture 9

Agenda. CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language 8/29/17. Recap: Binary Number Conversion

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36

CS 261 Fall Mike Lam, Professor. Structs and I/O

Dynamic Allocation in C

Common Misunderstandings from Exam 1 Material

CS 61c: Great Ideas in Computer Architecture

Pointers. 10/5/07 Pointers 1

CS C Primer. Tyler Szepesi. January 16, 2013

CSE2421 Systems1 Introduction to Low-Level Programming and Computer Organization

COMPSCI 210 Part II Data Structure

CS3157: Advanced Programming. Announcement

CSE 509: Computer Security

ENEE150 Final Exam Review

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings

Review of the C Programming Language for Principles of Operating Systems

C Language, Token, Keywords, Constant, variable

TDDB68 Concurrent Programming and Operating Systems. Lecture 2: Introduction to C programming

Variables in C. Variables in C. What Are Variables in C? CMSC 104, Fall 2012 John Y. Park

C mini reference. 5 Binary numbers 12

Programming in C - Part 2

CSE 333 Lecture 2 Memory

JTSK Programming in C II C-Lab II. Lecture 3 & 4

BLM2031 Structured Programming. Zeyneb KURT

C: Arrays, and strings. Department of Computer Science College of Engineering Boise State University. September 11, /16

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

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

Homework 3 CS161 Computer Security, Fall 2008 Assigned 10/07/08 Due 10/13/08

Transcription:

CS240: Programming in C Lecture 11: Bit fields, unions, pointers to functions Cristina Nita-Rotaru Lecture 11/ Fall 2013 1

Structures recap 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 Cristina Nita-Rotaru Lecture 11/ Fall 2013 2

Structure recap Member access Direct: s.member Indirect: s_ptr->member Dot operator. has precedence over indirection -> : agenda.contact->name Use const to make a structure read-only Cristina Nita-Rotaru Lecture 11/ Fall 2013 3

Memory layout for a structure Data alignment: when cpu accesses the memory reads more than one byte, usually 4 bytes on a 32-bit platform. What is the data structure is not a multiple of 4? Padding. Many computer languages and computer language implementations handle data alignment automatically. Cristina Nita-Rotaru Lecture 11/ Fall 2013 4

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) {. Cristina Nita-Rotaru Lecture 11/ Fall 2013 5

Unions They can hold different type of values at different times Definition is similar with 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. Cristina Nita-Rotaru Lecture 11/ Fall 2013 6

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 Cristina Nita-Rotaru Lecture 11/ Fall 2013 7

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. Cristina Nita-Rotaru Lecture 11/ Fall 2013 8

Unions: examples union number { ; int ival; float fval; double dval; Union can be member of a structure struct { int type; n; union number { int ival; float fval; double dval; value; Cristina Nita-Rotaru Lecture 11/ Fall 2013 9

Example: #include <stdio.h> typedef enum { INT, FLOAT, DOUBLE my_type; struct my_number{ my_type type; union { int ival; float fval; double dval; value; ; void initialize_my_number(struct my_number * n, int ival) { n->type = INT; n->value.ival = ival; void print_my_number(struct my_number n) { switch (n.type){ case INT: printf("%d\n", n.value.ival); break; case FLOAT: printf("%f\n", n.value.fval); break; case DOUBLE: printf("%lf\n", n.value.dval); break; default: printf("unknown type\n"); break; Cristina Nita-Rotaru Lecture 11/ Fall 2013 10

Example (cont.) int main() { struct my_number i; initialize_my_number(&i, 12); print_my_number(i); return 0; Cristina Nita-Rotaru Lecture 11/ Fall 2013 11

Memory layout for a process The operating system creates a process by assigning memory and other resources" Stack: keeps track of the point to which each active subroutine should return control when it finishes executing; stores variables that are local to functions Heap: dynamic memory for variables that are created with malloc, calloc, realloc and disposed of with free Data: initialized variables including global and static variables, un-initialized variables Code: the program instructions to be executed Virtual Memory Stack Heap Data Code Cristina Nita-Rotaru Lecture 11/ Fall 2013 12

Java vs C Structures: Example Java Example: class Slot { int x; int y; int direction; methods... In C: struct Slot { int x; int y; int direction; ; What about functions??? Cristina Nita-Rotaru Lecture 11/ Fall 2013 13

Pointers to functions Code resides in memory Function Pointers are pointers which point to the address of a function. Function pointers are variables. A function pointer always points to a function with a specific prototype, i.e. same parameters and return-type! int (*Function_ptr)(int, char); Cristina Nita-Rotaru Lecture 11/ Fall 2013 14

Why do we need function pointers? Functions as arguments to other functions: sort routine where the main mechanism for sorting is passed as a comparison function by the caller (354) Callback Functions: functions that are invoked when a particular event happens. Useful in networking or graphic applications (354) Cristina Nita-Rotaru Lecture 11/ Fall 2013 15

Working with pointers to functions Declaration and initialization int (*Function_ptr)(int, char*) = NULL; Assignment int print_error(int n, char* str){ ; printf( Error (%d): %s\n, n, str); return 0; Function_ptr = print_error; Function_ptr = &print_error; Calling a function pointer int ret = (*Function_ptr)(4, Exit\n ); Cristina Nita-Rotaru Lecture 11/ Fall 2013 16

Working with function pointers Pass a function pointer as argument void SomeFunc(int (*ptrfunc)(int, char*)){ int result = (*ptrfunc)(1, OK ); SomeFunc(&print_error); Return a function pointer typedef int(*ptrfun)(int, char*); ptrfun GetPrint(int type){ if(type == DETAILED) else return &print_details; return &print_summary; Cristina Nita-Rotaru Lecture 11/ Fall 2013 17

Pointers to functions and structures Function pointers can be members of structures. struct Slot { ; int x; int y; int direction; void (*print)(slot *s); Cristina Nita-Rotaru Lecture 11/ Fall 2013 18

Example #include <stdio.h> typedef void (*ptrfun)(int, char*); int main() { ptrfun f = NULL; void print_error(int n, char* str){ printf("error (%d): %s\n", n, str); f = &print_error; (*f)(10, "Call print_error"); void print_message(int n, char *str) { printf("message(%d): %s\n", n, str); void PassFunctionPointer(ptrFun g, int n, char *s) { (*g)(n, s); f = &print_message; (*f)(11, "Call print_message"); PassFunctionPointer(&print_error, 12, "Passing print_error"); PassFunctionPointer(&print_message, 13, "Passing print_message"); return 0; Cristina Nita-Rotaru Lecture 11/ Fall 2013 19

Readings and exercises for this lecture K&R Chapter 5.11, 6.8, 6.9 Code all the examples in the lecture. Cristina Nita-Rotaru Lecture 11/ Fall 2013 20