next up previous contents
Next: OPERATING SYSTEMS LABORATORY III Up: Ceng328 Operating Systems Laboratory Previous: Exercises   Contents

OPERATING SYSTEMS LABORATORY II - C Review I

  1. Using argc and argv as command line arguments.
  2. Arrays, Pointers and Dynamic Memory Allocation
    1. Pointer: A variable that contains the address of a variable. http://siber.cankaya.edu.tr/OperatingSystems/cfiles/code6.c code6.c
      #include <stdio.h>
      int main(int argc, char *argv[])
      {
        int x = 1, y = 2, z[10];
        int *ip; /* ip is a pointer to int */
        ip = &x; /* ip now points to x */
        printf("The address of pointer 'ip' is %p \n",&ip);
        printf("The thing that pointer 'ip' contains inside is %p \n",ip);
        printf("The thing that pointer 'ip' points is %d \n",*ip);
        printf("The address of variable 'x' is %p \n",&x);
        printf("The value of variable 'x' is %d \n",x);
        y = *ip; /* y has the value of x now */
        *ip = 0; /* x is 0 now */
        ip = &z[0]; /* ip now points to z[0] */
        return 0;
      }
      
      • Analyze the code.
      • Execute the code. What is the output and why?
      • Exercise: Modify the code above that ;
        • creates two 'double' type pointers,
        • puts numbers in,
        • prints out the values inside the pointers (address info.),
        • prints out the values that pointers point.
    2. In C, there is a strong relationship between pointers and arrays, strong enough that pointers and arrays should be discussed simultaneously. The pointer version of any code will in general be faster (Why?). http://siber.cankaya.edu.tr/OperatingSystems/cfiles/code7.c code7.c
      • Analyze the code.
      • Execute the code several times. What is the output and why? Observe the changes in the addressing scheme.
    3. Dynamic Memory Allocation: Allocating memory at runtime.
      • Malloc; http://siber.cankaya.edu.tr/OperatingSystems/cfiles/code8.c code8.c
        • Analyze the code.
        • What is the function of 'malloc'.
      • Realloc; http://siber.cankaya.edu.tr/OperatingSystems/cfiles/code9.c code9.c
        • Analyze the code.
        • What is the function of 'realloc'.
        • What is happening by the assginment? 'ip = tmp;'
      • For other memory related functions take a look at manpages. e.g., man malloc,
        • What is the difference between malloc and calloc? (We will discuss later)
        • What is brk()? (We will discuss later)

next up previous contents
Next: OPERATING SYSTEMS LABORATORY III Up: Ceng328 Operating Systems Laboratory Previous: Exercises   Contents
Cem Ozdogan 2009-05-11