Pointers. The structure of memory Computer memory is a linear sequence of addressable locations Addresses are numbered starting at zero In personal computers,

Slides:



Advertisements
Similar presentations
UNIT 9: Pointers Data Variable and Pointer Variable Pass by Reference
Advertisements

Chapter 6 Data Types
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Kernighan/Ritchie: Kelley/Pohl:
Elementary Data Types Prof. Alamdeep Singh. Scalar Data Types Scalar data types represent a single object, i.e. only one value can be derived. In general,
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
Memory Arrangement Memory is arrange in a sequence of addressable units (usually bytes) –sizeof( ) return the number of units it takes to store a type.
Pointer. Warning! Dangerous Curves C (and C++) have just about the most powerful, flexible and dangerous pointers in the world. –Most other languages.
Pointers Pointer - A pointer is a derived data type; that is it is a data type built from one of the standard types. Its value is any of the addresses.
Starting out with C++1 Chapter 9 – Pointers Getting the address of a Variable Why do we have pointers? Indirection – difference between –Will you go out.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
CS 61C L4 Structs (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #4: Strings & Structs
Object References. Objects An array is a collection of values, all of the same type An object is a collection of values, which may be of different types.
Elementary Data Types Scalar Data Types Numerical Data Types Other
1 CS 201 Dynamic Data Structures Debzani Deb. 2 Run time memory layout When a program is loaded into memory, it is organized into four areas of memory.
Structured Data Types and Encapsulation Mechanisms to create new data types: –Structured data Homogeneous: arrays, lists, sets, Non-homogeneous: records.
Arrays. 2 The array data structure An array is an indexed sequence of components Typically, the array occupies sequential storage locations The length.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Adapted from Dr. Craig Chase, The University of Texas at Austin.
Pointers Applications
Computer Skills2 for Scientific Colleges 1 Pointers in C++ Topics to cover: Overview of Pointers Pointer Declaration Pointer Assignment Pointer Arithmetic.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
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.
Basic Semantics Associating meaning with language entities.
SPL – Practical Session 2 Topics: – C++ Memory Management – Pointers.
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.
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Types(1). Lecture 52 Type(1)  A type is a collection of values and operations on those values. Integer type  values..., -2, -1, 0, 1, 2,...  operations.
Copyright Curt Hill Variables What are they? Why do we need them?
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Introduction to Computer Organization & Systems Topics: C arrays C pointers COMP Spring 2014 C Part IV.
Pointers *, &, array similarities, functions, sizeof.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
Topics memory alignment and structures typedef for struct names bitwise & for viewing bits malloc and free (dynamic storage in C) new and delete (dynamic.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Object Oriented Programming Lecture 2: BallWorld.
CSI 3125, Data Types, page 1 Data types Outline Primitive data types Structured data types Strings Enumerated types Arrays Records Pointers Reading assignment.
You learned how to declare pointer variables how to store the address of a variable into a pointer variable of the same type as the variable how to manipulate.
1 Memory, Arrays & Pointers. Memory 2 int main() { char c; int i,j; double x; cijx.
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
Recap Resizing the Vector Push_back function Parameters passing Mechanism Primitive Arrays of Constants Multidimensional Arrays The Standard Library string.
CSE 220 – C Programming malloc, calloc, realloc.
CSC 533: Programming Languages Spring 2016
Data Types In Text: Chapter 6.
Stack and Heap Memory Stack resident variables include:
Computer Organization and Design Pointers, Arrays and Strings in C
CSC 533: Programming Languages Spring 2015
Chapter 6 – Data Types CSCE 343.
Memory and Addresses Memory is just a sequence of byte-sized storage devices. The bytes are assigned numeric addresses, starting with zero, just like the.
Arrays and Pointers Reference: Chapter , 4.11 CMSC 202.
Computer Skills2 for Scientific Colleges
C++ Pointers and Strings
C++ Pointers and Strings
CSC 533: Programming Languages Spring 2019
SPL – PS2 C++ Memory Handling.
Presentation transcript:

Pointers

The structure of memory Computer memory is a linear sequence of addressable locations Addresses are numbered starting at zero In personal computers, the smallest addressable unit is a byte (8 bits) In large scientific computers, the unit is a "word" –A word is typically 32 to 128 bits, or even more

Pointers A pointer is an address of a storage location By convention, zero represents the "null" pointer A pointer is a number, and must itself be stored The size of a pointer depends on the amount of memory to be addressed A 16-bit pointer can address 64K locations A 32-bit pointer can address 4 trillion locations

Pointers in higher-level languages FORTRAN and Algol had no (user-level) pointers C makes pointers an arithmetic type Pascal provides pointers, but restricts their use Java has pointers but tries to hide them from the user –...but it throws a NullPointerException!

char pointers in C For any data type, whether built-in or user- defined, C allows a pointer to that type char *pch makes pch a pointer to a character You can use *pch as a character You can use pch as a pointer to a character You can do arithmetic on pointers pch++ increments pch by the size of a character

Other pointers in C If T is a type, T *p declares p a pointer to that type You can use p as a pointer to a T You can use *p as a T p++ increments p by the size of a T –Important because of the way arrays are treated You can make a pointer to any variable –If x is any variable, then &x is its address

Storage allocation in C You can allocate storage (from the "heap") –To allocate storage, use malloc(num_bytes) –This gives a pointer of unknown type: (void *) You then cast the pointer to the desired type Example: –int *myStorage; myStorage = (int *) malloc(100); –This gives you space for 25 integers (100 bytes)

Arrays in C Array indexing is syntactic sugar for pointers a[i] is treated as *(a+i) To zero out an array: –for (i = 0; i < size; i++) a[i] = 0; –for (i = 0; i < size; i++) *(a+i) = 0; –for (p = a; P < a+size; p++) *p = 0; Because a[i] means *(a+i), i[a] is equally legal!

Strings in C A character in C is a single (ASCII-encoded) byte A string is a pointer to a character (a char*) The first zero character ends the string You must allocate space for a string –str = (char *) malloc(101); To process all the characters of a string: –for (p = str; *p != 0; p++) process (*p);

Parameter transmission in C All parameters are passed by value To fake pass by reference, pass a pointer Example: to add 1 to x, call addOne(&x) Where: void addOne(int *n) { *n = *n + 1; } "Real" call by reference does exactly this, but automatically (so you don't need the *n syntax)

Problems with pointers in C No bounds checking--pointers can point outside the array,or even outside the program No type checking--you can cast a pointer to anything Memory leaks--you can forget to deallocate storage when you're done with it Dangling references--you can deallocate storage before you're done with it C enthusiasts say you just have to be careful

Pointers in Pascal p: ^T; makes p a pointer to type T To follow the pointer (and get the T ), say p^ To allocate space for a T, say new(p) You can assign pointers to pointer variables You can test pointers for equality and for null You can NOT do pointer arithmetic...and that's about all

Pascal needs pointers less than C Pascal has true arrays A string is an array of characters –Strings are clumsy, but they don't use pointers Pascal has both call by value and call by reference –No clumsy syntax needed in called routine Pointers are still adequate for building data structures such as linked lists, trees, etc.

Pointers in Java Java calls pointers "references" You cannot directly manipulate references in Java Any object reference is effectively a pointer You can allocate memory (with new ), use references, compare references, and do pointer- like things References in Java are practically the same as pointers in Pascal, but with a simpler syntax

The End