What's the best way to minimize the need of run time checks?

BartC bc at freeuk.com
Mon Aug 29 10:13:02 EDT 2016


On 29/08/2016 15:00, Grant Edwards wrote:
> On 2016-08-29, Chris Angelico <rosuav at gmail.com> wrote:
>> On Mon, Aug 29, 2016 at 10:13 PM, BartC <bc at freeuk.com> wrote:
>>> In C, you can write this:
>>>
>>>  int x;
>>>
>>>  x = 5;
>>>  x = "hello";
>>>
>>> With certain compilers (eg. gcc) you only get a warning. (And since
>>> I don't show warnings to avoid inundation, that seems to compile
>>> fine for me!)
>
> If you're ignoring compiler warnings, you're asking for problems and
> deserve whatever trouble you get.

(It's my IDE that's not displaying the warnings because it can use any C 
compiler and some of them really will swamp the output with harmless 
warnings that often cannot be turned off (about unused labels for example).

The situation is a little unusual as normally the C being compiled has 
been machine-generated so the actual source has already been mostly 
verified. (And in that source language, assigning "hello" directly to x 
is an error as a cast is mandatory.)

>> That's because strings, in C, are really pointers-to-char, and for
>> hysterical raisins, pointers can be assigned to integers with just a
>> warning. (Good code should have an explicit cast here.)
>>
>> You probably should have warnings enabled.

(Possibly they should be displayed when compiling 'straight' C. But then 
I wouldn't be able to 'assign a string to an integer' without any 
apparent problem!)

-- 
Bartc



More information about the Python-list mailing list