The New C Standard (Excerpted material)

Similar documents
The New C Standard (Excerpted material)

The New C Standard (Excerpted material)

The New C Standard (Excerpted material)

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

The New C Standard (Excerpted material)

The New C Standard (Excerpted material)

The New C Standard (Excerpted material)

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

The New C Standard (Excerpted material)

EL2310 Scientific Programming

Type Checking. Prof. James L. Frankel Harvard University

Contents of Lecture 3

The New C Standard (Excerpted material)

CSCI 171 Chapter Outlines

SSE and SSE2. Timothy A. Chagnon 18 September All images from Intel 64 and IA 32 Architectures Software Developer's Manuals

Final CSE 131B Spring 2004

The New C Standard (Excerpted material)

CS504 4 th Quiz solved by MCS GROUP

Pragma intrinsic and more

The New C Standard (Excerpted material)

V850 Calling Convention

The New C Standard (Excerpted material)

Data Representation and Storage. Some definitions (in C)

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

Adding Alignment Support to the C++ Programming Language / Consolidated

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

High Performance Computing and Programming, Lecture 3

CS240: Programming in C. Lecture 2: Overview

TMS470 ARM ABI Migration

Review of the C Programming Language for Principles of Operating Systems

Heap Arrays. Steven R. Bagley

Review of the C Programming Language

Introduction to C++ with content from

The New C Standard (Excerpted material)

Francesco Nidito. Programmazione Avanzata AA 2007/08

Topics Power tends to corrupt; absolute power corrupts absolutely. Computer Organization CS Data Representation

Midterm CSE 131B Spring 2005

QUIZ. What is wrong with this code that uses default arguments?

A Fast Review of C Essentials Part I

BLM2031 Structured Programming. Zeyneb KURT

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

FUNCTION POINTERS. int (*pf)(); // pf is a pointer to a function returning an int.

CSE 333 Lecture 2 Memory

QUIZ. What are 3 differences between C and C++ const variables?

Midterm CSE 131 Winter 2012

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

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

Recitation #11 Malloc Lab. November 7th, 2017

Computers Programming Course 5. Iulian Năstac

printf( Please enter another number: ); scanf( %d, &num2);

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

G52CPP C++ Programming Lecture 7. Dr Jason Atkin

CS201 Some Important Definitions

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

Variation of Pointers

DSP Mapping, Coding, Optimization

PERIYAR CENTENARY POLYTECHNIC COLLEGE Periyar Nagar- Vallam Thanjavur

CS429: Computer Organization and Architecture

CS240: Programming in C

SH-5 Generic and C Specific ABI

ELEC 377 C Programming Tutorial. ELEC Operating Systems

CS 314 Principles of Programming Languages. Lecture 11

Lecture 3: C Programm

The New C Standard (Excerpted material)

18-642: Code Style for Compilers

Motivation was to facilitate development of systems software, especially OS development.

The New C Standard (Excerpted material)

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

Computer System and programming in C

CSC2/454 Programming Languages Design and Implementation Records and Arrays

CSE 230 Intermediate Programming in C and C++

Hardware dependencies

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

Preview from Notesale.co.uk Page 6 of 52

CS241 Computer Organization Spring Data Alignment

The Gap Between the Virtual Machine and the Real Machine. Charles Forgy Production Systems Tech

Section Notes - Week 1 (9/17)

ATOMS. 3.1 Interface. The Atom interface is simple: atom.h #ifndef ATOM_INCLUDED #define ATOM_INCLUDED

Introduction to C. Robert Escriva. Cornell CS 4411, August 30, Geared toward programmers

Overview Implicit Vectorisation Explicit Vectorisation Data Alignment Summary. Vectorisation. James Briggs. 1 COSMOS DiRAC.

Pointers, Dynamic Data, and Reference Types

Why Study Assembly Language?

High Performance Computing: Tools and Applications

Run-time Environments - 3

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

CS Programming In C

Dynamic Data Structures. CSCI 112: Programming in C

Chapter 8 :: Composite Types

Chapter 1 GETTING STARTED. SYS-ED/ Computer Education Techniques, Inc.

Motivation was to facilitate development of systems software, especially OS development.

FUNCTIONS POINTERS. Pointers. Functions

CSC C69: OPERATING SYSTEMS

Introduction to C. Zhiyuan Teo. Cornell CS 4411, August 26, Geared toward programmers

The New C Standard (Excerpted material)

Special Topics: Programming Languages

G52CPP C++ Programming Lecture 8. Dr Jason Atkin

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

COSC 2P95. Procedural Abstraction. Week 3. Brock University. Brock University (Week 3) Procedural Abstraction 1 / 26

Data in Memory. variables have multiple attributes. variable

Transcription:

The New C Standard (Excerpted material) An Economic and Cultural Commentary Derek M. Jones derek@knosof.co.uk Copyright 2002-2008 Derek M. Jones. All rights reserved.

39 3.2 3.2 additive operators pointer to object pointer to void same representation and as common initial sequence requirement that objects of a particular type be located on storage boundaries with addresses that are particular multiples of a byte address Commentary In an ideal world there would be no requirements. In practice the designers of some processors have placed restrictions on the fetching of variously sized objects from storage. The underlying reason for these restrictions is to simplify (reduce cost) and improve the performance of the processor. Some processors do not have requirements, but may access storage more quickly if the object is aligned on a particular address boundary. The requirement that a pointer to an object behave the same as a pointer to an array of that object s type forces the requirement that sizeof(t) be a multiple of the of T. If two objects, having different types, have the same requirements then their addresses will be located on the same multiple of a byte address. Objects of character type have the least restrictive requirements, compared to objects of other types. Alignment and padding are also behind the assumptions that need to be made for the common initial sequence concept to work. The extent to which causes implementation-defined or undefined behavior is called out for each applicable construct. Various issues relating to were the subject of DR #074. C++ 39 3.9p5 Object types have requirements (3.9.1, 3.9.2). The of a complete object type is an implementation-defined integer value representing a number of bytes; an object is allocated at an address that meets the requirements of its object type. addressable storage unit Motorola 56000 There is no requirement on a C implementation to document its requirements. Other Languages Most languages hide such details from their users. Common Implementations Alignment is a very important issue for implementations. Internally it usually involves trade-offs between storage and runtime efficiency. Externally it is necessary to deal with parameter passing interfaces and potentially the layout of structure members. The C language is fortunate in that many system interfaces are specified in terms of C. This means that other languages have to interface to its way of doing things, not the other way around. Optimal code generation for modern processors requires implementations to consider issues associated with cache lines. Techniques for finding and using the optimal memory for the objects used in a program is an active research area. [3] The Motorola 56000 [4] allows its modulo arithmetic mode to be applied to pointer arithmetic operations (used to efficiently implement a circular buffer; the wraparound effectively built into the access instruction). The one requirement is that the storage be aligned on an address boundary that is a power of two greater than or equal to the buffer size. So an array of 10 characters needs to be placed on a 16-byte boundary, while an array of 100 characters would need to be on a 128-byte boundary (assuming they are to be treated as circular buffers). Translators for such processors often provide an additional type specifier to enable developers to indicate this usage. The Intel XScale architecture [2] has data cache lines that start on a 32-byte address boundary. Arranging for aggregate objects, particularly arrays, to have such an will help minimize the number of stalls while the processor waits for data, and it also enables optimal use of the prefetch instruction. The Intel x86 2 v 1.1 January 30, 2008

3.2 39 architecture has no requirement, but can load multibyte objects more quickly if appropriately aligned. RISC processors tend to have strict requirements, often requiring that scalar objects be aligned on a byte boundary that is a multiple of their size. Does the storage class or name space of an identifier affect its? Many implementations make no attempt to save storage, for objects having static storage duration, by packing them close together. It is often simpler to give them all the same, worst-case,. For hosted implementations potential savings (e.g., storage saved, time to access) may be small; there don t tend to be significant numbers of scalar objects with static storage duration. However, in those cases where freestanding environments have limited storage availability, vendors often go to great lengths to make savings. The of objects having static storage duration is sometimes controlled by the linker, which may need to linkers consider more systemwide requirements than a C translator (e.g., handling other languages or program image restrictions). For example, the C translator may specify that all objects start on an even address boundary; objects with automatic storage duration being placed on the next available even address after the previous object, but the linker using the next eighth-byte address boundary for objects. A host s parameter-passing mechanism may have a minimum requirement, for instance at least the of int, and stricter for types wider than an int. Minimizing the padding between different objects with automatic storage duration reduces the stack overhead for the function that defines them. It also helps to ensure they all fit within any short addressing modes available within the instruction set of the host processor. Unlike objects with static storage duration, most translators attempt to use an that is appropriate to the type of the object. The malloc function does not know the effective type assigned to the storage it allocates. It has to make worst-case assumptions and allocate storage based on the strictest requirements. The members of a structure type are another context where requirements can differ from the of objects having the same type in non-member contexts. member Most processor instructions operate on objects having a scalar type and any requirements usually only apply to scalar types. However, some processors contain instructions that operate on objects that are essentially derived types and these also have requirements (e.g., the Pentium streaming SIMD derived type instructions [1] ). The multiples that occur in requirements are almost always a power of two. On some processors the multiple of a scalar type is the same as its size, in bytes. Alignment requirements are not always constant during translation. Several implementations provide #pragma directives that control the used by translators. A common vendor extension for controlling the of structure members is the #pack preprocessor directive. gcc supports several extensions for getting and setting information. The keyword alignof, which returns the requirement of its argument, for instance, alignof (double) might return 8. The keyword attribute can be used to specify an object s at the point of definition: 1 int x attribute ((aligned(16))) = 0; Coding Guidelines Alignment is an issue that can affect program resource requirements, program interoperability, and source code portability. Alignment can affect the size of structure types. Two adjacent members declared with different scalar types may have padding inserted between them. Ordering members such that those with the same type are adjacent to each other can eliminate this padding (because the of a scalar type is often a multiple of its size). If many instances of an object having a particular structure type are created a large amount of storage may be consumed and developers may consider it to be worthwhile investigating ways of making savings; for instance, by changing the default settings, or by using a vendor-supplied #pragma. [5] If 39 January 30, 2008 v 1.1 3

39 3.2 identifier declared in one file?? storage layout pointer to void converted to/from pointer to structures represen-?tation information using is changed for a subset of structures there is always the danger that a declaration in one translation unit will have an that differs from the others. Adhering to the guideline recommendation on having a single point of declaration reduces the possibility of this occurring. Some applications chose to store the contents of objects having structure types, in binary form, in files as a form of data storage. This creates a dependency on an implementation and the storage layout it uses for types. Other programs that access these data files, or even the same program after retranslation, need to ensure that the same structure types have the same storage layout. This is not usually a problem when using the same translator targeting the same host. However, a different translator, or a different host, may do things differently. The design decisions behind the choice of data file formats is outside the scope of these coding guidelines. Programs that cast pointers to point at different scalar types are making use of requirements information, or lack of them. Porting a program which makes use of such casts to a processor with stricter requirements, is likely to result in some form of signal being raised (most processors raise some form of exception when an attempt is made to access objects on misaligned addresses). The cost of modifying existing code to work on such a processor is one of the factors that should have been taken into account when considering the original decision to allow a guideline deviation permitting the use of such casts. Developers sometimes write programs that rely on different types sharing the same requirements, e.g., int and long. There are cases where the standard guarantees identical s, but in general there are no such guarantees. Such usage is making use of representation information and is covered by a guideline recommendation. Example Do all the following objects have the same? 1 #include <stdlib.h> 2 /* 3 * #pragma pack 4 */ 5 6 struct S_1 { 7 unsigned char mem_1; 8 int mem_2; 9 float mem_3; 10 }; 11 12 static unsigned char file_1; 13 static int file_2; 14 static float file_3; 15 16 void f(unsigned char param_1, 17 int param_2, 18 float param_3) 19 { 20 unsigned char local_1, *p_uc; 21 int local_2, *p_i; 22 float local_3, *p_f; 23 24 p_uc = (unsigned char *)malloc(sizeof(unsigned char)); 25 p_i = (int *)malloc(sizeof(int)); 26 p_f = (float *)malloc(sizeof(float)); 27 } No, they need not, although implementations often chose to use the same requirements for each scalar type, independent of the context in which it occurs. The malloc function cannot know the use to which the storage it returns will be put, so it has to make worst-case assumptions. 1 /* 2 * Declare a union containing all basic types and pointers to them. 4 v 1.1 January 30, 2008

3.2 39 3 */ 4 union align_u { 5 char c, *cp; 6 short h, *hp; 7 int i, *ip; 8 long l, *lp; 9 long long ll, *llp; 10 float f, *fp; 11 double d, *dp; 12 long double ld, *ldp; 13 void *vp; 14 /* 15 * Pointer-to function. The standard does not define any generic 16 * pointer types, like it does for pointer to void. The wording 17 * only requires the ability to convert. There is no requirement 18 * to be able to call the converted function pointer. 19 */ 20 void (*fv)(void); 21 void (*fo)(); 22 void (*fe)(int,...); 23 }; 24 /* 25 * In the following structure type, the first member has type char. 26 * The second member, a union type, will be aligned at least to the 27 * strictest of its contained types. Assume that there 28 * is no additional padding at the end of the structure declaration 29 * than in the union. Then we can calculate the strictest 30 * required by any object type (well at least those used to 31 * define members of the union). 32 */ 33 struct align_s { 34 char c; 35 union align_u u; 36 }; 37 38 #define _ALIGN_ (sizeof(struct _align_s) - sizeof(union _align_u)) January 30, 2008 v 1.1

References 1. Intel. Data Alignment and Programming Issues for the Streaming SIMD Extensions with the Intel C/C++ Compiler. Intel Corporation, 1.1 edition, Jan. 1999. 2. Intel. Intel XScale Microarchitecture Programmers Reference Manual. Intel, Inc, 2001. 3. S. Larsen, E. Witchel, and S. Amarasinghe. Techniques for increasing and detecting memory. Technical Report MIT-LCS-TM- 621, MIT, USA, Nov. 2001. 4. Motorola, Inc. DSP5600 24-bit Digital Signal Processor Family Manual. Motorola, Inc, Austin, TX, USA, dsp56kfamum/ad edition, 1995. 5. Sun. C User s Guide. Sun Microsystems, Inc, Palo Alto, CA, USA, revision a edition, May 2000. v 1.1 January 30, 2008