Skip to main content

CSC 282.01, Class 11: Understanding your code

Overview

  • Preliminaries
    • Notes and news
    • Upcoming work
    • Questions
  • The joy of segfaults
  • Other memory problems
  • Tracing errors with gdb
  • Checking memory usage with valgrind

News / Etc.

  • Happy 20/4. Please be moderate.

Upcoming work

  • Probably none, given how things have been going.

Good things to do

  • Be moderate.
  • Go to convo.
  • Go to tonight’s talk on digital humanities.
  • CS table next Tuesday.
  • Contra dance tomorrow night
  • And more.

Questions

Can you explain the cs282 thing you type?

  • If you use bash, you have two important files that are usually run at startup. .bashrc and .bash_profile
  • Because they start with a period, you won’t see them when you type ls unless you type ls -a.
  • Your bashrc can contain all sorts of instructions
    • Settings for important variables, like CC, or PATH, or …
    • aliases - words you type to do more complex things
    • `alias word=”commands” - defines the alias
    • We can also put function definitions in our bashrc. Then our aliased commands can have parameters.
    • function alex() { pushd /home/mitchell17/$1; ls -alF }

What is the difference between cd and pushd.

  • push normally pushes something onto a stack. pushd pushes your current directory before going to another directory
  • popd lets you go back to where you were
  • dirs lists all the directories on your stack

How do I set my prompt to something awesome?

  • export PS1="something awesome "
  • http://www.gnu.org/software/bash/manual/bash.html#Controlling-the-Prompt
  • function servant { export PS1="$1 says: What is your command? "; }

An example

What does this program do?

/**
 * example.c
 *   An example for our discussion of GDB and valgrind.
 */

// +---------+---------------------------------------------------------
// | Headers |
// +---------+

#include <assert.h>
#include <stdio.h>

// +---------+---------------------------------------------------------
// | Helpers |
// +---------+

/**
 * A function that returns an array on the stack.
 */
void
vals (int *result[])
{
  int values[10];
  *result = values;
} // vals

/**
 * A function that fills in some values in an array.
 */
void
f (int n)
{
  int i;
  int values[10];
  for (i = 0; i < 10; i++)
    {
      values[i] = n;
      assert(values[i] == n);
    } // for
} // f

// +------+------------------------------------------------------------
// | Main |
// +------+

int
main (void)
{
  int *stuff;
  vals (&stuff);
  stuff[0] = 42;
  f (5);
  assert (stuff[0] == 42);
  return 0;
} // main

Whoops! We allocated space on the stack, rather than on the heap.

Detour: Why foobar?

  • Observation: At some point, folks at MIT started using foo in the programs they wrote.
    • Others soon started using bar
    • WW II military acronym, FUBAR, Fouled up beyond all recognition
  • Observation 2: The Smokey Stover comic strip in the 1920’s also used the term foo, as did an early Daffy Duck cartoon.

The joy of segfaults

Why might your program segfault?

  • When you reference something that doesn’t exist.
    • perpetual_motion_machine = 3
    • Referencing a non-existent field is a compiler error mac.field = 2.
  • Go too far in an array.
    • Files may behave differently
  • You attempt to write your own linked lists, and you are not yet competent.
  • malloc and free
    • Assign to pointer that has not been allocated

What do you do when it does?

  • Find someone who knows C better than I do and ask for help.
  • Rely on my knowledge of likely causes of errors and think carefully about any places in the code.
  • Curse myself for celebrating 4/20 in Sam’s class too often.

Other memory problems

What memory issues might crop up that don’t cause (immediate) segfaults?

  • Go too far in an array.

How do you find those issues?

Tracing errors with gdb

Checking memory usage with valgrind