Kernighan/Ritchie: Kelley/Pohl:

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.
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.
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:
POINTER Prepared by MMD, Edited by MSY1.  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference.
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.
Dynamic Data Structures H&K Chapter 14 Instructor – Gokcen Cilingir Cpt S 121 (July 26, 2011) Washington State University.
Chapter 9. 2 Objectives You should be able to describe: Addresses and Pointers Array Names as Pointers Pointer Arithmetic Passing Addresses Common Programming.
CS100A, Fall 1997, Lecture 241 CS100A, Fall 1997 Lecture 24, Tuesday 25 November (There were no written notes for lecture 23 on Nov. 20.) Data Structures.
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.
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.
1 Chapter 9 Arrays and Pointers. 2  One-dimensional arrays  The Relationship between Arrays and Pointers  Pointer Arithmetic and Element Size  Passing.
CS100A, Fall 1997, Lectures 221 CS100A, Fall 1997 Lecture 22, Tuesday 18 November Introduction To C Goal: Acquire a reading knowledge of basic C. Concepts:
1 Chapter 8 Functions, Pointers, and Storage Classes  Pointer  Pointers to void  Call-by-Reference  Basic Scope Rules  Storage Classes  Default Initialization.
Even More C Programming Pointers. Names and Addresses every variable has a location in memory. This memory location is uniquely determined by a memory.
Pointers Applications
C Programming Lecture 14 Arrays. What is an Array? b An array is a sequence of data items that are: all of the same typeall of the same type –a sequence.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. C does not Inherently have this facility,
Functions Kernighan/Ritchie: Kelley/Pohl: Chapter 4 Chapter 5.
ARRAY Prepared by MMD, Edited by MSY1.  Introduction to arrays  Declaring arrays  Initializing arrays  Examples using arrays  Relationship with pointers.
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.
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
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.
Chapter 8: Arrays Introduction to arrays Declaring arrays Initializing arrays Examples using arrays Relationship with pointers Array passing to a function.
1 CHAPTER 5 POINTER. 2 Pointers  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference  Dynamic.
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:
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.
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.
Integer Types short month; // half of a machine word int car; // one machine word unsigned long distance; Character Types char c = ‘\0’; // one byte char.
Review 1 List Data Structure List operations List Implementation Array Linked List.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
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.
Prepared by MMD, Edited by MSY1 CHAPTER 4 ARRAY. Prepared by MMD, Edited by MSY2 Arrays  Introduction to arrays  Declaring arrays  Initializing arrays.
Arrays and Pointers.
Pointers1 WHAT IS A POINTER? Simply stated, a pointer is an address. A running program consists of three parts: execution stack, code, and data. They are.
17-Feb-02 Sudeshna Sarkar, CSE, IT Kharagpur1 Arrays and Pointers Lecture 17 18/2/2002.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
CS162 - Topic #12 Lecture: –Arrays with Structured Elements defining and using arrays of arrays remember pointer arithmetic Programming Project –Any questions?
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 9.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Windows Programming Lecture 03. Pointers and Arrays.
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.
CSE 220 – C Programming malloc, calloc, realloc.
Computer Organization and Design Pointers, Arrays and Strings in C
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
Lecture 18 Arrays and Pointer Arithmetic
Presentation transcript:

Kernighan/Ritchie: Kelley/Pohl: Pointers and Arrays Kernighan/Ritchie: Kelley/Pohl: Chapter 5 Chapter 6

Lecture Overview Arrays Pointers Call-by-reference Arrays, pointers and pointer arithmetic Dynamic Memory Allocation

Arrays Programs often use homogeneous data For example: When the size of the data is too large, arrays are a better solution: int grade0, grade1, grade2; int grades[3];

Arrays It is good programming practice to define the size of an array as a symbolic constant The standard idiom for processing an array defined this way is using a for loop #define SIZE 8 int array[SIZE]; for (i = 0; i < SIZE; i++) printf ("%d ", array[i]);

Array Initialization A special format exists for initializing arrays If the list of initializers is shorter than the array, the rest of the array is set to zero In this case, the last two elements (f[3] and f[4]) will be initialized to zero float f[5] = { 0.0, 1.0, 2.0, 3.0, 4.0 }; float f[5] = { 0.0, 1.0, 2.0 };

Array Initialization Depending on its storage class, an array may or may not be automatically initialized static and external arrays are always initialized – this is done by setting all of their elements to zero automatic arrays will not necessarily be initialized (depends on the compiler), and thus they should be assumed to contain garbage

Array Initialization If an array is declared without a size, it is implicitly given a size according to the number of initializers Thus, the following two array declarations are equivalent: int a[] = {2, 3, 5, -7}; int a[4] = {2, 3, 5, -7};

Array Initialization Character arrays may be initialized in the same way: However, since character arrays serve as strings in C, the following short-cut is also available (equivalent to the above): char s[] = {'a', 'b', 'c', '\0'}; char s[] = "abc";

Lecture Overview Arrays Pointers Call-by-reference Arrays, pointers and pointer arithmetic Dynamic Memory Allocation

Pointers A simple variable in a program is stored in a certain number of bytes, at some particular memory location (or address) Pointers are used to access memory and manipulate addresses If v is a variable, then &v is the address in memory where its value is stored

Pointers Memory addresses are values that can be manipulated much like integer values The following declares a pointer to int: Examples of assignment to the pointer p: int *p; p = 0; p = NULL; /* equivalent to p = 0; */ p = &i; /* i must be an integer. */ p = (int *)1776; /* an absolute memory address */

Pointers A basic example of how the pointer mechanism works: We think of the pointer p is an arrow, which at this stage is pointing to nothing: int a = 1, b = 2, *p; a b p 1 2 ?

Pointers Now, assume that the next line of code is: After this statement, the memory contents will look like this: p = &a; a b p 1 2

Pointers Finally, consider the following assignment: This assigns the value pointed to by p to b Since p points to a, this is equivalent to the statement: b = *p; b = a;

Pointers – Example Print the address and value of a variable: #include <stdio.h> int main() { int i = 7; int *p = &i; printf (" Value of i: %d\n", *p); printf ("Location of i: %p\n", p); return 0; }

Pointers – Example The output of the program in our system: The actual location of a variable in memory is system-dependent The variable p is of type int *, and its initial value is &i The operator * dereferences p Value of i: 7 Location of i: 0xbffff924

Declarations and initializations Equivalent expression Pointer Expressions Evaluating some pointer expressions: Declarations and initializations int i = 3, j = 5, *p = &i, *q = &j, *r; double x; Expression Equivalent expression Value p == &i * * &p r = &x 7 * * p / * q + 7 *(r = &j) *= *p p == (&i) *(*(&p)) r = (&x) (((7 * (*p))) / (*q)) + 7 (*(r = (&j))) *= (*p) 1 3 illegal 11 15

Pointer Expressions The third line tries to assign the address of a double variable to a pointer to int In the fourth line: The first '*' is the multiplication operator The second '*' is the dereference operator In the fifth line, r is assigned the address of j, and then another assignment multiplies *r (which now means j) by *p

Lecture Overview Arrays Pointers Call-by-reference Arrays, pointers and pointer arithmetic Dynamic Memory Allocation

Call-by-reference In C, all parameters are passed by value In other languages, it is possible to pass parameters by reference, allowing the called function to modify them The same effect can be achieved in C, using pointers as function arguments

Call-by-reference – Example Let us try to write a function that swaps the values of its two arguments: This will have no effect on a and b: void try_to_swap (int x, int y) { int tmp; tmp = x; x = y; y = tmp; } try_to_swap (a, b);

Call-by-reference – Example However, using pointers we can write the swap() function as follows: This will swap the values of a and b: void swap (int *px, int *py) { int tmp; tmp = *px; *px = *py; *py = tmp; } swap (&a, &b);

Lecture Overview Arrays Pointers Call-by-reference Arrays, pointers and pointer arithmetic Dynamic Memory Allocation

Arrays and Pointers Arrays and pointers are very similar An array name is actually a pointer to the first element in the array Similarly, a pointer can be subscripted just as if it were an array However, there is one major difference: the value of a pointer can change, while an array points to a fixed address

Arrays and Pointers Suppose that a is an array and that i is an int, then a[i] is equivalent to *(a + i) Similarly, if p is a pointer, then p[i] is equivalent to *(p + i) This means that we can use array notation with pointers, and vice versa

Arrays and Pointers – Example #include <stdio.h> #define SIZE 7 int main() { int a[SIZE] = {12, 32, 434, 43, 26, 873, 43}; int i; for (i = 0; i < SIZE; i++) printf ("%d ", a[i]); printf ("\n"); return 0; } 12 32 434 43 26 873 43

Arrays and Pointers – Example The array traversal loop in the previous example can be written in different ways: for (i = 0; i < SIZE; i++) printf ("%d ", *(a + i)); int *pa = a; for (i = 0; i < SIZE; i++) printf ("%d ", *(pa++)); for (i = 0; i < SIZE; i++) printf ("%d ", i[a]);

Pointer Arithmetic Pointer arithmetic is one of the most powerful features of C If the variable p is a pointer to a particular type, then the expression p + 1 yields the correct machine address for storing or accessing the next variable of that type Other expressions may be used, such as: p++, p + i or p += i

Pointer Arithmetic – Example #include <stdio.h> int main() { double a[2], *p, *q; p = a; /* points to base of array */ q = p + 1; /* equivalent to q = &a[1]; */ printf ("%d\n", q - p); printf ("%d\n", (int)q - (int)p); return 0; } 1 8

Pointer Arithmetic – Example The previous output assumes that a double is stored in 8 bytes p points to a double and q points to the next double, therefore the difference in array elements is 1 However, the difference in actual memory locations is 8

Arrays as Function Arguments In a function definition, a parameter that is declared as an array is actually a pointer For convenience, bracket notation is also allowed, but the two are equivalent Inside the function, the parameter may be treated as a pointer, as an array, or alternately as both

Arrays as Function Arguments – Example The function header may be replaced with: /* n is the size of a[]. */ double sum (double a[], int n) { int i; double sum = 0.0; for (i = 0; i < n; ++i) sum += a[i]; return sum; } double sum (double *a, int n)

Lecture Overview Arrays Pointers Call-by-reference Arrays, pointers and pointer arithmetic Dynamic Memory Allocation

Dynamic Memory Allocation Until now we have seen two ways of allocating memory: At compile time, for static and global variables During run-time, for automatic (local) variables Both types require knowing the amount of memory that should be allocated beforehand (when the program is written)

Dynamic Memory Allocation Sometimes there is a need to explicitly tell the system to allocate memory during the program's operation This is called dynamic allocation Memory is allocated using the library function malloc(), defined in stdlib.h Other functions exist, such as calloc() or realloc() but we will not discuss them here

Dynamic Memory Allocation Dynamic allocation is used to create space for arrays, structures and unions The function malloc() takes a single argument of type size_t, and returns: NULL, if the required amount of memory cannot be allocated, or: A pointer of type void *, which points to a memory block of the requested size

Dynamic Memory Allocation The memory allocated by malloc() is not initialized to any value Space that was dynamically allocated is not returned to the system upon function exit The space must be freed explicitly by the programmer, through a call to free(ptr) ptr is a pointer that points to a block of space previously allocated by malloc()

Dynamic Memory Allocation – Example 1 #include <stdio.h> int main() { float arr[20]; arr[0] = 2; printf ("Value of first cell: %f\n", *arr); arr[7] = 17; printf ("Value of 8th cell: %f\n", *(arr + 7)); return 0; }

Dynamic Memory Allocation – Example 1 #include <stdio.h> int main() { float *arr; arr[0] = 2; printf ("Value of first cell: %f\n", *arr); arr[7] = 17; printf ("Value of 8th cell: %f\n", *(arr + 7)); return 0; } Wrong!

Dynamic Memory Allocation – Example 1 #include <stdio.h> int main() { float *arr = (float *)malloc (20 * sizeof (float)); arr[0] = 2; printf ("Value of first cell: %f\n", *arr); arr[7] = 17; printf ("Value of 8th cell: %f\n", *(arr + 7)); free (arr); return 0; } OK

Dynamic Memory Allocation – Example 2 int main() { int *grades, num_grades; int i, sum = 0; printf ("Please enter number of grades: "); scanf ("%d", &num_grades); grades = (int *)malloc (num_grades * sizeof (int)); printf ("Please enter grades: "); for (i = 0; i < num_grades; i++) scanf ("%d", &grades[i]); for (i = 0; i < num_grades; i++) { printf ("Grade %d: \t%d\n", i + 1, grades[i]); sum += grades[i]; } free (grades); printf ("Average: \t%d\n", sum / num_grades);

Dynamic Memory Allocation – Example 2 The output of the previous program: Please enter number of grades: 5 Please enter grades: 10 5 18 19 8 Grade 1: 10 Grade 2: 5 Grade 3: 18 Grade 4: 19 Grade 5: 8 Average: 12

Dynamic Memory Allocation – Example 2 When calling malloc() we need to take into account: The number of elements required The size of each element (using sizeof()) The type of the pointer returned by malloc() is void *, and therefore we need to cast it into the required type