Re: Code Comprehension
- From: Pascal Bourguignon <pjb@xxxxxxxxxxxxxxxxx>
- Date: Sun, 27 Aug 2006 05:04:09 +0200
"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.
.
- Follow-Ups:
- Re: Code Comprehension
- From: Logan Shaw
- Re: Code Comprehension
- From: Ben Pfaff
- Re: Code Comprehension
- References:
- Code Comprehension
- From: jj
- Re: Code Comprehension
- From: Pascal Bourguignon
- Re: Code Comprehension
- From: Logan Shaw
- Re: Code Comprehension
- From: Pascal Bourguignon
- Re: Code Comprehension
- From: Logan Shaw
- Re: Code Comprehension
- From: Rob Thorpe
- Re: Code Comprehension
- From: Pascal Bourguignon
- Re: Code Comprehension
- From: Phlip
- Code Comprehension
- Prev by Date: Re: Hashtable question
- Next by Date: Sorting question
- Previous by thread: Re: Code Comprehension
- Next by thread: Re: Code Comprehension
- Index(es):
Relevant Pages
|