Applications of Pointers I: Memory Management, Dynamic Memory

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

Computing Practice I

10th lecture, June 14, 2018

Martin J. Dürst


© 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]:


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]))
return count;


Conversion from Array Notation to Pointer Notation: Goal

int countUpper (char *string)
int count = 0;
while (*string)
if (isupper(*string++))
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

Memory area where local variables (and function arguments, return addresses, and stack pointers) are laid out for each function invocation
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:


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,...)


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







dynamic memory
memory allocation
comma operator (sequence operator)
コンマ演算子 (順序演算子)
garbage collection