Scope Rules and Storage Types

Slides:



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

Functions and scope Confidence In scope Reference: K&K textbook, Chapter 4.
Chapter 7: User-Defined Functions II
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
CIS 101: Computer Programming and Problem Solving Lecture 8 Usman Roshan Department of Computer Science NJIT.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 6 Functions.
Loose endsCS-2301, B-Term “Loose Ends” CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd.
"Loose ends"CS-2301 D-term “Loose Ends” CS-2301 System Programming C-term 2009 (Slides include materials from The C Programming Language, 2 nd edition,
More on FunctionsCS-2301 B-term More on Functions CS-2301, System Programming for Non-majors (Slides include materials from The C Programming Language,
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Scope Rules and Storage Types CS-2303, C-Term Scope Rules and Storage Types CS-2303, System Programming Concepts (Slides include materials from The.
Methods of variable creation Global variable –declared very early stage –available at all times from anywhere –created at the start of the program, and.
Miscellaneous topicsCS-2301 B-term Miscellaneous Topics CS-2301, System Programming for Non-majors (Slides include materials from The C Programming.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
Recursion and Function Implementation CS-2301 D-term Recursion and Implementation of Functions CS-2301 System Programming C-term 2009 (Slides include.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
CS-2303 System Programming Concepts
Recursion and Implementation of Functions
Storage & Linkage: Effects on Scope Rudra Dutta CSC Spring 2007, Section 001.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
Introduction to C++. Overview C++? What are references Object orientation Classes Access specifiers Constructor/destructor Interface-implementation separation.
1 Chapter 9 Scope, Lifetime, and More on Functions.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 2.
Basic Semantics Associating meaning with language entities.
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.
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.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
RecursionCIS 1057 Fall Recursion CIS 1057 Computer Programming in C Fall 2013 (Many slides based on/borrowed from Professor Hugh C. Lauer. Slides.
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.
Advanced Programming in C
The Three Attributes of an Identifier
Functions and Structured Programming
The Three Attributes of an Identifier
Programmazione I a.a. 2017/2018.
Programming Assignment #4 Binary Trees in C++
CS 240 – Lecture 5 Scope of Variables, The Stack, Automatic Variables, Global Variables, Constant Type.
Scope, Visibility, and Lifetime
Makefiles and Notes on Programming Assignment PA2
Strings and Streams Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Classes, Constructors, etc., in C++
Miscellaneous C++ Topics
Dynamic Memory Allocation (and Multi-Dimensional Arrays)
Structures, Unions, and Typedefs
Arrays and Pointers in C & C++
Linked Lists in C and C++
Programming Assignment #1 12-Month Calendar—
Templates (again) Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Symbolic Constants in C
Writing Functions.
Recursion and Implementation of Functions
Accessing Files in C Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Programming Assignment #5
Differences between Java and C
Your first C and C++ programs
Operator Overloading Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Functions in C and C++ CS-2303 System Programming Concepts Hugh C. Lauer (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
Digression on Loop Invariants
A Deeper Look at Classes
The Three Attributes of an Identifier
Scope Rules.
Introduction to Classes and Objects
Presentation transcript:

Scope Rules and Storage Types Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan and Ritchie, Absolute C++, by Walter Savitch, The C++ Programming Language, Special Edition, by Bjarne Stroustrup, and from C: How to Program, 5th and 6th editions, by Deitel and Deitel) CS-2303, A-Term 2012 Scope Rules and Storage Types

Scope Rules and Storage Types Data Storage in Memory Variables may be automatic or static Automatic variables are only declared within functions and compound statements (blocks) Storage allocated when function or block is entered Storage is released when function returns or block exits Parameters and result are (somewhat) like automatic variables Storage is allocated and initialized by caller of function Storage is released after function returns to caller. Definitions CS-2303, A-Term 2012 Scope Rules and Storage Types

Scope Rules and Storage Types Definitions Static data — data whose memory is allocated at compile time Actual memory location is set by Linker and Loader Values are retained across function calls Automatic data — data whose memory is allocated at run time on “The Stack” Each time function is called or block is entered Deleted when function or block exits! Values are not preserved from one call to next More about “The Stack” later CS-2303, A-Term 2012 Scope Rules and Storage Types

Scope of Automatic Variables Identifiers declared within a function or compound statement are visible only from the point of declaration to the end of that function or compound statement. Like Java CS-2303, A-Term 2012 Scope Rules and Storage Types

Scope Rules and Storage Types Example int fcn (float a, int b) { int i; double g; for (i = 0; i < b; i++) { double h = i*g; loop body – may access a, b, i, g, h } // for(i…) fcn body – may access a, b, i, g } // int fcn( … ) i is visible from this point to end of fcn g is visible from this point to end of fcn h is only visible from this point to end of loop! None of these are visible outside the function CS-2303, A-Term 2012 Scope Rules and Storage Types

Scope Rules and Storage Types Idiosyncrasies In traditional C (& Visual Studio) All variables must be declared at beginning of function or compound statement (i.e., before first statement); visible from that point on In gcc Variables may be declared anywhere in function or compound statement; visible from that point on In C99 & C++ Loop variables may be declared in for statement; visible only within loop body, but not beyond CS-2303, A-Term 2012 Scope Rules and Storage Types

Static Data – Very different Variables may be declared within or outside of functions Storage allocated when program is initialized Storage is released when program exits Variables outside of functions are usually visible to linker Unless you specify otherwise Compiler sets aside storage for all static variables at compiler or link time Values retained across function calls Initializations must evaluate to compile-time constants CS-2303, A-Term 2012 Scope Rules and Storage Types

Confusion in terminology Reserved word static in C & C++ programs does two things:– It makes the data static According to previous definition Even if it already was static It hides it from the linker! Variables declared outside of any function are called global They are static variables without the word “static”! CS-2303, A-Term 2012 Scope Rules and Storage Types

Static Variable Examples int j; //global, visible to linker & all functions in this program static float f; // not visible to linker, visible to // to all functions in this program int fcn (float a, int b) { // nothing inside of {} is visible to linker int i = b; //automatic double g; //automatic static double h; //static, not visible to // linker; value retained from call to call body – may access j, f, a, b, i, g, h } // int fcn( … ) CS-2303, A-Term 2012 Scope Rules and Storage Types

Static Variable Examples int j; //global, visible to linker & all functions in this program static float f; // not visible to linker, visible to // to all functions in this program int fcn (float a, int b) { // nothing inside of {} is visible to linker int i = b; //automatic double g; //automatic static double h; //static, not visible to // linker; value retained from call to call body – may access j, f, a, b, i, g, h } // int fcn( … ) Note: value of h is retained from one call to next Value of h is also retained across recursions CS-2303, A-Term 2012 Scope Rules and Storage Types

Scope Rules and Storage Types Extern Variables int j; //static, visible to linker & all functs static float f; // not visible to linker, visible to // to all functions in this program extern float p; // global, defined in another program and visible to linker int fcn (float a, int b) { // nothing inside of {} is visible to linker int i = b; //automatic double g; //automatic static double h; //static, not visible to // linker; value retained from call to call body – may access j, f, a, b, i, g, h , p } // int fcn( … ) extern :– connected by linker to global of another program CS-2303, A-Term 2012 Scope Rules and Storage Types

Extern Variables (continued) Examples:– stdin, stdout, stderr are extern variables that point to standard input, output, and error streams. extern variables:– Frequently occur in .h files. Each must be actually declared outside any function in exactly one .c file CS-2303, A-Term 2012 Scope Rules and Storage Types

Scope Rules and Storage Types Questions? Next Topic CS-2303, A-Term 2012 Scope Rules and Storage Types