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)

The New C Standard (Excerpted material)

The New C Standard (Excerpted material)

The New C Standard (Excerpted material)

The New C Standard (Excerpted material)

The New C Standard (Excerpted material)

The New C Standard (Excerpted material)

The New C Standard (Excerpted material)

The New C Standard (Excerpted material)

The New C Standard (Excerpted material)

Programming languages - C

The New C Standard (Excerpted material)

The New C Standard (Excerpted material)

The New C Standard (Excerpted material)

The New C Standard (Excerpted material)

The New C Standard (Excerpted material)

The New C Standard (Excerpted material)

Extended friend Declarations

The New C Standard (Excerpted material)

N1793: Stability of indeterminate values in C11

Proposal to Acknowledge that Garbage Collection for C++ is Possible X3J16/ WG21/N0932. Bjarne Stroustrup. AT&T Research.

Template Issue Resolutions from the Stockholm Meeting

The New C Standard (Excerpted material)

Semantics of Vector Loops

American National Standards Institute Reply to: Josee Lajoie

FORMALIZED SOFTWARE DEVELOPMENT IN AN INDUSTRIAL ENVIRONMENT

Recommended Practice for Software Requirements Specifications (IEEE)

Modules: ADL & Internal Linkage

The New C Standard (Excerpted material)

International Standards Organisation. Parameterized Derived Types. Fortran

Class 15. Object-Oriented Development from Structs to Classes. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Second release of the COMPASS Tool Tool Grammar Reference

Frequently Asked Questions. AUTOSAR C++14 Coding Guidelines

Clarifying the restrict Keyword. Introduction

Ch. 3: The C in C++ - Continued -

Modules:Context-Sensitive Keyword

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

5.Coding for 64-Bit Programs

ISO/IEC PDTS 21425, C++ Extensions for Ranges, National Body Comments

THE EVALUATION OF OPERANDS AND ITS PROBLEMS IN C++

Operator Dot Wording

Semantic constraint matching for concepts

Hacking in C. Pointers. Radboud University, Nijmegen, The Netherlands. Spring 2019

PROGRAMMAZIONE I A.A. 2017/2018

INTERNATIONAL TELECOMMUNICATION UNION

An Alternative to Name Injection from Templates

MISRA C:2012 Technical Corrigendum 1

XC Specification. 1 Lexical Conventions. 1.1 Tokens. The specification given in this document describes version 1.0 of XC.

C++ Module TS Issues List Gabriel Dos Reis Microsoft

Wording for lambdas in unevaluated contexts

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

3.8 Separate Compilation

CS2351 Data Structures. Lecture 7: A Brief Review of Pointers in C

Chapter 9. Def: The subprogram call and return operations of a language are together called its subprogram linkage

Object-Oriented Principles and Practice / C++

The New C Standard (Excerpted material)

Unified concept definition syntax

Programming Languages, Summary CSC419; Odelia Schwartz

The New C Standard (Excerpted material)

Pointers (continued), arrays and strings

Lecture Notes on Static Semantics

ISO. International Organization for Standardization. ISO/IEC JTC 1/SC 32 Data Management and Interchange WG4 SQL/MM. Secretariat: USA (ANSI)

1 Black Box Test Data Generation Techniques

Lecture Notes on Arrays

Functions and Parameter Passing

Mobile Application Programming. Swift Classes

Pointers (continued), arrays and strings

Common Lisp Object System Specification. 1. Programmer Interface Concepts

M.EC201 Programming language

Uncertain Data Models

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

Explicit Conversion Operator Draft Working Paper

System Verilog Tagged Unions and Pattern Matching

static CS106L Spring 2009 Handout #21 May 12, 2009 Introduction

explicit class and default definitions revision of SC22/WG21/N1582 =

OCL Support in MOF Repositories

Message-Passing and MPI Programming

Sardar Vallabhbhai Patel Institute of Technology (SVIT), Vasad M.C.A. Department COSMOS LECTURE SERIES ( ) (ODD) Code Optimization

INTERNATIONAL TELECOMMUNICATION UNION. SERIES X: DATA NETWORKS AND OPEN SYSTEM COMMUNICATIONS Open distributed processing

Principle of Complier Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore

Chapter 7. - FORTRAN I control statements were based directly on IBM 704 hardware

G Programming Languages - Fall 2012

IEEE LANGUAGE REFERENCE MANUAL Std P1076a /D3

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

Part 21: Implementation methods: Clear text encoding of the exchange structure

Merging Modules Contents

System Verilog Tagged Unions and Pattern Matching

Identifier character sequence reuse

Tokens, Expressions and Control Structures

0. Overview of this standard Design entities and configurations... 5

22c:111 Programming Language Concepts. Fall Types I

Guidelines for Writing C Code

Language Extensions for Vector loop level parallelism

COBOL's dominance in underlined by the reports from the Gartner group.

Procedural Programming & Fundamentals of Programming

Stroustrup Default comparisons N4175. Default comparisons. Bjarne Stroustrup

Mobile Application Programming. Swift Classes

Deducing the type of variable from its initializer expression (revision 4)

Transcription:

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

1849 6.9.2 External object s 6.9.2 External object s Semantics external object reserve storage object external translation unit syntax If the declaration of an identifier for an object has file scope and an initializer, the declaration is an external for the identifier. An external is an external declaration that is also a. The presence of an initializer turns a declaration into a, irrespective of its linkage. The of an object causes storage to be reserved for it. The Standard does not define the term external. The object described above is simply called a in. Other Languages The conditions under which a declaration is also a of an object vary between languages, depending on the model of separate translation they use. Common Implementations The base document did not support the use of initializers on declarations that included the extern storageclass specifier. Common usage is for developers to use the term, rather than external, to refer to such declarations. There does not appear to be a worthwhile benefit in attempting to change this common usage. 1848 tentative A declaration of an identifier for an object that has file scope without an initializer, and without a storage-class specifier or with the storage-class specifier static, constitutes a tentative. one external This defines the term tentative (which is only used in this, 6.9.2, subclause). Tentative s are a halfway house. They indicate that a declaration might be a, but the door is left open for a later declaration, in the same translation unit to be the actual or simply another tentative. The concept of tentative was needed, in C90, because of existing code that contained what might otherwise be considered to be multiple s, in the same translation unit, of the same object. Defining and using this concept allowed existing code, containing these apparent multiple s of the same object, in the same translation unit (an external of the same in more than one translation unit is a constraint violation) to be conforming. With two exceptions all external object declarations are tentative s; (1) a declaration that contains an initializer is a, and (2) a declaration that includes the storage-class specifier extern is not a. The Standard does not define the term tentative. Neither does it define a term with a similar meaning. A file scope object declaration that does not include an explicit storage-class specifier is treated, in, as a, not a tentative. A translation unit containing more than one tentative (in C terms) will cause a translator to issue a diagnostic. 1849 1 int glob; 2 int glob; /* does not change the conformance status of program */ 3 // ill-formed program v 1.1 January 30, 2008

6.9.2 External object s 1852 The term tentative is not generally used by developers and tends only to be heard in technical discussion by translator writers and members of the C committee. There does not appear to be a worthwhile benefit in educating developers about this term and the associated concepts. Their current misconceptions (e.g., declarations that include the storage-class specifier static become s at the point of declaration) appear to be harmless. Multiple external-declaration s of the same object are redundant (this general issue is discussed elsewhere), but otherwise harmless (a modification of the type of one of them will result in a diagnostic being redundant code issued unless all of the declarations have compatible type, i.e., they are similarly modified). 1850 If a translation unit contains one or more tentative s for an identifier, and the translation unit contains object implicit no external for that identifier, then the behavior is exactly as if the translation unit contains a file scope declaration of that identifier, with the composite type as of the end of the translation unit, with an initializer equal to 0. This specification requires implementations to create a of an object if the translation unit does not contain one (i.e., there is no declaration of the object that includes an initializer). For an object having an 1848 object external incomplete array type the effect of this specification is to complete the type and define an array having a single element. In the case of objects having an incomplete structure or union type the size of the object is needed to define it, which in turn requires a completed type. Thus, the behavior is undefined if an external object reserve storage object has an incomplete structure or union type at the end of a translation unit. The Standard does not permit more than one in the same translation unit (3.2p1) and so does not need to specify this behavior. It is common practice to omit the initializer in the declaration of an object. Developers invariably assume that an object declaration that omits a storage-class specifier is a (which does eventually become). The fact that an object might not be defined at its point of declaration is purely a technicality. 1853 EXAMPLE tentative array footnote 109 1851 If the declaration of an identifier for an object is a tentative and has internal linkage, the declared object type shall not be an incomplete type. type for internal linkage This requirement applies at the point of declaration, not at the end of the translation unit. The affect of this difference is illustrated by the following example: 1 static char a[]; /* Internal linkage, undefined behavior. */ 2 char b[]; /* External linkage, equivalent to char b[] = {0}; */ One consequence of this requirement is that implementations can allocate storage for objects having internal linkage as soon as the declaration has been processed, during translation. In the case of objects having external linkage the behavior is not undefined if the object has an incomplete array type (see previous C sentence). For objects having no linkage it is a constraint violation if the type of the object is not completed by the end of the declarator. Common Implementations Some implementations (e.g., gcc) support the declaration of objects having a tentative and internal linkage, using an incomplete type (which is completed later in the same translation unit). object type complete by end January 30, 2008 v 1.1

1853 6.9.2 External object s EXAMPLE linkage EXAMPLE 1 int i1 = 1; static int i2 = 2; extern int i3 = 3; int i4; static int i5; int i1; int i2; int i3; int i4; int i5; extern int i1; extern int i2; extern int i3; extern int i4; extern int i5; //, external linkage //, internal linkage //, external linkage // tentative, external linkage // tentative, internal linkage // valid tentative, refers to previous // 6.2.2 renders undefined, linkage disagreement // valid tentative, refers to previous // valid tentative, refers to previous // 6.2.2 renders undefined, linkage disagreement // refers to previous, whose linkage is external // refers to previous, whose linkage is internal // refers to previous, whose linkage is external // refers to previous, whose linkage is external // refers to previous, whose linkage is internal 1852 1 extern int i1; // external linkage 2 extern int i2; // external linkage 3 extern int i3; // external linkage 4 extern int i4; // external linkage 5 extern int i5; // external linkage 6 7 int i1; // external linkage 8 9 int i1 = 1; //, external linkage 10 static int i2 = 2; // internal linkage: undefined behavior -> external linkage on previous declaration 11 extern int i3 = 3; //, external linkage 12 int i4; // external linkage 13 static int i5; // internal linkage: undefined behavior -> external linkage on previous declaration The tentative s are all s with external linkage in. EXAMPLE tentative array EXAMPLE 2 If at the end of the translation unit containing int i[]; the array i still has incomplete type, the implicit initializer causes it to have one element, which is set to zero on program startup. 1853 object def-185inition implicit The implicit initialization (equal to 0), at the end of a translation unit, of the tentative of an object describes an effect. In the case of an object declared to be an array of unknown size, the initializer is treated as specifying a single element. C90 This example was added to the C90 Standard by the response to DR #011. This example is ill-formed. v 1.1 January 30, 2008

6.9.2 External object s 1853 This usage might be considered suspicious in that declaring an object to have an array type containing a single element is unusual and if it was known that only one element was needed why wasn t this information specified in the declaration. If the usage was unintended it is a fault and these coding guidelines are not intended to recommend against the use of constructs that are obviously faults. Any intended usage is likely to guidelines not faults be rare and thus a guideline recommendation (if shown to be cost effective technically) is not cost effective. January 30, 2008 v 1.1

References 1. I. Neamtiu, J. S. Foster, and M. Hicks. Understanding source code evolution using abstract syntax tree matching. In Proceedings of the 2005 International Workshop on Mining Software Repositories, pages 1 5, May 2005. v 1.1 January 30, 2008