Re: Code Comprehension



"Phlip" <phlipcpp@xxxxxxxxx> writes:

Pascal Bourguignon wrote:

The type of 0 and the type of '\0' are both int. There
is no difference between them in types of type or value.

The type of 0, '\0', "0", struct{char a[]="zero";} are both integer too.
Don't believe me?

This experiment doesn't test the front-end of the compiler, only the
back-end of the opcodes.

0xbff02deb: 0x00000100 ; another integer

You have discovered gdb's capacity to dump a memory location. This may have
very little to do with the C 'int' in the source code.

Specifically, if your experiment does not force the output opcodes to depend
on the size of the types involved, the compiler is free to optimize things
to machine words.

Saying "because my compiler" or "debugger does it like that" is not the same
as researching language law. All kinds of optimizations (and compiler bugs)
will get in the way.

Now write assert(sizeof(int) == sizeof '0'), or similar, and see what you
get. That's a slightly more accurate way to test - but still not language
law.

More specifically, my point is that for reasons of readability,
understanding or the code, you mustn't rely on the low-level notions
provided by your tools. I know perfectly well that _in_ _C_, the type
char is a subtype of the type int. But the semantics of a character
are not the semantics of an integer. You can write two C functions,
both compiled to the exact same bit pattern, one making manifest the
higher level semantics, and another obscuring it. Well, if you prefer
to write ch==0 instead of ch=='\0', I tell you that I can as well
program in assembler! But I'm also telling you that the program would
be even more readable if you wrote: char_equal_p(ch,ascii_nul) instead!



(Writing {char_t ch;char_equal_p(ch,ascii_nul);} would even have the
advantage that it could work with unicode characters too).

--
__Pascal Bourguignon__ http://www.informatimago.com/
Kitty like plastic.
Confuses for litter box.
Don't leave tarp around.
.



Relevant Pages

  • Re: Is C99 the final C? (some suggestions)
    ... This would violate the division between preprocessor and compiler too ... much (the preprocessor would have to understand quite a lot of C semantics). ... ...This is legal C (as per the Standard), but it overflows the stack on ...
    (comp.lang.c)
  • Re: left-to-right (was In-Out Parameters for functions)
    ... >> The question is what gets lost if an evaluation order is fixed. ... What I would expect from a good compiler is factoring out the cycle ... There is no need to check program semantics very precisely. ... language may simply outlaw "pure" functions like: ...
    (comp.lang.ada)
  • Re: Glasgow haskell vs. Lispworks
    ... gotos with block structured programming and the harmfulness of goto ... How many languages even have formally-defined semantics that a compiler must ... And WRT to your dislike of Haskell: ...
    (comp.lang.functional)
  • Re: WaitForSingleObject() will not deadlock
    ... One is to hijack the semantics of volatile to disable compiler optimizations ... and otherwise let the compiler to agressive optimization. ... Agressive optimizations are the ones that work on the edge of the semantics of the ... Because the compiler can see into lock and unlock, it is able to reduce f ...
    (microsoft.public.vc.mfc)
  • Re: Glasgow haskell vs. Lispworks
    ... previous version of the compiler did" exactly as Neel did. ... You can think of "semantics" as a fancy word for the set ... If it hasn't happened with Ocaml, ...
    (comp.lang.functional)