3 Sep 2002 redi   » (Master)

Passed the technical test thing I took last week for a job, so I'm in for an interview. I'm quite surprised, as although the test was very easy, I spent too long on the C++ section crafting the perfect answers and stupidly ran out of time and didn't write anything for some other chunks of it (and wrote complete rubbish for the database bit as I rushed to scribble something down.) Nevermind, I'm the only person to get through to the interview. Which is nice.

mathieu:

You're modifying a string literal. String literals in C have type (non-const) char*, but are non-modifiable, doing so invokes the dreaded undefined behaviour, so a coredump isn't too bad, you should think yourself lucky you didn't rupture a hole in the fabric of the universe. In C++ string literals are const, but as Stroustrup The Wise explains:

The type of a string literal is "array of the appropriate number of const characters," so "Bohr" is of type const char[5].
A string literal can be assigned to a char*. This is allowed because in previous definitions of C and C++, the type of a string literal was char*. Allowing this assignment of a string literal to a char* ensures that millions of lines of C and C++ remain valid. It is, however, an error to try to modify a string literal through such a pointer:
void f()
{
    char* p = "Plato";
    p[4] = 'e';  // error: assigment to const; result is undefined
}
This kind of error cannot in general be caught until run-time, and implementations differ in their enforcement of this rule.
Stroustrup, C++PL3E, ยง5.2.2

amars:

"Programming is one of the most difficult branches of applied mathematics; the poorer mathematicians had better remain pure mathematicians."
Edsger Dijkstra
I'm not sure I agree, but who am I to argue with Dijkstra?

Latest blog entries     Older blog entries

New Advogato Features

New HTML Parser: The long-awaited libxml2 based HTML parser code is live. It needs further work but already handles most markup better than the original parser.

Keep up with the latest Advogato features by reading the Advogato status blog.

If you're a C programmer with some spare time, take a look at the mod_virgule project page and help us with one of the tasks on the ToDo list!