Similar documents
CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

In Java we have the keyword null, which is the value of an uninitialized reference type

CS558 Programming Languages

CS558 Programming Languages

CS558 Programming Languages

Heap, Variables, References, and Garbage. CS152. Chris Pollett. Oct. 13, 2008.

Memory and Addresses. Pointers in C. Memory is just a sequence of byte-sized storage devices.

Manual Allocation. CS 1622: Garbage Collection. Example 1. Memory Leaks. Example 3. Example 2 11/26/2012. Jonathan Misurda

Name, Scope, and Binding. Outline [1]

Advances in Programming Languages: Regions

Dynamic Allocation in C

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

Run-time Environments - 3

G Programming Languages - Fall 2012

Class Information ANNOUCEMENTS

CS61, Fall 2012 Section 2 Notes

Q1: /8 Q2: /30 Q3: /30 Q4: /32. Total: /100

CSE 307: Principles of Programming Languages

Limitations of the stack

Chapter 5. Names, Bindings, and Scopes

[0569] p 0318 garbage

Programming. Pointers, Multi-dimensional Arrays and Memory Management

Chapter 3:: Names, Scopes, and Bindings

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

Chapter 2 (Dynamic variable (i.e. pointer), Static variable)

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING

THE GOOD, BAD AND UGLY ABOUT POINTERS. Problem Solving with Computers-I

Last week. Data on the stack is allocated automatically when we do a function call, and removed when we return

Short Notes of CS201

CS 31: Intro to Systems Pointers and Memory. Martin Gagne Swarthmore College February 16, 2016

CS 61C: Great Ideas in Computer Architecture C Pointers. Instructors: Vladimir Stojanovic & Nicholas Weaver

CS201 - Introduction to Programming Glossary By

CPSC 213. Introduction to Computer Systems. Winter Session 2017, Term 2. Unit 1c Jan 24, 26, 29, 31, and Feb 2

CMSC 330: Organization of Programming Languages

Week 9 Part 1. Kyle Dewey. Tuesday, August 28, 12

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

CPSC 3740 Programming Languages University of Lethbridge. Data Types

o Code, executable, and process o Main memory vs. virtual memory

Programming Languages

Chapter 5 Names, Binding, Type Checking and Scopes

Pointers, Dynamic Data, and Reference Types

Lecture Notes on Memory Management

APS105. Malloc and 2D Arrays. Textbook Chapters 6.4, Datatype Size

A brief introduction to C programming for Java programmers

StackVsHeap SPL/2010 SPL/20

CMSC 330: Organization of Programming Languages. Memory Management and Garbage Collection

Pointers. Memory. void foo() { }//return

Compiler Construction

Quick review pointer basics (KR ch )

Names, Bindings, Scopes

Dynamic Data Structures (II)

Intermediate Programming, Spring 2017*

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology

CMSC 330: Organization of Programming Languages

CS61C : Machine Structures

Compiler Construction

CSCI 171 Chapter Outlines

Dynamic Data Structures. CSCI 112: Programming in C

CPSC 213. Introduction to Computer Systems. Instance Variables and Dynamic Allocation. Unit 1c

Pointers and Memory 1

Lecture Notes on Memory Management

Pointers and Terminal Control

Arrays Arrays and pointers Loops and performance Array comparison Strings. John Edgar 2

Memory Leak. C++: Memory Problems. Memory Leak. Memory Leak. Pointer Ownership. Memory Leak

CS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 11

Announcements. assign0 due tonight. Labs start this week. No late submissions. Very helpful for assign1

Dynamic memory allocation (malloc)

Names, Scope, and Bindings

Chapter 8 :: Composite Types

Types and Type Inference

Dynamic Memory. R. Inkulu (Dynamic Memory) 1 / 19

Memory Allocation III

Hacking in C. Memory layout. Radboud University, Nijmegen, The Netherlands. Spring 2018

6. Names, Scopes, and Bindings

CMSC 341 Lecture 2 Dynamic Memory and Pointers

Data Types (cont.) Subset. subtype in Ada. Powerset. set of in Pascal. implementations. CSE 3302 Programming Languages 10/1/2007

Dynamic Memory Allocation

Binding and Variables

POINTER AND ARRAY SUNU WIBIRAMA

Run Time Environments

CS558 Programming Languages. Winter 2013 Lecture 3

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

Run-time Environments - 2

Programming Language Implementation

CS 345. Garbage Collection. Vitaly Shmatikov. slide 1

C11: Garbage Collection and Constructors

Dynamic Allocation in C

Names, Scope, and Bindings

Dynamic Memory Management

Lecture 2: C Programm

Memory Management. CSC215 Lecture

Intro to C: Pointers and Arrays

Lecture07: Strings, Variable Scope, Memory Model 4/8/2013

CS 11 C track: lecture 5

Dynamic Memory Allocation II October 22, 2008

CA31-1K DIS. Pointers. TA: You Lu

Types II. Hwansoo Han

Programming Languages Third Edition. Chapter 7 Basic Semantics

Principles of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004

Compilers. 8. Run-time Support. Laszlo Böszörmenyi Compilers Run-time - 1

Transcription:

https://lambda.mines.edu

A pointer is a value that indicates location in memory. When we change the location the pointer points to, we say we assign the pointer a value. When we look at the data the pointer points to, we say we dereference the pointer.

In C or C++: p[0] is equivalent to *p p[n] is equivalent to *(p + n) Example in C or C++ main () { double nums[100]; double *end = nums + 100; for (double *p = nums; p < end; p++) *p = rand() / RAND_MAX;

main() { char *name = "Jack"; /* What does this print? */ printf("%c\n", 2[name]);

main() { char *name = "Jack"; /* What does this print? */ printf("%c\n", 2[name]); Array accessess are written BASE[OFFSET], which is equivalent to *(BASE + OFFSET). Notice that OFFSET[BASE] is equivalent to *(OFFSET + BASE), and since addition is associative, the operation is equivalent.

A reference is a special kind of pointer type References can be assigned to point to other data References can be dereferenced to get the corresponding data Pointer arithmetic is not available on references

The lifetime of an object is the time between object creation and destruction; the time during which an object is bound to a memory cell Common lifetimes: Static Stack Dynamic Explicit Heap Implicit Heap Don t confuse these static/dynamic and explicit/implicit words with typing systems, we re referring to lifetimes here.

Static variables are bound to a memory cell before execution (typically, this means that binding time is during link time) void f() { static int x = 0; printf("%d\n", x++); if (x < 5) f(); main() { f();

With your learning group: 1 What are the advantages of a static lifetime? 2 What are the disadvantages? Collectively come up with and write an example function (in any language, or even pseudocode) which could not be completed using just static lifetimes. Be prepared to share with the class.

Advantages: Direct addressing efficiency Useful for history-sensitive applications: caching, memoization Disadvantages: No recursion Bad for reliability

Lifetime: Created when declaration is encountered, destroyed when stack frame is cleared int f(int x) { int z = x + 1; /*... */ if (z < 5) f(z); Note Variables might be allocated at beginning of method, not necessarily when declaration is encountered.

With your learning group: 1 What are the advantages of a stack dynamic lifetime? Disadvantages? 2 How does a stack dynamic lifetime allow for recursion? 3 When might static lifetime be more memory efficient than stack dynamic? Visa-versa? 4 Does stack dynamic allow for dynamically sized arrays? Hint: man 3 alloca Be prepared to share with the class.

Advantages: Recursion possible (each call gets its own stack frame!) Conserves storage for short-lifetime variables Disadvantages: Allocating and deallocating must be done at run time Functions cannot be history sensitive (which might be a good restriction...) Indirect addressing

Allocated and deallocated at runtime by explicit directives Accessed through pointers or references to heap memory (usually stored on stack) main() { printf("how many numbers to input? "); int amount; scanf(" %d", &amount); int *nums = malloc(amount * sizeof(int)); /* do some things... */ free(nums);

With your learning group: 1 What are the advantages of an explicit heap dynamic lifetime? Disadvantages? 2 What might be some of the dangers of explicit heap dynamic lifetimes? Write a few examples with your group. Be prepared to share with the class.

A memory leak occurs when all references to a memory allocation have been lost but the memory has not been freed yet. This becomes especially noticable when the routine that allocates memory is frequently called.

/* excuse me caller, free this for me! */ char * f(int n) { char *A = malloc(n); /*... */ return A; main() { char *result; for (int i = 1; i < 10000000; i++) { result = f(i); /* do some work, but don't free! */

A programmer may accidentally access memory they already freed! A pointer which points to freed data is called a dangling pointer. main() { char *A = NULL; int amount; for (int i = 1; i <= 10; i++) { scanf(" %d", &amount); if (i == 1 amount > 0) A = malloc(amount + 1); A[amount] = '\0'; /*... */ free(a);

A programmer may accidentally free a pointer twice! main() { char *buf = malloc(10); printf("what is your favorite word? "); scanf("%s\n", buf); if (!strcmp("computers", buf)) { printf("me too!\n"); free(buf); free(buf);

In an implicit dynamic lifetime, allocation is caused automatically by instantiation. Deallocation can be done a number of ways: reference counting, garbage collection, or simply not allowing references. def f(x): return [0] * x print(f(10))

Garbage collection provides means to automatically destroy inaccessible objects in an implicit dynamic system with references. The garbage collector is run occasionally. To run the garbage collector: 1 Iterate over all visible variables (the roots) and find where they point in the heap. 2 For each of the corresponding entries in the heap, mark them as visited and visit all entries they reference. 3 Repeat until you cannot visit any more entries, the unvisited entries can be freed.