Basic Semantics - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

Basic Semantics

Description:

Basic Semantics – PowerPoint PPT presentation

Number of Views:42
Avg rating:3.0/5.0
Slides: 26
Provided by: hong88
Category:
Tags: basic | free | semantics

less

Transcript and Presenter's Notes

Title: Basic Semantics


1
Chapter 5
  • Basic Semantics
  • Part 3

2
Object and extent
  • Object - an area of storage that is allocated in
    the environment as a result of the processing of
    a declaration.
  • e.g, variables, procedures (in Pascal)
  • BUT not constants or datatypes
  • Lifetime(extent) of an object is the duration of
    its allocation in the environment.
  • The lifetime of objects can extend beyond the
    region of a program where they may be accessed.
    (also an object can be accessible beyond its
    lifetime)

3
Pointers
  • Pointers require further extension of the
    structure of the environment. (a pointer is an
    object whose stored value is a reference to
    another object), e.g.,
  • (in Pascal) var x integer
  • (in C) int x
  • x does not point to an allocated object (and
    there is no way to know)

4
Manipulating pointers
  • Solution (Pascal)
  • x nil
  • if (x ltgt nil) x 2
  • Allocation
  • new(x)
  • x 2
  • dispose(x)
  • C
  • int x NULL
  • if (x ! NULL) x 2
  • Allocation
  • x (int) malloc(sizeof(int))
  • x 2
  • free(x)

5
Heap
  • Heap is an area in memory from which locations
    can be allocated (using new) and to which
    locations can be returned (using dispose)
  • allocation on the heap is usually referred to as
    dynamic allocations.
  • Allocation of local variables is referred to as
    stack-based or automatic allocation

6
Typical implementation of Environment
  • Three kinds of allocation in the environment or
    Storage Classes
  • Static (global variables)
  • automatic (local variables)
  • dynamic (pointers)

7
Specify a storage class
  • e.g., in C
  • int f(void)
  • static int x //x is allocated once
  • //and only once.
  • .....
  • Problem Write a function that returns a count of
    the number of times it is called.
  • Solution can easily be done in C using Static
    declaration (problem in Pascal)

8
Variables and Constants
  • Variable- an object where stored value can change
    during execution

9
Assignment
  • X Y (Pascal) x y (C)
  • A variable has
  • r-value - the value stored in the location
  • l-value - the location
  • Storage Semantic - Copy values
  • Pointer Semantic - Copy locations
  • Assignment by sharing
  • assignment by cloning

10
Storage and pointer semantics
11
assignment by cloning
12
Constants
  • Constants - A language entity that has a fixed
    value for the duration of the program. (no
    location, just the value).
  • Value semantics

13
Varieties of constants
  • Pascal constant- manifest constants, e.g.
    (C), const int I literal
  • Modula-2 relaxes the restriction, e.g.
    (C), const int I literal or expression
  • In Algol68 and Ada dynamic constants are legal,
    e.g. (Ada),
  • PI constant float 4.0 arctan(1.0)

14
Classification
  • Static constant - whose value can be computed
    prior to execution
  • compile-time constant (need not occupy memory)
  • load-time constant (need occupy memory)
  • dynamic constant - whose value is computed during
    execution

15
Aliases, Dangling References, Garbage
  • Alias- When the same object is bound to two
    different names at the same time.
  • Occurs in three ways
  • procedure call (pass by reference)
  • use of pointer variables
  • assignment by sharing
  • a problem with using pointers Side Effects

16
Aliasing example
  • typedef int Intptr
  • Intptr x, y
  • x (Intptr) malloc(sizeof(int))
  • x 1
  • y x / x and y now aliases /
  • y 2
  • printf(d, x)

17
Create or prevent aliasing
  • Prevent aliasing by explicit cloning any object
    (Java)
  • int y (int) x.clone()
  • explicit mechanism for aliasing in Fortran
  • EQUIVALENCE X, Y
  • COMMON statements

18
Dangling References
  • a location that has been deallocated from the
    environment, but can still be accessed by a
    program. (accessing an object beyond its
    lifetime)
  • Causes of dangling reference ...

19
Using pointers
  • typedef int Intptr
  • Intptr x, y
  • x (Intptr) malloc(sizeof(int))
  • x 2
  • y x / x and y now aliases /
  • free(x)
  • / y is now a dangling reference /
  • printf(d, y)

20
Block exit and function call
  • Block exit
  • int x
  • int y
  • y 2
  • x y
  • / x is now a
  • dangling
  • reference /
  • Function call
  • int dangle(void)
  • int x
  • return x

21
Garbage
  • Memory that has been allocated in the environment
    but that has become inaccessible to the program
  • It is safer to have garbage than dangling
    references
  • garbage - causes memory leak, but the program is
    internally correct
  • dangling reference - produce incorrect results,
    corrupt other programs, other runtime errors

22
Examples (1)
  • Example 1
  • int x
  • x (int) malloc(sizeof(int))
  • x NULL
  • Example 2
  • void p()
  • int x
  • x (int) malloc(sizeof(int))
  • x 2

23
Examples (2)
  • int x
  • x (int )
  • malloc(sizeof(int))
  • / x no longer accessible here
  • /

24
Garbage collection
  • Language systems that automatically reclaim
    garbage are said to perform garbage collection.
  • Stack-based management of memory in the
    environment of a block-structured languages is a
    simple garbage collection
  • Functional and OO language systems often rely on
    garbage collection (automatic allocating and
    deallocating)
  • e.g., Java, Lisp, SMALLTALK, Simula67, and Eiffel
  • C is an exception

25
The end of Chapter 5 Part 3
Write a Comment
User Comments (0)
About PowerShow.com