Chapter 6 Data Types. 2 3 4 5 6 7 8 9 10.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Dynamic memory allocation
Data Structures Using C++ 2E
Introduction to Memory Management. 2 General Structure of Run-Time Memory.
CSC 270 – Survey of Programming Languages C Lecture 6 – Pointers and Dynamic Arrays Modified from Dr. Siegfried.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Pointer Variables The normal variables hold values. For example, int j; j = 2; Then a reference to j in an expression will be identified with the value.
A pointer is the memory address of a variable. A memory address is a physical location within a system’s memory space. A pointer variable is variable used.
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
Pointers & Dynamic Memory Allocation Mugurel Ionu Andreica Spring 2012.
CS-1030 Dr. Mark L. Hornick 1 Pointers And Dynamic Memory.
Various languages….  Could affect performance  Could affect reliability  Could affect language choice.
1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the design and concepts behind pointer arithmetic ❏ To write.
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
Informática II Prof. Dr. Gustavo Patiño MJ
CPSC 388 – Compiler Design and Construction
1 Day 03 Introduction to C. 2 Memory layout and addresses r s int x = 5, y = 10; float f = 12.5, g = 9.8; char c = ‘r’, d = ‘s’;
Introduction to Data Structure, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure Memory Allocation Ming Li Department.
Pointers. Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Reference Counters Associate a counter with each heap item Whenever a heap item is created, such as by a new or malloc instruction, initialize the counter.
CS Data Structures Chapter 4 Lists.
Pointers Applications
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location. This is.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
Storage Bindings Allocation is the process by which the memory cell or collection of memory cells is assigned to a variable. These cells are taken from.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
1 Pointers Arrays have a disadvantage: Their size must be known at compile time. We would like the capability to allocate an array-like object of any needed.
1 Records Record aggregate of data elements –Possibly heterogeneous –Elements/slots are identified by names –Elements in same fixed order in all records.
1 Data Structures CSCI 132, Spring 2014 Lecture 10 Dynamic Memory and Pointers.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Chapter 9 Pointers and Dynamic Arrays (9.1). Pointers A variables which holds the memory address for a variable of a specific type. Call-by-Reference.
CPSC 252 Dynamic Memory Allocation Page 1 Dynamic memory allocation Our first IntVector class has some serious limitations the capacity is fixed at MAX_SIZE.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
Pointers in C++. 7a-2 Pointers "pointer" is a basic type like int or double value of a pointer variable contains the location, or address in memory, of.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
Pointers and Dynamic Memory Allocation. Declaring a pointer.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Chapter 13: Structures. In this chapter you will learn about: – Single structures – Arrays of structures – Structures as function arguments – Linked lists.
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
1 CS Programming Languages Class 09 September 21, 2000.
ISBN Chapter 6 Data Types Pointer Types Reference Types Memory Management.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
CSI 3125, Preliminaries, page 1 Class. CSI 3125, Preliminaries, page 2 Class The most important thing to understand about a class is that it defines a.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
System Programming Practical Session 7 C++ Memory Handling.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
P OINTERS A pointer is an address All data is stored in memory in some location that is indexed with an address Can refer to variables by name or by memory.
Data Structures in C++ Pointers & Dynamic Arrays Shinta P.
CSC Pointers Powerful feature of the C++ language One of the most difficult to master Essential for construction of interesting data structures.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Pointers Pointers are variables that contain memory addresses as their values. A variable directly contains a specific value. A pointer contains an address.
Data Types Chapter 6: Data Types Lectures # 13. Topics Chapter 6: Data Types 2 Introduction Primitive Data Types Character String Types Array Types Associative.
Recap Resizing the Vector Push_back function Parameters passing Mechanism Primitive Arrays of Constants Multidimensional Arrays The Standard Library string.
Object Lifetime and Pointers
Data Types In Text: Chapter 6.
Pointers, Polymorphism, and Memory Allocation
Records Design Issues: 1. What is the form of references?
Concepts of programming languages
Day 03 Introduction to C.
Chapter 10: Pointers Starting Out with C++ Early Objects Ninth Edition
Pointers and References
Dynamic Memory Allocation
Dynamic Memory.
Pointers and References
Presentation transcript:

Chapter 6 Data Types

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29 Pointer Type A pointer has a memory address as it value (r- value), and also has a special value, nil (NULL in C) Pointer operations: Assignment: Sets a pointer variable’s value to some useful address. (&) Dereferncing: Fetches the value in the memory cell whose address is referenced by the pointer. (*)

30 Pointer Types (C) # include ; int main(){ int k, n; int *p; //Pointer declaration k = 560; p = &k; //Pointer assignment n = *p; //Pointer dereferencing printf("\n n = %d ",n); //Prints 560 return(0); }

31 Pointer Variables - Applications Indirect addressing ( p=&k ) Array addressing ( *(mat+3) ) Record Field addressing ( s->id ) Allocation of dynamic storage ( p = (int*) malloc(64); )

32 Dangling Reference A dangling reference is a pointer that contains address of a dynamic variable that has been deallocated. –Pointer “p1” is set to point to a new heap-dynamic variable. –Pointer “p2” is assigned p1’s value. –The heap dynamic variable pointed to by “p1” is explicitly deallocated, and p1 is set to null. Pointer p2 is not changed. p2 is now a dangling reference.

33 Dangling Reference Heap p1 p2 Heap p1 (NULL) p2 (Deallocated)

34 Dangling Reference Problems The location being pointed to by p2 may have been reallocated to some new heap-dynamic variable. The new value will bear no relationship with the old pointer’s dereferenced value. If p2 is used to change the heap dynamic variable, then the value of the new heap dynamic variable will be destroyed.

35 Lost Object A lost heap-dynamic variable (lost object) is an allocated heap-dynamic variable that is no longer accessible to the user program, but still contains some useful data. Pointer p1 is set to point to a heap-dynamic variable. p1 is set to point to another heap dynamic variable. The first heap-dynamic variable is now inaccessible, or lost.

36 Lost Object Heap p1 Heap p1 Heap Lost object

37 Pointers and Java Java does not provide indirect addressing (&) and dereferencing operators (*). Java also does not allow any pointer arithmetic. Java does not allow explicit deallocation of storage. There is no explicit deallocation operator (such as “delete”). Since storage cannot be explicitly deallocated, you cannot have a dangling reference in Java.

38 Heap Management Never reassign a pointer variable which has a dynamic storage allocation. Before reassigning a pointer, make sure that the storage it points to, is deallocated. Immediately after deallocating a heap-dynamic storage, reset to null all the pointer variables pointing to it. Whenever a large storage allocation from heap is requested, check if enough memory is available.