csci 3136 principles of programming languages
play

CSCI 3136 Principles of Programming Languages Names, Scopes, and - PowerPoint PPT Presentation

CSCI 3136 Principles of Programming Languages Names, Scopes, and Bindings - 1 Summer 2013 Faculty of Computer Science Dalhousie University 1 / 87 Name A name is a mnemonic character string representing something else: 2 / 87 Name A name is


  1. CSCI 3136 Principles of Programming Languages Names, Scopes, and Bindings - 1 Summer 2013 Faculty of Computer Science Dalhousie University 1 / 87

  2. Name A name is a mnemonic character string representing something else: 2 / 87

  3. Name A name is a mnemonic character string representing something else: For example: • x,sin,f,prog1,null? are names 3 / 87

  4. Name A name is a mnemonic character string representing something else: For example: • x,sin,f,prog1,null? are names • 1,2,3, ‘‘test’’ are not names 4 / 87

  5. Name A name is a mnemonic character string representing something else: For example: • x,sin,f,prog1,null? are names • 1,2,3, ‘‘test’’ are not names • +, <=, . . . may be names, if they are not built-in operators 5 / 87

  6. Binding A binding is an association between two entities: 6 / 87

  7. Binding A binding is an association between two entities: For example: • Name and a memory location (for variables) 7 / 87

  8. Binding A binding is an association between two entities: For example: • Name and a memory location (for variables) • Name and a function 8 / 87

  9. Binding A binding is an association between two entities: For example: • Name and a memory location (for variables) • Name and a function Typically a binding is between a name and the object it refers to. 9 / 87

  10. Binding A binding is an association between two entities: For example: • Name and a memory location (for variables) • Name and a function Typically a binding is between a name and the object it refers to. A referencing environment is a complete set of bindings active at a certain point in a program. 10 / 87

  11. Scope 11 / 87

  12. Scope • Scope of a binding is the region of a program, or time interval(s) in the program’s execution during which the binding is active. 12 / 87

  13. Scope • Scope of a binding is the region of a program, or time interval(s) in the program’s execution during which the binding is active. • Scope is a maximal region of the program where no bindings are destroyed (e.g., body of a procedure). 13 / 87

  14. Binding Times Compile time • Mapping of high-level language constructs to machine code • Layout of static data in memory 14 / 87

  15. Binding Times Compile time • Mapping of high-level language constructs to machine code • Layout of static data in memory Link time • Resolve references between separately compiled modules 15 / 87

  16. Binding Times Compile time • Mapping of high-level language constructs to machine code • Layout of static data in memory Link time • Resolve references between separately compiled modules Load time • Assign machine addresses to static data 16 / 87

  17. Binding Times Compile time • Mapping of high-level language constructs to machine code • Layout of static data in memory Link time • Resolve references between separately compiled modules Load time • Assign machine addresses to static data Run time • Binding of values to variables • Allocate local variables of procedures on the stack • Allocate dynamic data and assign to variables 17 / 87

  18. Importance of Binding Time Early binding • Faster code • Typical in compiled languages 18 / 87

  19. Importance of Binding Time Early binding • Faster code • Typical in compiled languages Late binding • Greater flexibility • Typical in interpreted languages 19 / 87

  20. Object and Binding Lifetime Object lifetime • Period between the creation and destruction of an object • Example: − time between creation and destruction of a dynamically allocated variable in C++ using new and delete − pushing and popping a stack frame 20 / 87

  21. Object and Binding Lifetime Object lifetime • Period between the creation and destruction of an object • Example: − time between creation and destruction of a dynamically allocated variable in C++ using new and delete − pushing and popping a stack frame Binding lifetime • Period between the creation and destruction of the binding (name-to-object association) 21 / 87

  22. Object and Binding Lifetime Object lifetime • Period between the creation and destruction of an object • Example: − time between creation and destruction of a dynamically allocated variable in C++ using new and delete − pushing and popping a stack frame Binding lifetime • Period between the creation and destruction of the binding (name-to-object association) Two common mistakes • Dangling reference: no object for a binding (e.g., a pointer refers to an object that has already been deleted ) • Memory leak: no binding for an object (preventing the object from being deallocated) 22 / 87

  23. Storage Allocation An object’s lifetime corresponds to the mechanism used to manage the space where the object resides. 23 / 87

  24. Storage Allocation An object’s lifetime corresponds to the mechanism used to manage the space where the object resides. Static object • Object stored at a fixed absolute address • Object’s lifetime spans the whole execution of the program 24 / 87

  25. Storage Allocation An object’s lifetime corresponds to the mechanism used to manage the space where the object resides. Static object • Object stored at a fixed absolute address • Object’s lifetime spans the whole execution of the program Object on stack • Object allocated on stack in connection with a subroutine call • Object’s lifetime spans period between invocation of the subroutine and return from the subroutine 25 / 87

  26. Storage Allocation An object’s lifetime corresponds to the mechanism used to manage the space where the object resides. Static object • Object stored at a fixed absolute address • Object’s lifetime spans the whole execution of the program Object on stack • Object allocated on stack in connection with a subroutine call • Object’s lifetime spans period between invocation of the subroutine and return from the subroutine Object on heap • Object stored on heap • Object created/destroyed at arbitrary times − Explicitly by programmer or − Implicitly by garbage collector 26 / 87

  27. Example: Object Creation and Destruction in C++ 27 / 87

  28. Example: Object Creation and Destruction in C++ • Local objects are local to functions and blocks and exist while the execution is inside the function or block. 28 / 87

  29. Example: Object Creation and Destruction in C++ • Local objects are local to functions and blocks and exist while the execution is inside the function or block. • Heap objects are allocated/deallocated using new/delete . (free-store objects) 29 / 87

  30. Example: Object Creation and Destruction in C++ • Local objects are local to functions and blocks and exist while the execution is inside the function or block. • Heap objects are allocated/deallocated using new/delete . (free-store objects) • Non-static member objects of a parent object exist while the parent object exists. 30 / 87

  31. Example: Object Creation and Destruction in C++ • Local objects are local to functions and blocks and exist while the execution is inside the function or block. • Heap objects are allocated/deallocated using new/delete . (free-store objects) • Non-static member objects of a parent object exist while the parent object exists. • Array elements exist while the array exists. 31 / 87

  32. Example: Object Creation and Destruction in C++ • Local objects are local to functions and blocks and exist while the execution is inside the function or block. • Heap objects are allocated/deallocated using new/delete . (free-store objects) • Non-static member objects of a parent object exist while the parent object exists. • Array elements exist while the array exists. • Local static objects of functions/blocks exist after the first invocation of the function until termination. 32 / 87

  33. Example: Object Creation and Destruction in C++ • Local objects are local to functions and blocks and exist while the execution is inside the function or block. • Heap objects are allocated/deallocated using new/delete . (free-store objects) • Non-static member objects of a parent object exist while the parent object exists. • Array elements exist while the array exists. • Local static objects of functions/blocks exist after the first invocation of the function until termination. • Global, namespace, class static objects exist while the program runs. 33 / 87

  34. Example: Object Creation and Destruction in C++ • Local objects are local to functions and blocks and exist while the execution is inside the function or block. • Heap objects are allocated/deallocated using new/delete . (free-store objects) • Non-static member objects of a parent object exist while the parent object exists. • Array elements exist while the array exists. • Local static objects of functions/blocks exist after the first invocation of the function until termination. • Global, namespace, class static objects exist while the program runs. • Temporary objects in expressions exist during the evaluation of the expression. 34 / 87

  35. Static Objects 35 / 87

  36. Static Objects • Global variables 36 / 87

  37. Static Objects • Global variables • Variables local to subroutines, but retain their value between invocations 37 / 87

  38. Static Objects • Global variables • Variables local to subroutines, but retain their value between invocations • Constant literals 38 / 87

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend