Applications of Pointers I: Memory Management, Dynamic Memory

(ポインタの応用 II: メモリ操作、動的メモリ)

Computing Practice I

10th lecture, June 14, 2018

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

Martin J. Dürst

AGU

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

 

Today's Schedule

 

ミニテスト

 

Homework from Last Week

Collection of 09C2 memory layout

 

Results of Previous Exercises

09A1 09A2 09A3 09B1 09C1 09C2
100 points 80 76 76 48 47 55
60 points 19 23 23 48 47 38
errors - - - 3 3 2
not submitted 1 1 1 1 3 5

 

Memory Layout of Arrays (09C2)

 

Summary of Last Lecture

 

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

 

Equations for Pointers

 

Example of Using Equations

Simplifying &array[i]:
&(array[i])
&(*(array+i))
&*(array+i)
(array+i)
array+i

 

Conversion from Array Notation to Pointer Notation: Start

int countUpper (char string[])
{
int i;
int count = 0;
int length = strlen(string);

for (i=0; i<length; i++)
if (isupper(string[i]))
count++;
return count;
}

 

Conversion from Array Notation to Pointer Notation: Goal

int countUpper (char *string)
{
int count = 0;
while (*string)
if (isupper(*string++))
count++;
return count;
}

 

Properties of Pointer Notation and Array Notation

Attention: If there are no specific instructions, programs can be submitted either using array notation or pointer notation (or a mixture; please care about readability)

 

Exercise 10B1: Vocabulary Check

Stack:
Memory area where local variables (and function arguments, return addresses, and stack pointers) are laid out for each function invocation
Heap:
Memory area for dynamic memory
Global variable:
Variable declared outside of a function
Local variable:
Variable declared inside a function
static variable (declared inside a function):
Variable that persists between function invocations

 

Memory Layout

The memory area where each variable is allocated differs for different kinds of variables.

Typical example:


FFFFFFFF

Stack: Local variables, function arguments, other data necessary for function call

↓↓↓↓↓↓↓↓ grows downwards, shrinks upwards after usage ↑↑↑↑↑↑↑↑

 
Empty area
 

↑ ↑ ↑ ↑ ↑ ↑ grows upwards, usually does not shrink ↑ ↑ ↑ ↑ ↑ ↑

Heap: Memory needed at arbitrary points during program execution

Un-initialized global variables without

Initialized global variables

The actual program (functions,...)
00000000

 

Usage of Heap

 

Dynamic Memory Patterns

 

Explanation of Memory Allocation Pattern

if (!(              // test whether there is enough memory
my_array = // assign address of new memory region to pointer
(int *) // cast pointer to desired type
malloc( // call allocation function
sizeof(int) // size of a single element of my_array
* asize) // multiplied by the number of elements
))
printf("Not enough memory!\n") // error message
, // comma (sequence) operator
exit(1); // force end of program execution

 

Dynamic Memory Functions

 

Dynamic Memory: Cautions

 

void *

 

NULL Pointer

 

演習問題について

 

次回の準備

 

Glossary

dynamic memory
動的メモリ
pattern
ひな形、模型、定石
memory allocation
メモリの確保
comma operator (sequence operator)
コンマ演算子 (順序演算子)
garbage collection
ゴミ集め