0% found this document useful (0 votes)
7 views

W2 Variables

Uploaded by

karaerdal123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

W2 Variables

Uploaded by

karaerdal123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Variables and Storage

• Introduction
• Names
• Variables
• The Concept of Binding
• Scope
• Scope and Lifetime
• Referencing Environments
• Named Constants

Copyright © 2015 Pearson. All rights reserved. 1-1


Introduction

• Imperative languages are abstractions of


von Neumann architecture
– Memory (stores instruction and data)
– Processor (provides operations over content of memory)

Copyright © 2015 Pearson. All rights reserved. 1-2


Variables

• A variable is an abstraction of a memory


cell
• Variables can be characterized as a
sextuple of attributes:
– Name
– Address
– Value
– Type
– Lifetime
– Scope

Copyright © 2015 Pearson. All rights reserved. 1-3


Variables Attributes

• Name – a string of characterds to identify some


entity in a program
– not all variables have them
– If too short, they cannot be meaningful
– Case Sensitivity: affects readability
– Special Words (Reserved Words) cannot be used
– May have special characters, eg:

PHP: all variable names must Perl: all variable names begin with
start with dollar sign special characters, specifying type

$var = 2; $var1 = 2; #scalar


@var2 = (1,2,3); #array
%var3 = ('a',1, 'b',2); #hash

Copyright © 2015 Pearson. All rights reserved. 1-4


Variables Attributes (continued)

• Address - the memory address with which it is


associated
– A variable may have different addresses at different times
during execution (eg: local variable of a subprogram)
– If two variable names can be used to access the same
memory location, they are called aliases
– Aliases are created via pointers, reference variables, C and
C++ union types
– Aliases are harmful to readability (program
readers must remember all of them)

Copyright © 2015 Pearson. All rights reserved. 1-5


Variables Attributes (continued)

• Type - determines the range of values of variables


and the set of operations that are defined for
values of that type; in the case of floating point,
type also determines the precision
• Value - the contents of the location with which the
variable is associated
- The l-value of a variable is its address
- The r-value of a variable is its value
• Abstract memory cell - the physical cell or
collection of cells associated with a variable

Copyright © 2015 Pearson. All rights reserved. 1-6


The Concept of Binding

A binding is an association between an


entity and an attribute, such as between a
variable and its type or value, or between
an operation and a symbol
• Binding time is the time at which a binding
takes place.

Copyright © 2015 Pearson. All rights reserved. 1-7


Possible Binding Times

• Language design time -- bind operator


symbols to operations
• Language implementation time-- bind
floating point type to a representation
• Compile time -- bind a variable to a type
in C or Java
• Load time -- bind a C or C++ static
variable to a memory cell)
• Runtime -- bind a nonstatic local variable
to a memory cell
Copyright © 2015 Pearson. All rights reserved. 1-8
Static and Dynamic Binding

• A binding is static if it first occurs before


run time and remains unchanged
throughout program execution.
• A binding is dynamic if it first occurs during
execution or can change during execution
of the program

Copyright © 2015 Pearson. All rights reserved. 1-9


Type Binding

• How is a type specified?


• When does the binding take place?
• If static, the type may be specified by either
an explicit or an implicit declaration

Copyright © 2015 Pearson. All rights reserved. 1-10


Explicit/Implicit Declaration
• An explicit declaration is a program
statement used for declaring the types of
variables
• An implicit declaration is a default
mechanism for specifying types of variables
through default conventions, rather than
declaration statements
• Basic, Perl, Ruby, JavaScript, and PHP
provide implicit declarations
– Advantage: writability (a minor convenience)
– Disadvantage: reliability (less trouble with Perl)

Copyright © 2015 Pearson. All rights reserved. 1-11


Explicit/Implicit Declaration (continued)

• Some languages use type inferencing to


determine types of variables (context)
– C# - a variable can be declared with var and an
initial value. The initial value sets the type

– Visual Basic 9.0+, ML, Haskell, and F# use type


inferencing. The context of the appearance of a
variable determines its type

Copyright © 2015 Pearson. All rights reserved. 1-12


Dynamic Type Binding
• Dynamic Type Binding (JavaScript, Python,
Ruby, PHP, and C# (limited))
• Specified through an assignment statement
e.g., JavaScript
list = [2, 4.33, 6, 8];
list = 17.3;
– Advantage: flexibility (generic program units)
– Disadvantages:
• High cost (dynamic type checking and
interpretation)
• Type error detection by the compiler is difficult

Copyright © 2015 Pearson. All rights reserved. 1-13


Variable Attributes (continued)

• Storage Bindings & Lifetime


– Allocation - getting a cell from some pool of
available cells
– Deallocation - putting a cell back into the pool
• The lifetime of a variable is the time during
which it is bound to a particular memory
cell

Copyright © 2015 Pearson. All rights reserved. 1-14


Categories of Variables by Lifetimes

• Static--bound to memory cells before


execution begins and remains bound to the
same memory cell throughout execution,
e.g., C and C++ static variables in
functions
– Advantages: efficiency (direct addressing),
history-sensitive subprogram support
– Disadvantage: lack of flexibility (no recursion)

Copyright © 2015 Pearson. All rights reserved. 1-15


Categories of Variables by Lifetimes
• Stack-dynamic--Storage bindings are created for
variables when their declaration statements are
elaborated.
(A declaration is elaborated when the executable
code associated with it is executed)
• If scalar, all attributes except address are statically
bound
– local variables in C subprograms (not declared static)
and Java methods
• Advantage: allows recursion; conserves storage
• Disadvantages:
– Overhead of allocation and deallocation
– Subprograms cannot be history sensitive
– Inefficient references (indirect addressing)
Copyright © 2015 Pearson. All rights reserved. 1-16
Categories of Variables by Lifetimes
• Explicit heap-dynamic -- Allocated and
deallocated by explicit directives, specified by the
programmer, which take effect during execution
• Referenced only through pointers or references,
e.g. dynamic objects in C++ (via new and delete),
all objects in Java
• Advantage: provides for dynamic storage
management
• Disadvantage: inefficient and unreliable

Copyright © 2015 Pearson. All rights reserved. 1-17


Categories of Variables by Lifetimes

• Implicit heap-dynamic--Allocation and


deallocation caused by assignment
statements
– all variables in APL; all strings and arrays in Perl,
JavaScript, and PHP
• Advantage: flexibility (generic code)
• Disadvantages:
– Inefficient, because all attributes are dynamic
– Loss of error detection

Copyright © 2015 Pearson. All rights reserved. 1-18

You might also like