sizeof( int ) != sizeof( void * )

B

blufox

I read somewhere that standard Ansi C on 32 bit architecures assumes a
lot of things which lead to poor code.

For example
sizeof( int ) = sizeof( void * )
sizeof( long ) = sizeof( int )

Why is this poor code?
Am i missing something?

Thank you
 
R

Richard Heathfield

blufox said:
I read somewhere that standard Ansi C on 32 bit architecures assumes a
lot of things which lead to poor code.

"Ansi C on 32 bit architectures" doesn't really mean a great deal. If you
mean "programs supposedly written in ANSI C but relying in practice on
non-portable assumptions that happen to be true on the development
platform", then that is indeed meaningful.
For example
sizeof( int ) = sizeof( void * )
sizeof( long ) = sizeof( int )

Why is this poor code?

If sizeof(int) is not the same as sizeof(void *), then it's a bad idea to
assume it is. And of course the same applies to your second example.

Even if these assumptions happen to be true on the machine we are working on
today, they might not be true on the machine the boss suddenly decides to
buy tomorrow. And since he's spent all that money on supposedly portable C
programs, he's going to want to run those programs on that machine,
tomorrow. Writing the programs correctly in the first place is far easier
than explaining to your boss why you didn't.
 
J

Joe Smith

"Richard Heathfield"
blufox said:
"Ansi C on 32 bit architectures" doesn't really mean a great deal. If you
mean "programs supposedly written in ANSI C but relying in practice on
non-portable assumptions that happen to be true on the development
platform", then that is indeed meaningful.
ANSI C on a 32 bit architecture probably means less to Richard than me, as I
struggle with producing something ISO with an OS that emulates 32 bit
processing on a 64 bit chip.
In the stated environment and with vendor-dependent implementation, that
would mean for me in both statements:
2 = 4
without the necessary semicolon to notify your compiler that you're making a
different error than you think.
If sizeof(int) is not the same as sizeof(void *), then it's a bad idea to
assume it is. And of course the same applies to your second example.

Even if these assumptions happen to be true on the machine we are working
on
today, they might not be true on the machine the boss suddenly decides to
buy tomorrow. And since he's spent all that money on supposedly portable C
programs, he's going to want to run those programs on that machine,
tomorrow. Writing the programs correctly in the first place is far easier
than explaining to your boss why you didn't.

This last statement is true in a better world. joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top