Presentation is loading. Please wait.

Presentation is loading. Please wait.

Concepts of programming languages

Similar presentations


Presentation on theme: "Concepts of programming languages"— Presentation transcript:

1 Concepts of programming languages
Chapter 6 Data types Lec. 13 Lecturer: Dr. Emad Nabil

2 Array Initialization Some language allow initialization at the time of storage allocation C, C++, Java, C# example int list [] = {4, 5, 7, 83} Character strings in C and C++ char name [] = ″freddie″; Arrays of strings in C and C++ char *names [] = {″Bob″, ″Jake″, ″Joe″]; Java initialization of String objects String[] names = {″Bob″, ″Jake″, ″Joe″};

3 Heterogeneous Arrays A heterogeneous array is one in which the elements need not be of the same type Supported by Perl, Python, JavaScript, and Ruby numList = [2000, 2003, 2005, 2006] stringList = ["Essential", "Python", "Code"] mixedList = [1, 2, "three", 4] subList = ["A", "B", ["C", 2006]] listList = [numList, stringList, mixedList, subList] for x in listList: for y in x: if isinstance(y, int): print (y + 1) if isinstance(y, str): print ("String:" + y) Python code

4 Array Initialization C-based languages Ada Python
int list [] = {1, 3, 5, 7} char *names [] = {″Mike″, ″Fred″, ″Mary Lou″}; Ada List : array (1..5) of Integer := (1 => 17, 3 => 34, others => 0); Python list = [x ** 2 for x in range(12) if x % 3 == 0] print (list) Output  [0, 9, 36, 81]

5 Pointer and Reference Types

6 Pointer and Reference Types
A pointer type variable has a range of values that consists of memory addresses and a special value, nil Provide the power of indirect addressing 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)

7 Design Issues of Pointers
What are the scope of and lifetime of a pointer variable? What is the lifetime of a heap-dynamic variable? Should the language support pointer types, reference types, or both?

8 Pointer Operations Two fundamental operations: assignment and dereferencing Assignment is used to set a pointer variable’s value to some useful address Dereferencing gets the value stored at the location represented by the pointer’s value Dereferencing can be explicit or implicit C++ uses an explicit operation via *

9 Pointer Assignment Illustrated
Explicit dereferencing Int * ptr= new int(206); Int j = *ptr The assignment operation j = *ptr

10 Pointer to record Struct student { }; Student * s=new student();
String name; Int age; }; Student * s=new student(); (*s).name=“”; S->name=“”; Explicit dereferencing Explicit dereferencing

11 Try it: http://www.tutorialspoint.com/compile_ada_online.php
with ada.text_io; use ada.text_io; with ada.integer_text_io; use ada.integer_text_io; procedure hello is type Box is record l, w: Integer := 0; end record; type BoxPtr is access Box; b: Box; bPrt: BoxPtr; begin b := (11, 22); bPrt := new Box; bPrt.l := 33; bPrt.w := 44; Put(b.l); Put(bPrt.l); end hello; Pointer to Box Implicit dereferencing Implicit dereferencing Try it:

12 Problems with Pointers
Dangling pointers (dangerous) A pointer points to a heap-dynamic variable that has been deallocated int * arrayPtr1; int * arrayPtr2 = new int[100]; arrayPtr1 = arrayPtr2; delete [] arrayPtr2; // Now, arrayPtr1 is dangling, because the heap storage // to which it was pointing has been deallocated. Dangle استرخى – تدلى

13 Problems with Pointers
Lost heap-dynamic variable An allocated heap-dynamic variable that is no longer accessible to the user program (often called garbage) Pointer p1 is set to point to a newly created heap-dynamic variable Pointer p1 is later set to point to another newly created heap-dynamic variable The process of losing heap-dynamic variables is called memory leakage (تسرب) int * Ptr1 = new int(10); Ptr1 = new int (20); The first heap-dynamic variable is now inaccessible, or lost. memory leakage illustration

14 Pointers in Ada lost heap-dynamic dangling-pointer
The dangling-pointer problem: A heap-dynamic variable may be (at the implementor’s option) implicitly deallocated at the end of the scope of its pointer type; (garbage Collection available on some implementations of Ada) The lost heap-dynamic variable problem is not eliminated by Ada. dangling-pointer lost heap-dynamic

15 Pointers in C and C++ Extremely flexible but must be used with care
Pointers can point at any variable regardless of when or where it was allocated Used for dynamic storage management and addressing Pointer arithmetic is possible Explicit dereferencing and address-of operators Domain type need not be fixed (void *) void * can point to any type

16 Pointers in C and C++ Pointer to int int *ptr; int count, init; ptr = &init; count = *ptr; Get address Explicit dereferencing

17 Pointer Arithmetic in C and C++
float stuff[100]; float *p; p = stuff; *(p+5) is equivalent to stuff[5] and p[5] *(p+i) is equivalent to stuff[i] and p[i]

18 Reference Types C++ includes a special kind of pointer type called a reference type that is used primarily for formal parameters Advantages of both pass-by-reference and pass-by-value int result = 0; int &ref_result = result; . . . ref_result = 100; Cout<<result; //100 result and ref_result are aliases.

19 Implicit dereferencing is occurred, to increase readability
Void f(int & x) { X++; } Int main() Int a=5; F(a); Cout<<a; Compiler send address of a, not value of a

20 Reference Types Java extends C++’s reference variables and allows them to replace pointers entirely Java reference variables can be assigned to refer to different class instances; they are not constants. All Java class instances are referenced by reference variables.

21 Reference Types C# includes both the references of Java and the pointers of C++via unsafe code C# has automatic garbage collection for reference variables only, not for objects pointed to by pointers. static unsafe void Main(string[] args) { int[] arr = { 1, 2 }; fixed (int* x = arr) x[0] += 1; *(x+1) += 1; Console.WriteLine(*x); //2 Console.WriteLine(*(x+1)); //3 } Console.WriteLine(arr[0]); //2 Console.WriteLine(arr[1]); //3

22 Reference Types All variables in the object-oriented languages Smalltalk, Python, Ruby and Lua are references. They are always implicitly dereferenced. Furthermore, the direct values of these variables cannot be accessed.

23 Evaluation of Pointers
Dangling pointers and dangling objects are problems as is heap management Pointers are like goto's--they widen the range of cells that can be accessed by a variable Pointers or references are necessary for dynamic data structures--so we can't design a language without them

24 Representations of Pointers
Large computers use single values In early Intel microprocessors use segment and offset (16 bits for each)

25 Solutions to the Dangling-Pointer Problem
Tombstone: extra heap cell that is a pointer to the heap-dynamic variable The actual pointer variable points only at tombstones When heap-dynamic variable de-allocated, tombstone remains but set to nil Costly in time and space dangling-pointer This approach prevents a pointer from ever pointing to a deallocated variable

26 Int * pointer1 = new int(5); Int * pointer2 = pointer1;
111222 112233 445566 112233 445566 5 pointer1 Tombstone Dynamic heap variable 111333 112233 pointer2 Delete pointer1; 111222 112233 445566 112233 NULL 5 pointer Tombstone Dynamic heap variable 111333 Costly in time and space 112233 access to tombstone that equals to null result in run-time error pointer2

27 Solutions to the Dangling-Pointer Problem
An alternative to tombstones is the locks-and-keys approach used in the implementation of UW-Pascal (Fischer and LeBlanc, 1977, 1980). Locks-and-keys: Pointer values are represented as (key, address) pairs Heap-dynamic variables are represented as variable plus cell for integer lock value When heap-dynamic variable allocated, lock value is created and placed in lock cell and key cell of pointer

28 Dangling Pointer solution using Locks-and-keys:
Int * x = new int (10); Int *y=x; delete y; Key address Lock allocated memory x12ff66 x x12ff66 y delete y; x12ff66 x If they match, the access is legal; otherwise the access is treated as a run-time error. This approach is save as it doesn’t access other program data, as the heap dynamic variable may be assigned to another program. x12ff66 y

29 The best solution to the Dangling Pointer
Take deallocation of heap-dynamic variables out of the hands of programmers. No explicit deallocation of heap-dynamic variables, son there will be no dangling pointers. the run-time system must implicitly deallocate heap-dynamic variables when they are no longer useful. Python, LISP, Java and C# systems have always done this. Recall that C#’s pointers do not include implicit deallocation.


Download ppt "Concepts of programming languages"

Similar presentations


Ads by Google