The Three Attributes of an Identifier

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

1 Storage Duration and Scope –Local and global variables Storage classes –automatic, static, external, register Todays Material.
Copyright © 2000, Daniel W. Lewis. All Rights Reserved. CHAPTER 9 MEMORY MANAGEMENT.
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
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.
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.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 7: User-Defined Functions II.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
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.
C Lecture Notes Functions (Cont...). C Lecture Notes 5.8Calling Functions: Call by Value and Call by Reference Used when invoking functions Call by value.
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.
Storage Classes.
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.
18-2 Understand “Scope” of an Identifier Know the Storage Classes of variables and functions Related Chapter: ABC 5.10, 5.11.
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 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Functions Kernighan/Ritchie: Kelley/Pohl: Chapter 4 Chapter 5.
18. DECLARATIONS.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
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.
 2007 Pearson Education, Inc. All rights reserved Random Number Generation  rand function – Load – Returns "random" number between
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material.
Dale Roberts CSCI 230 Functions Scope, Parameter Passing, Storage Specifiers Department of Computer and Information Science, School of Science, IUPUI Dale.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
#include using namespace std; // Declare a function. void check(int, double, double); int main() { check(1, 2.3, 4.56); check(7, 8.9, 10.11); } void check(int.
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science) MTech[IT],MPhil (Comp.Sci), MCA, MSc[IT], PGDCA, ADCA, Dc. Sc. & Engg.
CSI 3125, Preliminaries, page 1 Class. CSI 3125, Preliminaries, page 2 Class The most important thing to understand about a class is that it defines a.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
+ Storage Classes and Linkage. + Introduction Scope describe the region or regions of a program that can access and identifier Variables can be shared.
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.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
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.
Functions Course conducted by: Md.Raihan ul Masood
C Functions -Continue…-.
The Three Attributes of an Identifier
Chapter 5 Conclusion CIS 61.
Chapter 5 - Functions Outline 5.1 Introduction
Scope, Parameter Passing, Storage Specifiers
Advanced Programming Basics
Chapter 6 - Functions Outline 5.1 Introduction
C Storage classes.
Scope Rules and Storage Types
Chapter 7: User-Defined Functions II
Dynamic Memory.
1-6 Midterm Review.
Compiler vs linker The compiler translates one .c file into a .o file
The Three Attributes of an Identifier
More C++ Classes Systems Programming.
Scope Rules.
Presentation transcript:

The Three Attributes of an Identifier Identifiers have three essential attributes: storage duration scope linkage

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 memory is set aside for the variable and when that memory is released automatic allocated when the surrounding block is executed deallocated when the block terminates static stays in the same storage location as long as the program is running can retain its value indefinitely (until program terminates)

Automatic Storage Duration allocated when the surrounding block is executed deallocated when the block terminates ... void Sort(int list[], int Sz) { int startIdx = 0; } declared inside a block created (memory allocated) on each call initialized on each call deallocated (memory reclaimed) when call ends

Static Storage Duration stays in the same storage location as long as the program is running can retain its value indefinitely (until program terminates) int numCallsToSort = 0; ... void Sort(int list[], int Sz) { static int numSwaps = 0; } declared outside all blocks initialized once, keeps its value until program ends 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 identifier is recognized as a valid name block scope visible from its point of declaration to the end of the enclosing block place declaration within a block file scope visible from its point of declaration to the end of the enclosing file place declaration outside of all blocks (typically at beginning of file)

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

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

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 variable can be shared by different parts of the program external linkage may be shared by several (or all) files in the program internal linkage restricted to a single file, but shared by all functions within that file no linkage restricted to a single function

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

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

No Linkage no linkage restricted to a single function ... void Sort(int list[], int Sz) { static int numSwaps = 0; int startIdx = 0; } declared inside a block 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

Example: Declarations/Definitions From Section 6.7 of the C11 Standard: 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. A name may be declared many times in a single scope, but may only be defined once. // A.c . . . int externalA = 10; // definition: external linkage, // static storage, file scope static int staticA = 20; // definition: internal linkage, void A() { int localA = 0; // definition: no linkage, static int localstaticA = 0; // definition: no linkage, // static storage, block scope } // main.c . . . extern int externalA; // declaration, not definition int main() { }

Example: Full Source // A.c #include <stdio.h> int externalA = 10; // definition: external linkage, // static storage, file scope static int staticA = 20; // definition: internal linkage, void A() { printf(" A(): externalA = %d\n", externalA); printf(" A(): staticA = %d\n", staticA); int localA = 0; // definition: no linkage, // auto storage, block scope ++localA; printf(" A(): localA = %d\n", localA); static int localstaticA = 0; // definition: no linkage, // static storage, block scope ++localstaticA; printf(" A(): localstaticA = %d\n", localstaticA); }

Example: Full Source // main.c #include <stdio.h> #include "A.h" extern int externalA; // declaration, not definition int main() { printf("main(): making first call to A():\n"); A(); printf("main(): resetting externA:\n"); externalA = 5; printf("main(): making second call to A():\n"); printf("main(): making third call to A():\n"); return 0; } // A.h #ifndef A_H #define A_H void A(); #endif

Example: First Call printf("main(): making first call to A():\n"); . . . main(): making first call to A(): A(): externalA = 10 A(): staticA = 20 A(): localA = 1 A(): localstaticA = 1

Example: Access and Second Call . . . printf("main(): resetting externA:\n"); externalA = 5; printf("main(): making second call to A():\n"); A(); main(): making second call to A(): A(): externalA = 5 A(): staticA = 20 A(): localA = 1 A(): localstaticA = 2

Example: Third Call . . . printf("main(): making third call to A():\n"); A(); main(): making third call to A(): A(): externalA = 5 A(): staticA = 20 A(): localA = 1 A(): localstaticA = 3