The Three Attributes of an Identifier

Slides:



Advertisements
Similar presentations
1 Storage Duration and Scope –Local and global variables Storage classes –automatic, static, external, register Todays Material.
Advertisements

Chapter 7: User-Defined Functions II
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
FunctionsFunctions Systems Programming. Systems Programming: Functions 2 Functions   Simple Function Example   Function Prototype and Declaration.
Run time vs. Compile time
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Methods of variable creation Global variable –declared very early stage –available at all times from anywhere –created at the start of the program, and.
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
Storage & Linkage: Effects on Scope Rudra Dutta CSC Spring 2007, Section 001.
Scope.
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
C++ for Engineers and Scientists Third Edition
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
Copyright 2001 Oxford Consulting, Ltd1 January Storage Classes, Scope and Linkage Overview Focus is on the structure of a C++ program with –Multiple.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
Chapter 6: Modularity Using Functions. In this chapter, you will learn about: – Function and parameter declarations – Returning a single value – Returning.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Chapter 6: User-Defined Functions
Functions Kernighan/Ritchie: Kelley/Pohl: Chapter 4 Chapter 5.
18. DECLARATIONS.
Learners Support Publications Classes and Objects.
CS 2130 Lecture 5 Storage Classes Scope. C Programming C is not just another programming language C was designed for systems programming like writing.
C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
+ Storage Classes and Linkage. + Introduction Scope describe the region or regions of a program that can access and identifier Variables can be shared.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
CHAPTER 8 Scope, Lifetime, and More on Functions.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
Revisiting building. Preprocessing + Compiling 2 Creates an object file for each code file (.c ->.o) Each.o file contains code of the functions and structs.
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
C Part 2 Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens The Three Attributes of an Identifier Identifiers have three essential.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Functions Students should understand the concept and basic mechanics of the function call/return pattern from CS 1114/2114, but some will not. A function.
The Three Attributes of an Identifier
Chapter 6 CS 3370 – C++ Functions.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
CSE 374 Programming Concepts & Tools
Functions and an Introduction to Recursion
C Functions -Continue…-.
Type Checking, and Scopes
CS 326 Programming Languages, Concepts and Implementation
The Three Attributes of an Identifier
A Lecture for the c++ Course
Chapter 5 Conclusion CIS 61.
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Chapter 5 - Functions Outline 5.1 Introduction
6 Chapter Functions.
Classes and Objects.
Scope Rules and Storage Types
Based on slides created by Bjarne Stroustrup & Tony Gaddis
1-6 Midterm Review.
Scope of Identifier The Scope of an identifier (or named constant) means the region of program where it is legal to use that.
Functions Students should understand the concept and basic mechanics of the function call/return pattern from CS 1114/2114, but some will not. A function.
More C++ Classes Systems Programming.
SPL – PS3 C++ Classes.
Presentation transcript:

The Three Attributes of an Identifier What is an identifier? - name of a variable - name of a type - name of a function Identifiers have three essential attributes: - storage duration (variables only) - scope - linkage

Declarations and Definitions A declaration specifies the interpretation and attributes of a set of identifiers. A definition of an identifier is a declaration for that identifier that: - for an object, causes storage to be reserved for that object; - for a function, includes the function body; - for an enumeration constant, is the (only) declaration of the identifier; - for a typedef name, is the first (or only) declaration of the identifier.

Storage Duration storage duration static is used in multiple senses in C: - static storage duration, which may or may not involve the word "static" - static applied to a file-scoped identifier, making it private to the file (internally linked) - static vs dynamic memory allocation We don't talk about all those concepts at once, but it's important to remind students of the various distinctions, since the overloading of the term is confusing to them. storage duration determines when, during execution of a program, memory is set aside for the variable and when that memory is released automatic duration - storage is allocated when the surrounding block of code is executed - storage is automatically deallocated when the block terminates static duration - storage is allocated when execution begins - variable stays in the same storage location as long as the program is running - variable can retain its value indefinitely (until program terminates)

Automatic Storage Duration automatic duration - storage is allocated when the surrounding block of code is executed - storage is automatically deallocated when the block terminates ... void Sort(int list[], int Sz) { int startIdx = 0; } default for variables that are declared inside a block created (memory allocated) on each call initialized on each call deallocated (memory reclaimed) when call ends

Static Storage Duration static duration - storage is allocated when execution begins - variable stays in the same storage location as long as the program is running - variable can retain its value indefinitely (until program terminates) int numCallsToSort = 0; ... void Sort(int list[], int Sz) { static int numSwaps = 0; } default for variables declared outside all blocks initialized once, keeps its value until program ends variable is declared inside a block, with keyword static initialized once, keeps its value from one call to the next

Scope One might expect the students to understand the concept of scope from their previous courses in Java. One would be disappointed; it seems this idea is not really discussed formally in those courses. The primary distinction (vs Java) is most likely the notion of block scope in C in conjunction with the notion of name hiding. I would not dwell on this; IMO the best practice is to not employ duplicate names in nested scopes in C anyhow. scope (of an identifier) the range of program statements within which the name is recognized as a valid name block scope - name is visible from its point of declaration to the end of the enclosing block - place declaration of name within a block file scope - name is visible from its point of declaration to the end of the enclosing file - place declaration of name outside of all blocks (typically before any blocks)

Block Scope block scope - name is visible from its point of declaration to the end of the enclosing block - place declaration of name within a block void Sort(int list[], int Sz) { static int numSwaps = 0; int startIdx = 0; for ( ... ) { int stopIdx = ...; } ... return; name is declared inside a block name can only be referred to from declaration to end of block

File Scope file scope - name is visible from its point of declaration to the end of the enclosing file - place declaration of name outside of all blocks (typically before any blocks) name declared outside all blocks name can be referred to from any function within the file potentially dangerous avoid unless necessary pass parameters instead int numCallsToSort = 0; ... void Sort(int list[], int Sz) { }

Linkage This is the hardest concept for the students. Actually, I think it's widely misunderstood, or at least misused, by C programmers. The advice I would give is that any file-scoped identifier that does not need to be referenced from another file should always be declared with "static" so it has internal linkage. linkage determines the extent to which the name can be shared by different parts of the program external linkage - name may be shared by several (or all) files in the program internal linkage - name is restricted to a single file, but shared by all functions within that file no linkage - name is restricted to a single function

External Linkage external linkage - name may be shared by several (or all) files in the program int numCallsToSort = 0; ... void Sort(int list[], int Sz) { } name is declared outside all blocks name can be referred to from other files potentially very dangerous use only if necessary

Internal Linkage internal linkage - name is restricted to a single file, but shared by all functions within that file static int numCallsToSort = 0; ... void Sort(int list[], int Sz) { } name is declared outside all blocks, using reserved word static name cannot be referred to from other files potentially dangerous use only if necessary

No Linkage no linkage - name is restricted to a single function ... void Sort(int list[], int Sz) { static int numSwaps = 0; int startIdx = 0; } name is declared inside a block name can only be referred to within the block where the declaration is placed

Determining the Attributes The default storage duration, scope and linkage of a variable depend on the location of its declaration: inside a block automatic storage duration, block scope, no linkage outside any block static storage duration, file scope, external linkage When the defaults are not satisfactory, see: auto static extern register

Global Symbols? global symbol - name has external linkage - references to the name are handled by the linker - compiler and linker have relevant rules… for function names… for variable names function names: - functions not defined locally are assumed to be external; so compiler leaves resolving them to the linker - triggers implicit declaration warning, and frequently link-time errors when actual function interface doesn't match implicit one - global functions should be declared in header files variable names: - variables not declared locally are treated as errors - must use "extern" declaration for global variables defined elsewhere

Function Names static void func1( ) { . . . } defines file-local symbol func1; not global void func2( ) { . . . } defines (strong) global symbol func2 static void func3( ); defines no symbol; declares func3 void func4( ); extern void func4( ); makes e an external reference all examples are at file scope

Variable Names static int x = 4; static int y; defines file-local symbols x and y int w; defines weak global symbol aka common symbol int z = 4; defines strong global symbol extern int ext; ext is defined somewhere else all examples are at file scope

Use Case: Global Type Name struct _Rational { int64_t top; int64_t bottom; }; typedef struct _Rational Rational; Rational.h Data types are typically needed throughout an implementation, and so must be global. The type declaration is usually placed in a suitable header file so it can be included as needed.

Use Case: Global Function Name Rational Rational_Add(Rational left, Rational right); Rational.h Used for any function that needs to be called from other modules. Very common. Place function declaration in suitable header file.

Use Case: Global Variable Name // no example given . . . almost always a bad idea Unlike types and functions, variables are mutable. Making a variable global allows modifications to it to be made from anywhere.

Use Case: File-local Type Name struct _indexEntry { uint32_t location; int32_t key; }; typedef struct _indexEntry indexEntry; static indexEntry Index[maxEntries]; uint32_t findEntry(int32_t keyValue) { . . . } Index.c Here, we create an array to index a collection of records. The index uses objects that only make sense locally, so the type is file-local.

Use Case: File-local Variable Name struct _indexEntry { uint32_t location; int32_t key; }; typedef struct _indexEntry indexEntry; static indexEntry Index[maxEntries]; uint32_t findEntry(int32_t keyValue) { . . . } Index.c The array that holds the index entries is file-local, so various search and mutator functions can access it directly. The search function shown here would be declared in a header file, so it is global.

Use Case: File-local Variable Name . . . static indexEntry Index[maxEntries]; Index.c But… why make the array file-local? Why not make it local to a function? Answer: we need to populate the array with entries as we build the index, and the index (the array) needs to be persistent. If we made the array local to a function - it could only be accessed from within that function - it would cease to exist when that function returned

Use Case: File-local Variable Name . . . static indexEntry Index[maxEntries]; Index.c Why not make the array global? Answer: we want to restrict modifications to the array. If we made the array global, it could be changed from anywhere in the program.

Use Case: File-local Function Name static Rational Rational_Reduce(Rational original); . . . static Rational Rational_Reduce(Rational original) { } Rational.c Used for any function that needs to be called only from the current module. Place function declaration suitable .c file, and make the function static.