Hide menu

Lab00: Introduction to C programming

(The monospace font size defaults to 12 on IDA's firefox installation. If the code on this page is difficult to read change your firefox settings in Edit->Preferences->Content->Advanced (Swedish: Redigera->Inställningar->Innehåll->Avancerat).)

1. Hello, world. [10 min]

Everytime you start to work with a new programming language, it is tradition to make the "Hello, world" program. Create it, compile it, and run it.

2. debugthis

First try to understand the program below and spot the bug, and fix it if you can.

Run the program in ddd to find the row where the segmentation fault occurs. You must first compile it with gcc -g for this to work.

#include <stdio.h>

int main(int argcchar ** argv)
{
  char str[] = "sihtgubed";
  char *stri = &str[8];
  char *buf[9];
  char **bufi, **bufend;
  bufi = buf;
  bufend = &buf[9];

  while (bufi != bufend){
    *bufi = stri;
    bufi++;
    stri--;
  }

  while (bufi != buf){
    *(*bufi) -= 32;
    bufi--;
  }

  while (bufi != bufend){
    printf("%c", **bufi);
    bufi++;
  }
}

N.B. This is a silly and not very pretty program. Hint: Check an ASCII table (for example by typing man ascii in the console).

You need to demonstrate this part (2) for your assistant!

3. Create a linked list of integers.

Create a linked list with the following functions:

  void append(struct list_item *firstint x); /* puts x at the end of the list */
  void prepend(struct list_item *firstint x); /* puts x at the beginning of the list */
  void print(struct list_item *first);  /* prints all elements in the list */
  /* input_sorted: find the first element in the list larger than x
     and input x right before that element */

  void input_sorted(struct list_item *firstint x);
  void destroy(struct list_item *first); /* free everything dynamically allocated */

Hint1: You need to use malloc (and free of course) and a struct list_item that contains a pointer to the same struct:
  struct list_item {
    int value;
    struct list_item * next;
  };

Hint2: Implementing the list will be easier if you make first be a static head element that does not contain any real data. (Because there are fewer exceptions if you know first is never NULL.) Your main can start like this:
  int mainint argcchar ** argv)
  {
    struct list_item root;
    root.value = -1/* This value is always ignored */
    root.next = NULL;

You need to demonstrate this part (3) for your assistant!

4. Browsing the code. [10 min]

Using tags with emacs. Install pintos using the instructions from Lab0. Change to your new ${HOME}/pintos/src and run:
    
      gmake TAGS
This created an index of structs and functions in a new file named TAGS. Now let's find where the pintos semaphore is. Start emacs and press meta-. (hold meta and press dot). Type the keyword you wish to look for, "semaphore" in this case, and press enter. Press enter again to confirm the path to your TAGS file, and there you are!

Sometimes when you want to know where a file is in the hierarchy this command is useful:

     find ${HOME}/pintos/src |grep filename
Try find the file list.c. Open it to see a more complicated linked list implementation than the one you just created.

Now imagine that you want to find all files where this list is used, for this you can use the following command:

     find ${HOME}/pintos/src -type f -print |xargs grep list_init

Page responsible: Christoph W Kessler
Last updated: 2014-01-08