CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.

Slides:



Advertisements
Similar presentations
Dynamic memory allocation
Advertisements

Lectures 10 & 11.
Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
Programming and Data Structure
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.
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
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Programming in C Pointers and Arrays, malloc( ). 7/28/092 Dynamic memory In Java, objects are created on the heap using reference variables and the new.
CSCI 171 Presentation 11 Pointers. Pointer Basics.
Pointer applications. Arrays and pointers Name of an array is a pointer constant to the first element whose value cannot be changed Address and name refer.
Engineering Problem Solving with C Fundamental Concepts Chapter 6 Pointers.
Kernighan/Ritchie: Kelley/Pohl:
Engineering Problem Solving With C++ An Object Based Approach Chapter 9 Pointers and Creating Data Structures.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
Chapter 10.
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.
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.
CS 61C L4 Structs (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #4: Strings & Structs
Pointers Applications
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.
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.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
Chapter 17 Pointers and Arrays. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Pointers and Arrays.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
1 Programming with Pointers Turgay Korkmaz Office: SB Phone: (210) Fax: (210) web:
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
Pointers and Arrays Beyond Chapter Pointers and Arrays What are the real differences? Pointer Holds the address of a variable Can be pointed.
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
Stack and Heap Memory Stack resident variables include:
Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. C does not Inherently have this facility,
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 and Arrays. 2 When an array is declared,  The compiler allocates sufficient amount of storage to contain all the elements of the array in.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
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:
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect.
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
Introduction to Computer Organization & Systems Topics: C arrays C pointers COMP Spring 2014 C Part IV.
Pointers *, &, array similarities, functions, sizeof.
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
© Oxford University Press All rights reserved. CHAPTER 7 POINTERS.
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.
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
POINTERS Introduction to Systems Programming - COMP 1002, 1402.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
1 Dynamic Memory Allocation. 2 In everything we have done so far, our variables have been declared at compile time. In these slides, we will see how to.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
Announcements You will receive your scores back for Assignment 2 this week. You will have an opportunity to correct your code and resubmit it for partial.
CS162 - Topic #12 Lecture: –Arrays with Structured Elements defining and using arrays of arrays remember pointer arithmetic Programming Project –Any questions?
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.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 9.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
CSCI 125 & 161 / ENGR 144 Lecture 16 Martin van Bommel.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
Stack and Heap Memory Stack resident variables include:
Computer Organization and Design Pointers, Arrays and Strings in C
POINTERS.
Programming Languages and Paradigms
Lecture 6 C++ Programming
Object Oriented Programming COP3330 / CGS5409
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
Chapter 16 Pointers and Arrays
Programming in C Pointers and Arrays.
Presentation transcript:

CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers

C pointers What are C pointers? –Answer: variables that store memory addresses i.e., they “point” to memory locations And they can vary – be assigned a new value

A simple statement int m = 37; The C compiler has to make a lot of things happen to deal with this simple statement: A memory location is allocated for the variable m During the execution of the program, an instruction that stores the value 37 to that memory location is executed Later on in the program if we see the identifier m (in the same scope), then we know that we are referring to either –to the value stored at the memory location allocated for m, or –to the memory location allocated for m

The address operator: & Can we access the lvaue (i.e., the memory address) of a variable on the right-hand-side of an assignment (or in an expression in general)? –Yes, we can do that using the address operator & –Given a variable x, –&x is the lvalue of the variable x –Equivalently, &x is the memory address of x We have seen the & operator before: scanf(“%d”, &x); This function call is telling the scanf function to store the value it reads from the stdin input stream to the memory location &x

The dereference operator: * C language allows us to declare variables that store memory addresses: They are called pointers Given a memory address, the dereference operator * returns the value stored at that memory address We use * to declare pointer variables: int *p; /* p points to an int, i.e., dereferencing p returns an int value */

Using * and & operators Use * to declare a pointer variable: int *p; /* now p can point to an int */ Use & to retrieve a variable’s lvalue: p = &m; /* p is now storing the address of m i.e., p is pointing to m */ scanf(“%d”, p); /* stores input in m */ Use * to dereference a pointer: *p = 19; /* stores 19 at the location p points to */ printf(“an int value: %d”, *p); /* prints the value that p is pointing, i.e., prints the value stored at the memory location p */

Pointer types For dereferencing, and for pointer arithmetic: The type that a pointer points to must be known –e.g., an int * can only point to an int –Exception: a void * can point to any type So cannot dereference directly – must cast first Can a pointer point to a pointer? –Why not? Pointers are variables too! double d, *dp = &d, **dpp = &dp; **dpp = 17.5; /* stores 17.5 in d */

Pointer (address) arithmetic The operations that can be performed on pointers (or addresses): –A pointer can be assigned to another pointer of the same type –An integer value can be added to or subtracted from a pointer –A pointer can be assigned to or compared with the symbolic constant NULL which is defined in –Pointers to elements of the same array can be subtracted or compared as a means to accessing elements in the array Cannot use multiplication or division with pointers Cannot add two pointers either

Incrementing a pointer Incrementing a pointer does not necessarily increase the address value by one –It increases it based on the size of the memory required to store the value it points to. Execute the following code and see what happens: char *cptr=(char *)1000; int *iptr=(int *)1000; double *dptr=(double *)1000; cptr++; iptr++; dptr++; printf("New value of char ptr : %u\n",cptr); printf("New value of intr ptr : %u\n",iptr); printf("New value of double ptr : %u\n",dptr);

Array names are not pointers, but they are like pointers int x[10]; What does this declaration do? –Allocates 10 consecutive int locations –Also permanently associates x with the address of the first of these int locations – i.e., x is the same as &x[0] &x[i] is exactly the same as (x+i) x[i] is exactly the same as *(x+i) So why is x not a pointer? –You cannot modify the value of x, it is always pointing to the first element of the array

Example The two loops below are doing the same thing int x[] = { 1, 2, 3, 4, 5, 6}; int *ptr = x; int sum1 = 0, sum2 = 0; for (k = 0; k < 6; k++) { sum1 += x[k]; } for (k = 0; k < 6; k++) { sum2 += *(ptr + k); } printf("Sum is %d and %d\n", sum1, sum2);

More pointers vs. array names If p is int* (i.e., a pointer to int ), then … –p = &x[0] is okay –And p = x has exactly the same effect Moreover p[i] is now an alias for x[i] –Until ++p moves p to point at x[1] or beyond But x = p is illegal (assuming x is an array name)

Parameter passing with pointers In C variables always passed to functions “by value” –So functions need pointers to change values change(x); /* function cannot change x’s value */ change(&x); /* function may change x’s value */ Return values are copies too – so similar issues

A parameter passing example void triple1(int x) { x = x * 3; } void triple2(int *x) { *x = *x * 3; } Later in main function, for instance: int a[] = {10, 7}; triple1(a[0]); /* What is being passed? */ printf("%d\n", a[0]); /* What is printed? */ triple2(a); /* What is being passed? */ printf("%d\n", a[0]); /* What is printed? */ Be sure to understand why these results occur. –Hint: draw the memory storage – including storage duration

Why do we have to deal with pointers? Given that programming using pointers seems somewhat challenging, why do we need them? One reason is, as we have seen earlier, pointers allow us to write functions with side-effects, i.e., the changes made in the function body are reflected to the caller You have to use this carefully since side-effects are hard to understand when someone else looks at your code. A more significant reason for using pointers is that, without pointers, we would be forced to know the size of each memory we plan to allocate before runtime We would need to know the sizes of the arrays we plan to allocate for example But sometimes, the size of an array we plan to allocate may depend on the size of the input. What can we do?

Two ways to allocate memory Static memory allocation – done at compile-time int x; double a[5]; /* allocates space for 1 int, 5 doubles */ Both size and type are clearly specified ahead of time – x can only hold int values, a only double s Dynamic memory allocation – done during execution Must use library methods like malloc malloc allocates specific amount of memory, returns void * Cast to appropriate pointer type – then use as always int *ip = (int *)malloc(sizeof(int)); Notes: returns NULL if memory not available so must check that; cast is optional Must free the memory when done with it: free(ip);

What is sizeof ? A unary operator – computes the size, in bytes, of any object or type –Usage: sizeof object or sizeof(type) –If x is an int, sizeof x == sizeof(int) is true Works for arrays too – total bytes in whole array

The malloc function malloc is used for dynamic memory allocation The argument of the malloc function is the number of bytes of memory required (where each byte is 8 bits). For example, if we want to allocate a memory for 10 integers, we need to figure out how many bytes we need per integer and then multiple that by 10. We can use sizeof(int) to get the number of bytes used per integer value Note that this value is system dependent If there is not enough memory for allocation, then malloc returns NULL calloc : like malloc but initializes the allocated memory to 0 realloc : can be used to change the size of the allocated memory

Pointer tutorial Pointer fun

Strings in C A char array, terminated by '\0' String literal – anything enclosed in double quotes –e.g., "cat" – automatically includes terminator Must allocate memory to store the characters char sentence[50]; /* up to 49 chars plus '\0' */ char word[] = "cat"; /* length is 3, but size is 4 */ Point to such memory with char* (pointer to char) char *s = sentence; s = word; cat\0

Strings in C Equivalent declarations of a string char filename[11] = “sensor.txt”; char filename[] = “sensor.txt”; char filename[] = {‘s’, ‘e’, ‘n’, ‘s’, ‘o’, ‘r’, ‘.’, ‘t’, ‘x’, ‘t’, ‘\0’};

String library functions The standard C library contains a number of functions for strings which are defined in string.h : strlen(t) : Returns the length of the string s. strcopy(s, t) : Copies string t to string s and returns a pointer to s. strcat(s, t) : Concatenates string t to the end of string s. strcmp(s, t): Compares string s to string t in an element-by-element comparison. A negative values is returned if s t ;

Topics we have discussed after the first midterm File IO Functions Parameter passing Scope, global vs. local variables Recursive functions Arrays Multi-dimensional arrays Arrays as function parameters Pointers Address and dereference operators Pointers and arrays C strings dynamic memory allocation