Applications of Pointers II: References, Indirection

(ポインタの応用 I: 参照、間接)

Computing Practice I

11th lecture, June 21, 2018

http://www.sw.it.aoyama.ac.jp/2018/CP1/lecture11.html

Martin J. Dürst

AGU

© 2005-18 Martin J. Dürst 青山学院大学

Today's Schedule

 

ミニテスト

 

Summary of Last Lecture

 

Results of Previous Exercises

10A1 10B2 10C1 10C2
100 points 95 56 28 -
60 points 4 43 61 9
partial - - - 12
errors 1 1 10 53
not submitted - - 26

 

Main Points for Problem 10C2

 

Usages of Pointers

  1. Low-level address manipulation (device allocation,...)
  2. Speedup of array processing
  3. Dynamic memory management
  4. Reference (passing of arguments to a function by reference,...)
  5. Indirection

  

References in Natural Language

 

Examples of Call by Value and Call by Reference

Call by value:

  1. Directly pass the thing to be fixed (Ex: Shoe repair)
  2. Write value on a piece of paper, pass the paper (can be thrown away)

Call by reference:

  1. Indicate the address of the thing to be fixed (Ex: Water pipe repair)
  2. Put value in a box, pass the address of the box

 

Call by Reference

Pattern for Call by Reference (Except Arrays)

 

Application of Call by Reference: Multiple Return Values

Problem: Create a function that returns both n2 and n3

Solution: Pass uninitialized variables by reference:

void square_cube(int n, int *pn2, int *pn3)
{
*pn2 = n * n;
*pn3 = *pn2 * n;
}

Caller side:

int a=5, a2, a3;
square_cube(a, &a2, &a3);
printf("a=%d, a*a=%d, a*a*a=%d\n", a, a2, a3);

 

Pattern for Call by Reference (Arrays)

 

How to Pass Arrays by Value

  1. Call by reference, do not change in callee
  2. As in (1), but add const qualifiers to declare and assure there are no changes
  3. Copy data before passing it
  4. Wrap the array in a struct, and pass the struct (which is passed by value)

 

Calling Conventions Overview

everything except arrays arrays
call by value automatic copy before calling, wrap in struct, use const
call by reference use pointers automatic (except length)

 

Indirection

Famous quote: "All problems in computer science can be solved by another level of indirection"

Examples of indirection:

In C, indirection is realized using pointers

 

Applications of Indirection

When ordering data, actually moving the data around means:

Indirection can solve all these problems (e.g. 11C3)

 

Functions as Arguments

(pointers to) functions can be passed as arguments to other functions

Typical example: Execute some 'work' according to some pattern, where the 'work' details are not fixed

Application example: Execute some work (e.g. clean blackbords, clean room, switch off lights, check air quality, check fire alarms...) in all lecture halls of the Sagamihara campus

Solution:

 

How to Implement Sorting

 

Introducing Comparison Functions

 

Functions as Arguments in Other Programming Languages

 

Example of Difference between Languages

Ruby: students.sort { |a, b| a.number <=> b.number }

Or even easier: students.sort_by { |a| a.number }

Shorthand: students.sort_by &:number

C (from 11C3):

int compareNumbers (const void* a, const void* b)
{
Student *studentPa = *((Student **) a);
Student *studentPb = *((Student **) b);

return strcmp (studentPa->studentNumber,
studentPb->studentNumber);
}
... qsort(byNumber, studentCount, sizeof(Student*), compareNumbers);

 

今日の演習

穴埋めや追加が多いので、インデントに注意

 

次回の準備

 

Glossary

call (pass) by reference
参照渡し
array index
配列の指数
callee side
(呼ばれた) 関数側
caller side
呼び出し側
sort(ing)
ソート、整列
(sort) criterion
(整列の) 基準
bubble sort
バブル整列
insertion sort
挿入整列
selection sort
選択整列
quicksort
クイックソート
comparison function
比較関数