OPERATING SYSTEMS LABORATORY II - C Review I

IMPORTANT: If gcc command does not work on your own system, it should be installed first. Below are the commands for installing gcc on various Linux distributions; you just have to run the command (assuming you have a working internet connection) for your OWN distribution ONLY ONCE.
  1. Pardus: sudo pisi it gcc
  2. Ubuntu, Kubuntu, Debian, etc: sudo apt-get install build-essential
  3. Fedora, Scientific Linux, etc: su -c ``yum install gcc''
Each of these commands will require the user to enter password.
  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 assignment? 'ip = tmp;'
      • For other memory related functions take a look at man pages. e.g., man malloc,
        • What is the difference between malloc and calloc? (We will discuss later)
        • What is brk()? (We will discuss later)
Cem Ozdogan 2010-02-25