CSC 270 – Survey of Programming Languages C Lecture 6 – Pointers and Dynamic Arrays Modified from Dr. Siegfried.

Slides:



Advertisements
Similar presentations
Dynamic memory allocation
Advertisements

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.
Chapter 6 Data Types
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.
Copyright © 2000, Daniel W. Lewis. All Rights Reserved. CHAPTER 9 MEMORY MANAGEMENT.
Pointers in C Rohit Khokher
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.
Kernighan/Ritchie: Kelley/Pohl:
POINTER Prepared by MMD, Edited by MSY1.  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference.
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
Informática II Prof. Dr. Gustavo Patiño MJ
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.
Chapter 9. 2 Objectives You should be able to describe: Addresses and Pointers Array Names as Pointers Pointer Arithmetic Passing Addresses Common Programming.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
1 Lecture 9  Arrays  Declaration  Initialization  Applications  Pointers  Declaration  The & and * operators  NULL pointer  Initialization  Readings:
Lecture 2 Pointers Pointers with Arrays Dynamic Memory Allocation.
ARRAYS AND POINTERS Although pointer types are not integer types, some integer arithmetic operators can be applied to pointers. The affect of this arithmetic.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Pointers Applications
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
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.
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,
Pointers Pointer a data type stores a memory address points to whatever the memory location contains A pointer is a variable that can store a memory address.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 4 Pointers and Dynamic Arrays Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
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 and Dynamic Memory Allocation. Declaring a pointer.
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:
1 Pointers to structs. 2 A pointer to a struct is used in the same way as a pointer to a simple type, such as an int. Pointers to structs were introduced.
Slide 1 Chapter 10 Pointers and Dynamic Arrays. Slide 2 Learning Objectives  Pointers  Pointer variables  Memory management  Dynamic Arrays  Creating.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Pointers and Dynamic Arrays
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
Review 1 List Data Structure List operations List Implementation Array Linked List.
Pointers *, &, array similarities, functions, sizeof.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Pointers and Dynamic Arrays.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
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.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
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.
Windows Programming Lecture 03. Pointers and Arrays.
Pointers CSC1201: Programming Language 2. Topics Pointers ▫Memory addresses ▫Declaration ▫Dereferencing a pointer ▫Pointers to pointer.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Pointers and Dynamic Arrays
Values – also known as scalars (eg. int, double, char, float)
Dynamic Memory Allocation
CSC 270 – Survey of Programming Languages
Chapter 10: Pointers 1.
Object Oriented Programming COP3330 / CGS5409
Pointers and Dynamic Variables
Pointers, Dynamic Data, and Reference Types
Objectives You should be able to describe: Addresses and Pointers
Pointers, Dynamic Data, and Reference Types
Presentation transcript:

CSC 270 – Survey of Programming Languages C Lecture 6 – Pointers and Dynamic Arrays Modified from Dr. Siegfried

What is a Pointer? A point is a variable whose values are memory addresses. We call it a pointer because we envision the address as “pointing” to where the value is stored. Deferencing a pointer gets the value at that memory address Reference parameters make use of pointers. Arrays are passed by reference because the name of an array (without an index following it) is a pointer to where the array is stored.

Pointer Variables When we write doublex; we are saying that there is a double-precision value stored in memory and x is the value at that location. When we write double *p we are saying that p is a pointer to a double value that is stored in memory and that p’s value is the address at which the value is stored. (Read backwards) Note: could be double* p instead.

Assign and Dereference Pointers #include int main(void) { int a; int * aPtr; a = 7; aPtr = &a; printf("The address of a is %p\nThe value of aPtr is %p\n",&a,aPtr); printf("The value of a is %d\nThe value of *aPtr is %d\n",a,*aPtr); printf("\nThe result of &*aPtr is %p\n and the value of *&aPtr is %p\n", &*aPtr,*&aPtr); printf("\nThe result of *&a is %d\n",*&a); //CANT DO THIS because you cannot deference an int: printf("%d",*a); }

Deferencing Result The address of a is 0x7fff22344cfc The value of aPtr is 0x7fff22344cfc The value of a is 7 The value of *aPtr is 7 The result of &*aPtr is 0x7fff22344cfc and the value of *&aPtr is 0x7fff22344cfc The result of *&a is 7

Declaring and Using Pointer Variables We can declare several pointer variables in the same statement, even together with variable of the type to which they point: intv1, v2, v3, *p1, *p2, *p3; We can assign values to pointers using the referencing operator ( & ): p1 = &v1;/* p1 holds the address where v1 is stored.*/

Using Pointers v1 = 0; p1 = &v1; *p1 = 42; printf("%d\n", v1); printf("%d\n", *p1); Output 42

Pointers and the Assignment Operation Assign a pointer to another pointer p2 = p1 printf("%d\n", *p2) ; will also produce 42 (unless v1’s value was changed).

p1 = p2 p1 8 p2 9 p1 8 p2 9 BeforeAfter

*p1 = *p2 p1 8 p2 9 p1 9 p2 9 BeforeAfter

malloc() The library function malloc() is used to allocate memory for a data item and then to assign its address to a pointer variable. The prototype for malloc() is void* malloc (size_t size); where size_t is an unsigned integer type Variables that are created using malloc() are called dynamically allocated variables. Found in stdlib.h

malloc() - An Example p1 = (int *) malloc(sizeof(int)); scanf("%d", p1); *p1 = *p1 + 7; printf("%d", *p1);

free() The function free() eliminates a dynamic variable and returns the memory that the dynamic variable occupied to the heap. It can be re-used. The prototype: void free (void* ptr); After the delete statement, p ’s value is undefined.

Demo Dynamic Variables #include int main (void){ int *p1, *p2; p1 = (int *) malloc(sizeof(int)); *p1 = 42; p2 = p1; printf ("*p1 == %d\n *p2 == %d\n", *p1,*p2); *p2 = 53; puts ("\n\nAfter *p2 = 53:\n"); printf("*p1 == %d\n *p2 == %d\n", *p1,*p2); p1 = (int *) malloc(sizeof(int));

*p1 = 88; *p2 = 33; puts ("\n\nAfter *p1 = 88 and *p2 = 33"); puts("and created new var for p1 to reference:\n"); printf("*p1 == %d\n *p2 == %d\n", *p1,*p2); free(p1); // why not free p1 before second malloc??? free(p2); return(0); }

Output from BasicPointer.c *p1 == 42 *p2 == 42 After *p2 = 53: *p1 == 53 *p2 == 53 After *p1 = 88 and *p2 = 33 and created new var for p1 to reference: *p1 == 88 *p2 == 33

Explaining BasicPointer.c ? p1 ? p2 int *p1, *p2; p1 ? ? p2 p1 = (int *) malloc(sizeof(int));

Explaining BasicPointer.c p1 42 ? p2 *p1 = 42; p1 42 p2 p2 = p1;

Explaining BasicPointer.c p1 53 p2 *p2 = 53; p1 ? p2 53 p1 = (int *) malloc(sizeof(int));

Explaining BasicPointer.c p1 88 p2 33 *p1 = 88; *p2 = 33

Basic Memory Management The heap is a special area of memory reserved for dynamically allocated variables. Compilers would return NULL if there wasn’t enough memory when calling malloc(). It could potentially cause the program to abort execution.

Stopping Errors with malloc() int*p; p = (int *) malloc(sizeof(int)); if (p == NULL){ printf("Insufficient memory\n"); exit(1); } /*If malloc()succeeded, the program continues here */

NULL NULL is actually the number 0, but we prefer to think of it as a special-purpose value.. NULL ’s definition appears in, and NULL can be assigned to a pointer variable of any type.

Dangling Pointers A dangling pointer is an undefined pointer. It either has no address assigned yet or what it pointed to is now gone. If p is a dangling pointer, then *p references memory that has been returned to the heap and the result is unpredictable. C has no built-in mechanism for checking for dangling pointers. – For this reason, it is always a good idea to set dangling pointers to NULL.

Dynamic Variables Variables created using the malloc function are called dynamic variables (they are created and destroyed while the program is running. Storage for local variables are allocated when the function is called and de-allocated when the function call is completed. They are called automatic variables because this is all done automatically. Variables declared outside any function or class definition are called external (or global) variables. They are statically allocated because their storage is allocated when the program is translated.

typedef You can define a pointer type name so that pointer variables can be declared like other variables. E.g., typedef int * IntPtr; IntPtr p;// equivalent to int *p; typedef can be used to define any kind of data type: typedef double Kilometers; Kilometers distance;

Dynamic Arrays A dynamic array is an array whose size is not specifically when you write the program. Example inta[10]; typedef int*IntPtr; IntPtrp; … p = a; /* p[i] refers to a[i] */

ArrayDemo.c // Program to demonstrate that an array variable is // a kind of pointer variable #include typedef int* IntPtr; intmain(void) { IntPtrp; inta[10]; intindex; for (index = 0; index < 10; index++) a[index] = index;

p = a; for (index = 0; index < 10; index++) printf("%d ", p[index]); printf("\n"); for (index = 0; index < 10; index++) p[index] = p[index] + 1; for (index = 0; index < 10; index++) printf("%d ", a[index]); printf("\n"); return(0); } Output

Creating and Using Dynamic Arrays You do not always know in advance what size an array should be. Dynamic arrays allow the programmer to create arrays that are flexible in size: typedef double*DoublePtr; DoublePtrd; d = (double *) malloc (10*sizeof(double));

DynArrayDemo.c // Searches a list of numbers entered at the // keyboard #include typedef int*IntPtr; voidfillArray(int a[], int size); // Precondition: size is the size of the array a // Postcondition: a[0] through a[size-1] have been // filled with values read from the keyboard.

intsearch(int a[], int size, int target); // Precondition: size is the size of the array a // The array elements a[0] through a[size-1] have // values. // If target is in the array, returns the first index // of target // If target is not in the array, returns -1. intmain(void) { printf("This program search a list of " " numbers.\n"); intarraySize, target; intlocation;

printf("How many numbers will be on the " "list\t?"); scanf("%d", &arraySize); IntPtra; a = (int *) malloc(arraySize*sizeof(int)); fillArray(a, arraySize); printf("Enter a value to search for:\t?"); scanf("%d", &target); location = search(a, arraySize, target);

if (location == -1) printf("%d is not in the array.\n", target); else printf("%d is element %d in the array.\n", target, location); free(a); return(0); }

// Uses the library : voidfillArray(int a[], int size) { printf("Enter %d integers.", size); int index; for ( index = 0; index < size; index++) scanf("%d", &a[index]); }

int search(int a[], int size, int target) { int index = 0; while ((a[index] != target) && (index < size)) index++; if (index == size) /* If target is not in a */ index = -1; return index; }

Why use free(a); ? The free(a) function call is necessary if the program will do other things after finishing its use of a dynamic array, so the memory can be reused for other purposes.

PtrDemo.c #include int* doubler (int a[], int size); /* * Precondition: size is the size of the array a * A indexed variables of a have values. * Returns: a pointer to an array of the same size *as a in which each index variable is *double the corresponding element in a. */

intmain(void) { inta[] = {1, 2, 3, 4, 5}; int*b; b = doubler(a, 5); inti; printf("array a:\n"); for (i = 0; i < 5; i++) printf("%d ", a[i]); printf("\n"); printf("Array b:\n"); for (i = 0; i < 5; i++) printf("%d ", b[i]);

printf("\n"); free(b); return(0); } int*doubler(int a[], int size) { int*temp; temp = (int *) malloc(size*sizeof(int)); int i; for (i = 0; i < size; i++) temp[i] = 2*a[i]; return temp; }

Output from PtrDemo.cpp array a: Array b:

Pointer Arithmetic If p is a pointer, p++ increment p to point to the next element and p += i; has p point i elements beyond where it currently points. Example typedef double*DoublePtr; DoublePtrd; d = new double[10]; d +1 points to d[1], d+2 points to d[2]. If d = 2000, d+1 = 2008 ( double use 4 bytes of memory).

Pointer Arithmetic – An Example for (i = 0; i < arraySize; i++) printf("%d ", *(d+i)); is equivalent to for (i = 0; i < arraySize; i++) printf("%d ", d[i]);

Pointers and ++ and -- You can also use the increment and decrement operators, ++ and – to perform pointer arithmetic. Example d++ advances the pointer to the address of the next element in the array and d–- will set the pointer to the address of the previous element in the array.

Multidimensional Dynamic Arrays Multidimensional dynamic arrays are really arrays of arrays or arrays of arrays of arrays, etc. To create a 2-dimensional array of integers, you first create an array of pointers to integers and create an array of integers for each element in the array.

Creating Multidimensional Arrays // Create a data type for to integers typedef int * IntArrayPtr; // Allocate an array of 3 integer pointers IntArrayPtr *m = (int**)malloc(3*sizeof (int *)); // Allocate for 3 arrays of 4 integers each. int i; for ( i = 0; i < 3; i++) m[i] = (int *) malloc(4*sizeof(int)); // Initialize them all to 0 for ( i = 0; I < n; i++) for (int j = 0; j < n; j++) m[i][j] = 0;

free Since m is an array of array, each of the arrays created with malloc() in the for loop must be returned to the heap using a call to free() and then afterward, m itself must be returned using free().

MultArrayDemo.c #include typedef int*IntArrayPtr; intmain(void) { intd1, d2; inti, j; IntArrayPtr *m; printf("Enter the row and column dimensions" " of the array:\t"); scanf("%d%d", &d1, &d2);

m = (IntArrayPtr *) malloc(d1 *sizeof(IntArrayPtr)); for (i = 0; i < d1; i++) m[i] = (int *) malloc(d2 * sizeof(int)); /* m is now a d1-by-d2 array. */ printf("Enter %d rows of %d integers each:\n", d1, d2); for (i = 0; i < d1; i++) for (j = 0; j < d2; j++) scanf("%d", &m[i][j]); printf("Echoing the two-dimensional array:\n");

for (i = 0; i < d1; i++){ for (j = 0; j < d2; j++) printf("%d ", m[i][j]); printf("\n"); } for (i = 0; i < d1; i++) free(m[i]); free(m); return (0); }

Output Enter the row and column dimensions of the array: 3 4 Enter 3 rows of 4 integers each: Echoing the two-dimensional array: