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

Similar documents
Data Representation and Storage. Some definitions (in C)

Computer Systems Principles. C Pointers

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

Data Representation and Storage

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

Syntax and Variables

CS240: Programming in C

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

Data Structures Unit 02

C Praktikum. Advanced Pointers. Eugen Betke, Nathanael Hübbe, Michael Kuhn, Jakob Lüttgau, Jannek Squar

High Performance Computing in C and C++

Final CSE 131B Spring 2004

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

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

DEPARTMENT OF MATHS, MJ COLLEGE

DETAILED SYLLABUS INTRODUCTION TO C LANGUAGE

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

CS429: Computer Organization and Architecture

EL6483: Brief Overview of C Programming Language

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

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Undefinedness and Non-determinism in C

Recitation #11 Malloc Lab. November 7th, 2017

Types, Variables, and Constants

Stream Model of I/O. Basic I/O in C

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

Lecture 02 C FUNDAMENTALS

Programming in C - Part 2

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

Introduction to C++ with content from

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

6.096 Introduction to C++ January (IAP) 2009

Understanding Pointers

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

More in-class activities / questions

Dept. of Computer and Information Science (IDA) Linköpings universitet Sweden

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

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

Physics 2660: Fundamentals of Scientific Computing. Lecture 3 Instructor: Prof. Chris Neu

CS 61c: Great Ideas in Computer Architecture

ENERGY 211 / CME 211. Functions

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

A Fast Review of C Essentials Part I

We would like to find the value of the right-hand side of this equation. We know that

C Programming Review CSC 4320/6320

Computer System and programming in C

CS 61c: Great Ideas in Computer Architecture

>B<82. 2Soft ware. C Language manual. Copyright COSMIC Software 1999, 2001 All rights reserved.

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

G52CPP C++ Programming Lecture 8. Dr Jason Atkin

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

PRINCIPLES OF OPERATING SYSTEMS

OBJECT ORIENTED PROGRAMMING USING C++

Programming and Data Structures

Fundamentals of Programming Session 19

Contents of Lecture 3

Programming. Structures, enums and unions

Tokens, Expressions and Control Structures

ESC101N Fundamentals of Computing

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

CSE 333 Lecture 2 Memory

CS3157: Advanced Programming. Announcement

OBJECT ORIENTED PROGRAMMING

C Syntax Out: 15 September, 1995

CS61C Midterm Review on C & Memory Management

Computers Programming Course 5. Iulian Năstac

3/7/2018. Sometimes, Knowing Which Thing is Enough. ECE 220: Computer Systems & Programming. Often Want to Group Data Together Conceptually

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

CSCI 171 Chapter Outlines

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

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

Review of the C Programming Language for Principles of Operating Systems

Topic 6: A Quick Intro To C

Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and

Programming for Engineers Structures, Unions

Short Notes of CS201

Do not start the test until instructed to do so!

Final CSE 131B Spring 2005

C Introduction. Comparison w/ Java, Memory Model, and Pointers

Type Checking. Prof. James L. Frankel Harvard University

Midterm CSE 131B Spring 2005

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

12. Pointers Address-of operator (&)

Arrays and Pointers in C. Alan L. Cox

CS201 - Introduction to Programming Glossary By

Structures and Unions in C. Alan L. Cox

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

CS240: Programming in C

COMP 2355 Introduction to Systems Programming

ANSI C Reserved Words

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar..

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

DECLARATIONS. Character Set, Keywords, Identifiers, Constants, Variables. Designed by Parul Khurana, LIECA.

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

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

Fundamental of Programming (C)

Carnegie Mellon. Cache Lab. Recitation 7: Oct 11 th, 2016

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

Midterm CSE 131 Winter 2014

Transcription:

Structures Proseminar C Grundlagen und Konzepte Michael Kuhn Research Group Scientific Computing Department of Informatics Faculty of Mathematics, Informatics und Natural Sciences University of Hamburg 2013-05-10 1 / 15

Overview 1 Structures and Unions 2 Alignment, Padding and Bit Fields 3 Access and Initialization 4 Compound Literals 5 Opaque Structures 6 Summary 2 / 15

struct structs can contain multiple variables Potentially different data types The contained variables are called members 1 struct foo1_ s 3 double bar ; 4 int baz ; 5 }; 1 sizeof ( double ) == 8 2 sizeof ( int ) == 4 3 sizeof ( struct foo1_ s ) == 12 3 / 15

union unions look the same as structs They only take up the space required by the largest member Only one member is valid at any time 1 union foo1_ u 3 double bar ; 4 char baz [8]; 5 }; 1 sizeof ( double ) == 8 2 sizeof ( char [8]) == 8 3 sizeof ( union foo1_ u ) == 8 4 / 15

typedef Writing struct foo1 s every time can be cumbersome typedef allows defining a new data type Done implicitly in C++ 1 struct foo1_ s 3 double bar ; 4 int baz ; 5 }; 6 7 typedef struct foo1_ s foo1_ s ; 8 9 struct foo1_ s a; 10 foo1_s b; 5 / 15

Introduction The compiler aligns struct members for optimal access This can lead to padding 1 struct foo2_ s 3 char bar ; 4 int baz ; 5 }; 1 sizeof ( char ) == 1 2 sizeof ( int ) == 4 3 sizeof ( struct foo2_ s ) == 8 baz starts at a 4 byte boundary, wasting 3 bytes after bar Best practice: Group members of same type, from largest to smallest 6 / 15

Bit Fields Bit fields allow limiting the memory occupied by a member Can be used to provide convenient bitwise access 1 struct foo3_ s 3 unsigned int bar : 8; 4 unsigned int flag : 1; 5 unsigned int : 23; 6 }; 7 8 struct foo3_ s a; 9 a. bar = 255; 10 a. flag = 2; /* invalid! */ 1 sizeof ( struct foo3_ s ) == 4 7 / 15

Access Members can be accessed using. and -> a->b is equal to (*a).b 1 struct foo2_ s 3 char bar ; 4 int baz ; 5 }; 6 7 struct foo2_ s a; 8 a. bar = a ; 9 10 struct foo2_s b [1]; 11 b-> baz = 42; 8 / 15

Initialization structs can also be directly initialized Unspecified members are initialized to zero 1 struct foo2_ s 3 char bar ; 4 int baz ; 5 }; 6 7 struct foo2_s a = { a, 42 }; 8 struct foo2_ s b = {. baz = 23 }; 9 struct foo2_ s c = { 0 }; 9 / 15

Assignments Compound literals have the following form: (type){arguments} 1 struct arg_ s 3 int a; 4 int b; 5 }; 6 7 struct arg_ s a = { 1, 2 }; 8 struct arg_ s b; 9 b = { 1, 2 }; /* invalid! */ 10 b = ( struct arg_s ){ 1, 2 }; 10 / 15

Assignments, Function Arguments and Return Values 1 struct arg_s * p; 2 p = &{ 1, 2 }; /* invalid! */ 3 p = &( struct arg_s ){ 1, 2 }; 1 static 2 struct arg_ s 3 foo ( struct arg_ s a) 4 { 5 return ( struct arg_s ){ a.a, a.b }; 6 } 7 8 foo ({ 1, 2 }); /* invalid! */ 9 foo (( struct arg_s ){ 1, 2 }); 11 / 15

Declaration Putting struct definitions into header files unnecessarily leaks implementation details to users structs are often used as opaque data types Listing 1: opaque.h 1 struct opaque_s ; 2 typedef struct opaque_s opaque_s ; 3 4 char const * opaque_get_name ( opaque_s *); 12 / 15

Definition The actual struct contents and implementation are hidden Listing 2: opaque.c 1 # include " opaque.h" 2 3 struct opaque_s 4 { 5 char * name ; 6 }; 7 8 char const * opaque_get_name ( opaque_s * o) 9 { 10 return o-> name ; 11 } 13 / 15

Summary Structures and unions allow grouping of different data types The compiler aligns and pads structures for optimal access, which can lead to wasted memory Structures can be initialized by simply listing values for all members or by using designated initializers Compound literals allow assigning structures directly and passing anonymous structures to functions Opaque structures can be used to separate the interface from the implementation 14 / 15

Bonus: Strict Aliasing The compiler can assume that two objects of different data types do not reside at the same memory address That is, they do not alias each other 1 uint16_t a [2] = { 1, 2 }; 2 *( uint32_t *) a = 42; 3 4 printf ("%d %d\n", a[0], a [1]) ; 1 42 0 1 warning : dereferencing type - punned pointer will break strict - aliasing rules [- Wstrict - aliasing ] 15 / 15