Tuesday, October 20, 2009

Fault Fixed

The segmentation fault was due to a stupid hard-coded directory I had in my code. I was reading out the random data points to make sure they were in the same region as the data, but I forgot that I had put in the exact directory. When I uploaded it to riemann and tried to run it, the code was trying to write files to a directory that didn't exist. Silly silly me.

1 comment:

  1. Last tip. :) Code defensively. For example, when you open a file, or try to write to a file, immediately following put in a line that checks that the file actually exists or can be written to. If not, write out a little message to that effect. It may seem like a "duh" thing to do as you write the code, but you will save yourself (and others who may run your code) tons of heartache. As you saw in this example, the computer flies by these types of errors and blows up in inexplicable ways. It will save you time and impress people who look at your code. If anyone mocks you for doing it, you are totally free to put the smack down on them (p. 46 in K&C).

    OK, really last tip. Learn the assert() function, available where all fine programming languages are sold. The assert function is like a sanity check that evaluates any statement you give it to be true or false, and dies if it's false. For example (in pseudocode):

    file = open("my_file.txt")
    assert(file.exists(), "Hey, the file you tried to open (%s) doesn't exist!" % file)

    That's rather pythony, but as I say, it's available in C. The C version doesn't have an error message, but the principle applies. Another example:

    float f = some_complicated_calc();
    assert( !isnan(f) ); // die if the result is not a valid number

    Here's a random page I found:

    http://ptolemy.eecs.berkeley.edu/~johnr/tutorials/assertions.html

    And one more:

    http://bit.ly/33x2Uf

    ReplyDelete