[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Sv: Sv: Sv: Sv: Sv: PenguinFile

>>>> char* pThisIsAString = "whatever";
>>>> WriteToDisk(pThisIsAString);
>>>> WriteToDisk(pThisIsaString);
>>>> Should this compile? I would consider it a bug if it did...
>>>Why shouldn´t it? pThisIsAString is a completely valid C-style string.
>>>And, depending on the implementation, you eventually *can* modify it.
>>Actually there are two bugs on those two lines. pThisIsAString is an
>>pointer, but "whatever" yields a const pointer. In the third line,
>Bjarne Stroustrup, "The C++ Programming Language, 3rd Ed.", Section 5.2.2:
>"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 the assignment of a string literal to a char* ensures that
>millions of lines of C and C++ remain valid. Ti is, however, an error to
>try to modify a string literal through such a pointer:

I actually wasn't aware of that. I think it should be an explicit compiler
option to allow this, though.

>>pThisIsAString is spelled with the wrong case for the a.
>Emails always contain typos.
This one actually was on porpuse. If the compiler itself figured out that
the bug above (which turns out not nessecarily is a bug) was "ok", then I
see no reason it shouldn't also figure out that the wrong case here is an
"ok" bug since it can figure out what variable the programmer really meant.
Of course, this isn't desired behavior in any way.

>>I would have chosen whatever everybody else used.
>Bad luck. Back then there were some teams independently designing CPUs at
>the same time - so there wasn't "everybody else".
Hmm... Then I would call around.