"Alexei A. Frounze" <> writes:
> "Keith Thompson" <kst-> wrote in message
> news:...
>> "Alexei A. Frounze" <> writes:
>> > "John Bode" <> wrote in message
>> > news: oups.com...
>> > ...
>> >> The quickest way to find out what pointer sizes are on your particular
>> >> implementation:
>> >>
>> >> printf("sizeof(char*): %lu\n", (unsigned long) sizeof(char*));
>> >> printf("sizeof(int*): %lu\n", (unsigned long) sizeof(int*));
>> >> printf("sizeof(short*): %lu\n", (unsigned long) sizeof(short*));
>> >
>> > And chances are that the tree will print the same number. And that's
>> > practically on every system, if not on all.
>>
>> Probably, but there are valid reasons why char* and void* might be
>> bigger,
>
> Bigger than what and why?
Bigger than int* ...
>> or at least have a different representation, than int*.
... as I think I made clear in the same sentence.
The example I've brought up here several times in the past is the C
implementation on Cray vector machines. The smallest directly
addressible unit of memory is 64 bits, so the obvious value for
CHAR_BIT would be 64 -- except that the system runs a version of Unix,
and there's a need to be able to represent strings of 8-bit characters
and share information with other systems, so the implementation uses
CHAR_BIT==8. An int* (pointing to a 64-bit int) is simply a native
64-bit machine address, but a char* pointer needs to specify which
8-bit byte within the word it points to. This is done (purely in
software) by storing a 3-bit offset in the (otherwise unused)
high-order 3 bits of the pointer.
If the machine used all 64 bits of a word pointer, a byte pointer
would have to store the offset somewhere else -- i.e., it would have
to be bigger than 64 bits.
> Like what? A few lowest signigicant bits being 0 due to alignment?
No, see above.
[...]
>> But the most important point is that, most of the time, none of this
>> should matter.
>
> True.
>
>> You can write code that doesn't *care* whether
>> different pointer types have different sizes, and that will work
>> properly whether they differ or not. Just don't depend on any
>> assumptions that aren't guaranteed by the standard. It's often
>> (usually?) *easier* to write portable code than to write code that
>> depends on system-specific sizes.
>
> I'm not sure of this... The practice seem to show lots of examples of the
> directly opposite. Many C programmers, the beginners (including me some time
> ago), do not know C and the standard well for obvious reasons -- it's new to
> them and it's different from whatever they had learned or used before. If
> they happen to know a bit about the CPU for which they write their C code,
> they're more likely to not use sizeof for the basic types, not care about
> the alignment when placing objects in memory, ignore various warnings about
> prototypes, conversion/promotion, etc etc. I did it all myself. Now I don't
> do it because I'm fortunate enough to have seen (correct my English here if
> need be) various platforms, not just the x86. And this taught me how to do
> things better. Those who are condemned to x86 and ignorance about C and its
> standard, are condemned to making bad code. And it's not easier to write
> portable code in that case, it's simply impossible.
Perhaps the problem is in the way beginners learn C. If you learn the
language without reference to the details of the underlying machine,
you'll presumably develop the habit of writing portable code because
you don't know how not to. Learning the details of particular
implementations *later* can let you write non-portable code when
necessary, while knowing the difference. (I'm using "you" as a verbal
shorthand; I'm not referring to you personally.)
--
Keith Thompson (The_Other_Keith)
kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.