C pointers (Reek, Ch. 6) 1CS 3090: Safety Critical Programming in C.

Slides:



Advertisements
Similar presentations
Lectures 10 & 11.
Advertisements

Programming and Data Structure
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.
Pointers in C Rohit Khokher
Pointers & Dynamic Memory Allocation Mugurel Ionu Andreica Spring 2012.
Dr. Kalpakis CMSC 661, Principles of Database Systems Representing Data Elements [12]
1 Pointers Lecture Introduction Pointers  Powerful, but difficult to master  Simulate pass-by-reference  Close relationship with arrays and.
Pointer, malloc and realloc 1. Name entered was 6 char, not enough space to put null terminator 2 Array of char.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
Pointer. Warning! Dangerous Curves C (and C++) have just about the most powerful, flexible and dangerous pointers in the world. –Most other languages.
C data types and declarations (Reek, Ch. 3) 1CS 3090: Safety Critical Programming in C.
C strings (Reek, Ch. 9) 1CS 3090: Safety Critical Programming in C.
C structures and unions (Reek, Ch. 10) 1CS 3090: Safety Critical Programming in C.
CS 61C L03 C Arrays (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #3: C Pointers & Arrays
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
Even More C Programming Pointers. Names and Addresses every variable has a location in memory. This memory location is uniquely determined by a memory.
Adapted from Dr. Craig Chase, The University of Texas at Austin.
1 The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into.
C arrays (Reek, Ch. 8) 1CS 3090: Safety Critical Programming in C.
CS 61C L03 C Arrays (1) A Carle, Summer 2006 © UCB inst.eecs.berkeley.edu/~cs61c/ CS61C : Machine Structures Lecture #3: C Pointers & Arrays
Copyright 2001 Oxford Consulting, Ltd1 January Pointers and More Pointers Pointers and Arrays We’ve now looked at  Pointers  Arrays  Strings.
C Arrays and Pointers In Java, pointers are easy to deal with –In fact, there is little that can go wrong in Java since pointer access is done for you.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Introduction to Pointers.. What is a pointer? A derived type which holds the address of a variable. Simply a memory location where the variable is stored.
Variables and Objects, pointers and addresses: Chapter 3, Slide 1 variables and data objects are data containers with names the value of the variable is.
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.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
CSE 232: C++ pointers, arrays, and references Overview of References and Pointers Often need to refer to another object –Without making a copy of the object.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
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.
SPL – Practical Session 2 Topics: – C++ Memory Management – Pointers.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
1 Pointers and Strings Chapter 5 2 What You Will Learn...  How to use pointers Passing arguments to functions with pointers See relationship of pointers.
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Pointer Arithmetic CSE 2541 Rong Shi. Pointer definition A variable whose value refers directly to (or "points to") another value stored elsewhere in.
November 18, 2015Memory and Pointers in Assembly1 What does this C code do? int foo(char *s) { int L = 0; while (*s++) { ++L; } return L; }
Copyright Curt Hill Variables What are they? Why do we need them?
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.
1 CSC 211 Data Structures Lecture 5 Dr. Iftikhar Azim Niaz 1.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
C programming---Pointers The first step: visualizing what pointers represent at the machine level. In most modern computers, main memory is divided into.
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.
1 Pointers: Parameter Passing and Return. 2 Passing Pointers to a Function Pointers are often passed to a function as arguments  Allows data items within.
Chapter 16 Pointers and Arrays Pointers and Arrays We've seen examples of both of these in our LC-3 programs; now we'll see them in C. Pointer Address.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Pointers. What Is Pointer l every variable has memory address char c=’y’; int i=2; address of variable i is 0022 l address can used to refer to this variable.
Pointers. Introduction to pointers Pointer variables contain memory addresses as their values. Usually, a variable directly contains a specific value.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Windows Programming Lecture 03. Pointers and Arrays.
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.
C arrays (Reek, Ch. 8) CS 3090: Safety Critical Programming in C.
Motivation and Overview
Basic notes on pointers in C
Object Oriented Programming COP3330 / CGS5409
Arrays and Pointers Reference: Chapter , 4.11 CMSC 202.
Pointers.
C++ Pointers and Strings
ECE 103 Engineering Programming Chapter 38 C Pointers, Part 2
C++ Pointers and Strings
SPL – PS2 C++ Memory Handling.
Introduction to Pointers
Presentation transcript:

C pointers (Reek, Ch. 6) 1CS 3090: Safety Critical Programming in C

Review of pointers CS 3090: Safety Critical Programming in C2  A pointer is just a memory location.  A memory location is simply an integer value, that we interpret as an address in memory.  The contents at a particular memory location are just a collection of bits – there’s nothing special about them that makes them int s, char s, etc.  How you want to interpret the bits is up to you.  Is this... an int value?... a pointer to a memory address?... a series of char values? 0xfe4a10c5

Review of pointer variables CS 3090: Safety Critical Programming in C3  A pointer variable is just a variable, that contains a value that we interpret as a memory address.  Just like an uninitialized int variable holds some arbitrary “garbage” value, an uninitialized pointer variable points to some arbitrary “garbage address” char *m; (char *) m

Following a “garbage” pointer CS 3090: Safety Critical Programming in C4  What will happen? Depends on what the arbitrary memory address is:  If it’s an address to memory that the OS has not allocated to our program, we get a segmentation fault  If it’s a nonexistent address, we get a bus error  Some systems require multibyte data items, like int s, to be aligned: for instance, an int may have to start at an even- numbered address, or an address that’s a multiple of 4. If our access violates a restriction like this, we get a bus error  If we’re really unlucky, we’ll access memory that is allocated for our program – We can then proceed to destroy our own data!

How can you test whether a pointer points to something meaningful? CS 3090: Safety Critical Programming in C5  There is a special pointer value NULL, that signifies “pointing to nothing”. You can also use the value 0. char *m = NULL;... if (m) {... safe to follow the pointer... }  Here, m is used as a Boolean value  If m is “false”, aka 0, aka NULL, it is not pointing to anything  Otherwise, it is (presumably) pointing to something good  Note: It is up to the programmer to assign NULL values when necessary

Indirection operator * CS 3090: Safety Critical Programming in C6  Moves from address to contents char*m = ″ dog ″ ; char result = *m; m gives an address of a char *m instructs us to take the contents of that address result gets the value ′ d ′ (char *) m d (char) o (char) g (char) NUL (char) result d (char)

Address operator & CS 3090: Safety Critical Programming in C7  Instead of contents, returns the address char*m = ″ dog ″, **pm = &m; pm needs a value of type char **  Can we give it *m ? No – type is char  Can we give it m ? No – type is char *  &m gives it the right value – the address of a char * value (char *) (char **) m pm d (char) o (char) g (char) NUL (char)

Pointer arithmetic CS 3090: Safety Critical Programming in C8  C allows pointer values to be incremented by integer values char*m = ″ dog ″ ; char result = *(m + 1); m gives an address of a char (m + 1) gives the char one byte higher *(m + 1) instructs us to take the contents of that address result gets the value ′ o ′ (char *) m d (char) o (char) g (char) NUL (char) result o (char)

Pointer arithmetic CS 3090: Safety Critical Programming in C9  A slightly more complex example: char*m = ″ dog ″ ; char result = *++m; m gives an address of a char ++m changes m, to the address one byte higher, and returns the new address *++m instructs us to take the contents of that location result gets the value ′ o ′ (char *) m d (char) o (char) g (char) NUL (char) result o (char)

Pointer arithmetic CS 3090: Safety Critical Programming in C10  How about multibyte values?  Q: Each char value occupies exactly one byte, so obviously incrementing the pointer by one takes you to a new char value... But what about types like int that span more than one byte?  A: C “does the right thing”: increments the pointer by the size of one int value inta[2] = {17, 42}; int m = a; int result = *++m; (int *) m (char) result 42 (int) 17 (int) 42 (int)

Example: initializing an array CS 3090: Safety Critical Programming in C11 #define N_VALUES 5 float values[N_VALUES]; float *vp; for ( vp = &values[0]; vp < &values[N_VALUES]; ) *vp++ = 0; (float *) vp (float) &values[0] &values [N_VALUES] values (float []) 0 (float) 0 (float) 0 (float) 0 (float) 0 (float) (done!)

A note on assignment: Rvalues vs. Lvalues CS 3090: Safety Critical Programming in C12  What’s really going on in an assignment?  Different things happen on either side of the = int a = 17, b = 42; b = a; 17 (int) a 42 (int) b a is the “rvalue” (right value) We go to the address given by a... and get the contents ( 17 ) b is the “lvalue” (left value) We go to the address given by b...and get the contents? No! We don’t care about 42! We just want the address of b – to store 17 into

A note on assignment: Rvalues vs. Lvalues CS 3090: Safety Critical Programming in C13  This explains a certain “asymmetry” in assignments involving pointers: char *m = NULL, **pm = NULL; m = “dog”; pm = &m; (char *) (char **) m pm d (char) o (char) g (char) NUL (char) Here, m is an lvalue – It’s understood that the address of m is what’s needed Once again, we need the address of m – but since it’s an rvalue, just plain m will give the contents of m – use & to get the address instead

Example: strcpy “string copy” CS 3090: Safety Critical Programming in C14 char *strcpy(char *dest, const char *src)  (assume that) src points to a sequence of char values that we wish to copy, terminated by NUL  (assume that) dest points to an accessible portion of memory large enough to hold the copied chars  strcpy copies the char values of src to the memory pointed to by dest  strcpy also gives dest as a return value

Example: strcpy “string copy” CS 3090: Safety Critical Programming in C15 char *strcpy(char *dest, const char *src) { const char *p; char *q; for(p = src, q = dest; *p != '\0'; p++, q++) *q = *p; *q = '\0'; return dest; } d (char) o (char) g (char) NUL (char) (char *) src (char *) dest (char *) pq d (char) o (char) g (char) NUL (char)

Pointer subtraction and relational operations CS 3090: Safety Critical Programming in C16  Only meaningful in special context: where you have two pointers referencing different elements of the same array  q – p gives the difference (in number of array elements, not number of bytes between p and q (in this example, 2 )  p < q returns 1 if p has a lower address than q ; else 0 (in this example, it returns 1) (float) (float *) p q