Various languages….  Could affect performance  Could affect reliability  Could affect language choice.

Slides:



Advertisements
Similar presentations
Chapter 6 Data Types
Advertisements

Names and Bindings.
Chapter 5 Names, Bindings, and Scopes
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
CS 355 – Programming Languages
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
Lifetime “The lifetime of a variable is the time during which the variable is bound to a specific memory location.” [p. 219] “…the lifetime of a variable.
CS 330 Programming Languages 10 / 18 / 2007 Instructor: Michael Eckmann.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes Names Variables The Concept of Binding Type Checking Strong Typing Type Compatibility.
The Concept of Variables
Copyright © 1995 by Addison-Wesley Publishing Co. 1 Names - Design issues: - Maximum length? - Are connector characters allowed? - Are names case sensitive?
CS 330 Programming Languages 10 / 24 / 2006 Instructor: Michael Eckmann.
Names and Bindings Introduction Names Variables The concept of binding Chapter 5-a.
Names, Bindings, and Scopes
Software II: Principles of Programming Languages Lecture 5 – Names, Bindings, and Scopes.
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
CS 330 Programming Languages 10 / 21 / 2008 Instructor: Michael Eckmann.
ISBN 0-321— Chapter 6 sections 1-4, 9 Primitive Data Types Numbers Strings Ordinal Types Pointers.
Names and Binding In procedural programming, you write instructions the manipulate the “state” of the process where the “state” is the collection of variables.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
COMP4730/2003/lec5/H.Melikian Names, Bindings,Type Checking and Scopes (Chapter 5) - Design issues: - Maximum length? - Are connector characters allowed?
1 CS Programming Languages Class 07 September 14, 2000.
5-1 Chapter 5: Names, Bindings, Type Checking, and Scopes Variables The Concept of Binding Type Checking Strong Typing Type Compatibility Scope and Lifetime.
March 12, ICE 1341 – Programming Languages (Lecture #6) In-Young Ko Programming Languages (ICE 1341) Lecture #6 Programming Languages (ICE 1341)
Subprograms Support process abstraction and modularity –characteristics of subprograms are that they have a single entry point the calling entity is suspended.
Chapter 5 Names, Bindings, and Scopes. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 5 Topics Introduction Names Variables The Concept.
ISBN Chapter 5 Names, Bindings, and Scopes.
Storage Bindings Allocation is the process by which the memory cell or collection of memory cells is assigned to a variable. These cells are taken from.
Introduction A variable can be characterized by a collection of properties, or attributes, the most important of which is type, a fundamental concept in.
Basic Semantics Associating meaning with language entities.
ISBN Chapter 5 Names, Bindings, and Scopes.
CSC3315 (Spring 2008)1 CSC 3315 Subprograms Hamid Harroud School of Science and Engineering, Akhawayn University
1 Records Record aggregate of data elements –Possibly heterogeneous –Elements/slots are identified by names –Elements in same fixed order in all records.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
CPSC 252 Dynamic Memory Allocation Page 1 Dynamic memory allocation Our first IntVector class has some serious limitations the capacity is fixed at MAX_SIZE.
CS 330 Programming Languages 10 / 30 / 2007 Instructor: Michael Eckmann.
1 CS Programming Languages Class 09 September 21, 2000.
ISBN Chapter 6 Data Types Pointer Types Reference Types Memory Management.
Names, Bindings, and Scope Session 3 Course : T Programming Language Concept Year : February 2011.
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
Names and Binding In Text: Chapter 4.
ISBN Variables, Names, Scope and Lifetime ICOM 4036 Lecture 9.
1 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Parser Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator.
CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
Names, Bindings, Type Checking and Scopes. Chapter 5 Topics Introduction Names Variables The Concept of Binding Type Checking Strong Typing Type Equivalence.
CHAPTER 4 VARIABLES & BINDING SUNG-DONG KIM DEPT. OF COMPUTER ENGINEERING, HANSUNG UNIVERSITY.
Data Types Chapter 6: Data Types Lectures # 13. Topics Chapter 6: Data Types 2 Introduction Primitive Data Types Character String Types Array Types Associative.
Chapter 5 Names, Bindings, Type Checking CSCE 343.
5.2 Names - We discuss all user-defined names here
5.2 Names - We discuss all user-defined names here
Object Lifetime and Pointers
Data Types In Text: Chapter 6.
Chapter 6 – Data Types CSCE 343.
Chapter 5 Names, Bindings, Type Checking, and Scopes.
Names, Bindings, and Scopes
Concepts of programming languages
Chapter 5 Names, Bindings, Type Checking, and Scopes.
Names, Bindings, Type Checking, and Scopes
Dynamic Memory.
Names, Bindings, and Scopes
Presentation transcript:

various languages…

 Could affect performance  Could affect reliability  Could affect language choice

 The lifetime of a variable is the time during which it is bound to a particular memory cell  Ruby built-in objects created when values assigned (e.g., x=5)  Other classes created with new  factory methods also create objects  Ruby uses garbage collection to destroy objects that are no longer reachable

Static  bound to memory cells before execution begins  remains bound to the same memory cell throughout execution  all FORTRAN 77 variables, C static variables (not C++ class variables)  Advantages: efficiency (direct addressing) history-sensitive subprogram support  Disadvantage: lack of flexibility (no recursion) storage can't be shared among subprograms void myFn() { static int count=0; count++; cout << count; } myFn(); Quick Ex: Trace! Discuss bullets

Assuming C/C++  DATA segment subdivided into parts when loaded into memory From: p temp temp2 high address low address command-line args & environment variables stack heap uninitialized data (BSS) initialized by exec (block started by symbol) initialized data text read from program file by exec

Stack-dynamic  Created when execution reaches code  Allocated from the run-time stack  Variables may be allocated at the beginning of a method, even though declarations may be spread throughout  Advantages: allows recursion conserves storage  Disadvantages: Overhead of allocation and deallocation (not too bad, since all memory allocated/ deallocated together) Subprograms cannot be history sensitive Inefficient references (indirect addressing) void myFn2(int parm) { int temp; … int temp2; } parm temp temp2 sp local How? Compared to what?

Explicit heap-dynamic  Allocated (and deallocated) by explicit directives during runtime  new/delete, malloc/free etc.  Accessed only through pointers or references  Dynamic objects in C++, all objects in Java  Advantage: provides for dynamic storage management  Disadvantages: inefficient and unreliable C# methods that define a pointer must include reserved word unsafe void myFn3() { int* nums = new int[5]; … } public void myFn4() { Point point = new Point(); }

Implicit heap-dynamic  Allocation and deallocation caused by assignment statements  No new/delete… these are implied!  all variables in APL; all strings and arrays in Perl and JavaScript  Advantage: flexibility  Disadvantages: Inefficient because all attributes are dynamic loss of error detection list = [2, 4.33, 6, 8]; Which lifetimes are used in Ruby?

 A pointer type variable has a range of values that consists of memory addresses and a special value, nil or NULL  Provide a way to manage dynamic memory  A pointer can be used to access a location in the area where storage is dynamically created (usually called a heap) In C++, is it necessary for all pointers to access heap?

 Two fundamental operations: assignment and dereferencing  Assignment is used to set a pointer variable’s value to some useful address  Dereferencing yields the value stored at the location represented by the pointer’s value Dereferencing can be explicit or implicit C++ uses an explicit operation via * j = *ptr; sets j to the value located at ptr C++ also does implicit dereferencing void myFun(int& x) { x = 5; } What about Java?

ptr = new int; // assignment *ptr = 206; // dereferencing j = *ptr; // dereferencing Provide the power of indirect addressing (access variable via address stored in another variable, may not be dynamic)

float stuff[100]; float *p; p = &stuff; int i=3; p is an alias for stuff *(p+5) is equivalent to stuff[5] and p[5] *(p+i) is equivalent to stuff[i] and p[i] stuff 0x100 p 0x

 Domain type need not be fixed ( void * )  void * can point to any type. Use type casts.  void * cannot be de-referenced  void * often used in C to pass as arguments.  In C++, generally better to use templates so compiler can do appropriate type checking.

 Do you remember the difference between a dangling pointer and a memory leak?

 Dangling pointers (dangerous) A pointer points to a heap-dynamic variable that has been de-allocated may have been reallocated values no longer meaningful writing to it could cause storage manager to fail.  Example Point p = new Point(3,4); delete p; // dangling – p still has address! cout << p.getX(); // bad!!

 Memory Leak(dangerous) Memory has not been deleted (returned to heap manager) No variables contain the address (so not accessible) When is this a problem?  Example int[] p = new int[5000]; p = new int[10000];

 C++ includes a special kind of pointer type called a reference type that is used primarily for formal parameters Constant pointer that is always implicitly dereferenced void myFun(int &y) { y = y + 1; } What does this mean, “constant pointer”?

 Java extends C++’s reference variables and allows them to replace pointers entirely References refer to object instances – not necessarily constant Implicitly derefenced (i.e., no * required) No pointer arithmetic Java does NOT have pointers! But references as implemented in Java have many similarities.  C# includes both the references of Java and the pointers of C++.

 Does Ruby have references or pointers?  Ruby has garbage collection. What problem does garbage collection solve?