sizeof( int ) != sizeof( void * )

Discussion in 'C Programming' started by blufox, May 22, 2006.

  1. blufox

    blufox Guest

    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
     
    blufox, May 22, 2006
    #1
    1. Advertising

  2. 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.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, May 22, 2006
    #2
    1. Advertising

  3. blufox

    Joe Smith Guest

    "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.

    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.

    >> For example
    >> sizeof( int ) = sizeof( void * )
    >> sizeof( long ) = sizeof( int )
    >>
    >> Why is this poor code?

    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
     
    Joe Smith, May 22, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ollej Reemt
    Replies:
    7
    Views:
    600
    Jack Klein
    Apr 22, 2005
  2. Stig Brautaset

    `void **' revisited: void *pop(void **root)

    Stig Brautaset, Oct 25, 2003, in forum: C Programming
    Replies:
    15
    Views:
    836
    The Real OS/2 Guy
    Oct 28, 2003
  3. Spiros Bousbouras
    Replies:
    40
    Views:
    1,538
    Keith Thompson
    Jan 20, 2007
  4. Stargazer

    Re: sizeof(const int *) > sizeof(int *)

    Stargazer, Jun 14, 2010, in forum: C Programming
    Replies:
    14
    Views:
    689
    Ben Bacarisse
    Jul 8, 2010
  5. Ben Bacarisse

    Re: sizeof(const int *) > sizeof(int *)

    Ben Bacarisse, Jun 14, 2010, in forum: C Programming
    Replies:
    12
    Views:
    455
    David Thompson
    Jul 2, 2010
Loading...

Share This Page