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

Steve D'Aprano steve+python at pearwood.info
Mon Aug 29 08:46:55 EDT 2016


On Mon, 29 Aug 2016 10:31 pm, Chris Angelico 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!)
> 
> 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.)

Let me see if I've got this straight... Bart's second assignment will
allocate a block of memory at least five bytes in size, stuff the ASCII
codes for 'h', 'e', 'l', 'l' and 'o' in that block (possibly with a null
byte at the end?) and then assign x to the address of that block.

Am I right?


That's better than my first thought, which was that it would write either

    0x6865  ('he', if int is 16 bits)

or

    0x68656c6c  ('hell', if int is 32 bits)


to x, and either discard the rest of the characters or just blindly write
them over the top of whatever variable (if any) happens to follow x in
memory.


> Getting inundated with warnings would be a major code smell.

"The low oil warning light in my car was always on, so I put some masking
tape over it so it wasn't bothering me any more."


It freaks me out something wicked when I run a major GUI application like
Firefox from the command line. Have you seen how many warnings and failed
assertions it generates? It is scary.




-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list