Welcome to CS 241!

Lab 1 - Introduction to C

What’s Lab Gonna Be Like?

Rules

  • The first 20ish minutes are going to be dedicated going over a review worksheet
  • Then you can work on your labs the rest of the time
  • You get credit by making an honest effort in the worksheet
  • Email GA if you need an exemption

Lab Attendance

  • 10 Minute Rule, if you aren’t here in the first 10 minutes of class, you don’t get credit
  • Part of your grade in this class relies on you attending labs. Toward the end of every lab, we will ask you to swipe out. You may only leave early if you show that you have finished the lab to your lab attendant or if the lab attendant calls the time.

Different Lab Sections

Due to seating limitations, you are required to go to the lab section you signed up for. If you wish to go to any other lab section, you may:

  • Go to section ADH at 7:30pm-8:50pm in 0218. This section only has the least registered students, and thus there will be room to accommodate you on a first come first served basis.

  • Get permission from the TA of another section to go to their section, provided
    • you will be working on your laptop in the room, or
    • there is seating available where registered students get priority.

If choosing the latter option, you must email the TA in charge of your assigned lab and the TA for the lab to which you are going.

Remember that lab attendance is required; per course policy, missing 3 or more lab sections will result in a failing grade.

Virtual Machines

ssh **NETID**@sp17-cs241-**xxx**.cs.illinois.edu

Note

You are going to need to be on campus VPN for this to work. If you want to make it work at home, make sure to log in to campus VPN then try ssh’ing. Look at the development guide!

Late Adds

If you added in the past 24 hours you should be getting an email soon about your VM from there you can get working. If you cannot find the assignments

Worksheets

What are they?

You all should have gotten worksheets when you came in, these will mainly be review/expansion from the topics that you learn in lecture so shouldn’t be anything new.

Note: There is a typo in this worksheet (sorry we make mistakes too!) In a bit about bits, the line

(short | 0xCC) -> (shifted | 0xCC).

Time to work

Start working on the sheet, we will be around to ask some questions. We will see how long people are taking at around 15ish minutes and then go through those parts of the worksheets (the rest will be a challenge for to complete).

Informational Slides

Question 1

int a = 0;
size_t a_size = sizeof(a++);
printf("size: %zu, a: %d\n", a_size, a);

Question 2

#define swap(a, b) temp = a; \
    a = b; \
    b = temp;
    
void selection_sort(int* a, size_t len){
    size_t temp = len - 1;
    for(size_t i = 0; i < temp; ++i){
        size_t min_index = i;
        for(size_t j = i+1; j < len; ++i){
            if(a[j] < a[i]) min_index = j;
        }
        if(i != min_index) 
            swap(a[i], a[min_index]);     
    }
}

Question 3

short mystery_bits(short input){
    short max_set = ~0;
    short masked = input & (0xFF00 ^ max_set);
    short shifted = masked << 16;
    short ret = (shifted | 0xCC);
    return ret;
}

Question 4

void positive_under_ten(int input){
    if(0 < input < 10){
        printf("Input is in the range\n");
    }else{
        printf("Input is not in the range\n");
    }
}

Question 5

int print_error(int err_num){
    switch(err_num){
    case ENOENT:
        printf("No such file or entry\n");
    case EINTR:
        printf("Interrupted\n");
    default:
        break;
    }
}

Today’s lab: Debugging

Valgrind!

Usage

Given a program test arg1 arg2. valgrind --leak-check=type command type = <no|summary|yes|full> valgrind --show-leak-kinds=all ...

Leak Types

  1. Memory block: a block of allocated, not-freed memory.
  2. Definitely Lost: No pointer to a memory block.
  3. Still Reachable: Have pointer to a memory block.
  4. Indirectly Lost: Memory blocks pointing to a memory block is lost
  5. Possibly Lost: Have a (moved) pointer pointing to a memory block

GDB

A lot of you are afraid, don’t be!

Commands intro

  • layout src gives you a text based gui
  • break <file:line|function> [if condition] You can make powerful breakpoints by giving a line, but only under certain circumstances.
  • watch (type *)0xADDRESS Watches an address for a read or write and tells you when it has changed – useful for when you don’t know where the bug is.
  • backtrace, bt see what functions you are in
  • up, down goes up and down a stack frame

Demo!

Authors: Steve & Bhuvan