Skip to content

Mandatory task 2 ING2504 Fall 2025

When you have completed all three parts of this mandatory task, show/demonstrate your solution to Erik

Part 1: Pointers

(these tasks are copied from the text book chapter 14)

  1. First, write a simple program called null.c that creates a pointer to an integer, sets it to NULL, and then tries to dereference it. Compile this into an executable called null. What happens when you run this program?

  2. Next, compile this program with symbol information included (gcc -g). Doing so let’s put more information into the executable, enabling the debugger to access more useful information about variable names and the like. Run the program under the debugger by typing gdb null and then, once gdb is running, type run. What does gdb show you? Now compile this program without the -g and run it again with gdb, what kind of information is missing now?

Part 2: Address translation

You must see the lecture kapittel 6 del 2 OSTEP ORG kapittel 20 before you do this task

Write a C-program that declares a local ("automatic" according to the textbook) variable and prints the variable's virtual address (see chapter 14 in the textbook if you are unsure about how to do this).

  1. Find out what the page size is on your operating system with getconf PAGESIZE

  2. Split the virtual address your program printed into the virtual page number (VPN) and the offset. Remember that you are on a 64-bit architecture so the VPN is actually split in four levels and the address translation works as described in the X86-64-figure in "6.2.1 Multi-level PT" in the compendium. What is the index into the "Page table" (the last level in the multi-level page table) ?

# Remember that Google can convert numbers for you e.g.
# you can ask Google this:
0x7eb to binary
0b101 to decimal

Part 3: Memory allocation

Consider the following C-program mem1.c:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define S 1024*256

int main(void) {
   int size = S;
   printf("Creating array with %d elements\n",size);
   int *array = malloc(size * sizeof(int));
   pause();
}

Compile mem1.c (let the executable file be named mem1) and execute it in the background. Write down the values in the columns VIRT and RES when you execute the command top -n 1 -p$(pgrep mem1).

Now consider the following C-program mem2.c:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define S 1024*256

int main(void) {
   int i,size = S;
   printf("Creating array with %d elements\n",size);
   int *array = malloc(size * sizeof(int));
   for(i=0;i < size;i++) {
      array[i] = i;
   }
   pause();
}

Compile mem2.c (let the executable file be named mem2) and execute it in the background. Write down the values in the columns VIRT and RES when you execute the command top -n 1 -p$(pgrep mem2).

  1. Explain the difference in the values VIRT (virtual memory) and RES (physical memory). Why is there a difference? How big is the difference, and why is it exactly this number?