Re: replacement for sizeof()

Discussion in 'C Programming' started by Brett Frankenberger, Aug 12, 2003.

  1. In article <bh9hvc$abo$>,
    istartedi <> wrote:
    >
    >vishnu mahendra <> wrote in message
    >news:...
    >> can anyone please tell me how to find the size of a variable or a
    >> datatype with out using a sizeof() operatot.
    >> thank you in advance,
    >> vishnu

    >
    >Ummm... write it to a temporary binary file, then read it back.
    >
    >p.s., I'm sure somebody will poke a hole in this method, but I can't
    >think of one right now.


    Can you please post code that will write it to a binary file without
    using sizeof?

    -- Brett
    Brett Frankenberger, Aug 12, 2003
    #1
    1. Advertising

  2. In article <bhao2p$92t$-Berlin.DE>,
    Jirka Klaue <-berlin.de> wrote:
    >Brett Frankenberger wrote:
    >> In article <bh9hvc$abo$>,
    >> istartedi <> wrote:
    >>
    >>>vishnu mahendra <> wrote in message
    >>>news:...
    >>>
    >>>>can anyone please tell me how to find the size of a variable or a
    >>>>datatype with out using a sizeof() operatot.
    >>>>thank you in advance,
    >>>>vishnu
    >>>
    >>>Ummm... write it to a temporary binary file, then read it back.
    >>>
    >>>p.s., I'm sure somebody will poke a hole in this method, but I can't
    >>>think of one right now.

    >>
    >> Can you please post code that will write it to a binary file without
    >> using sizeof?

    >
    > type var, x[2];
    >
    > fwrite(&var, 1, (int)&x[1] - (int)&x[0], f);


    Fair enough. But why bother writing it and reading it back, then. Why
    not just
    #define mysizeof(x) (int)&(x)[1] - (int)&(x)[0]

    -- Brett
    Brett Frankenberger, Aug 12, 2003
    #2
    1. Advertising

  3. Brett Frankenberger

    Dan Pop Guest

    In <bhao2p$92t$-Berlin.DE> Jirka Klaue <-berlin.de> writes:

    >Brett Frankenberger wrote:
    >> In article <bh9hvc$abo$>,
    >> istartedi <> wrote:
    >>
    >>>vishnu mahendra <> wrote in message
    >>>news:...
    >>>
    >>>>can anyone please tell me how to find the size of a variable or a
    >>>>datatype with out using a sizeof() operatot.
    >>>>thank you in advance,
    >>>>vishnu
    >>>
    >>>Ummm... write it to a temporary binary file, then read it back.
    >>>
    >>>p.s., I'm sure somebody will poke a hole in this method, but I can't
    >>>think of one right now.

    >>
    >> Can you please post code that will write it to a binary file without
    >> using sizeof?

    >
    > type var, x[2];
    >
    > fwrite(&var, 1, (int)&x[1] - (int)&x[0], f);


    What is the purpose of the fwrite call in this case? The idea was to use
    fwrite() as part of the solution, but you can't use it unless you already
    have the solution ;-)

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Dan Pop, Aug 12, 2003
    #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. Derek
    Replies:
    7
    Views:
    24,325
    Ron Natalie
    Oct 14, 2004
  2. Trevor

    sizeof(str) or sizeof(str) - 1 ?

    Trevor, Apr 3, 2004, in forum: C Programming
    Replies:
    9
    Views:
    626
    CBFalconer
    Apr 10, 2004
  3. Vinu
    Replies:
    13
    Views:
    1,414
    Lawrence Kirby
    May 12, 2005
  4. blufox

    sizeof( int ) != sizeof( void * )

    blufox, May 22, 2006, in forum: C Programming
    Replies:
    2
    Views:
    554
    Joe Smith
    May 22, 2006
  5. Alex Vinokur
    Replies:
    7
    Views:
    496
    Clark S. Cox III
    Aug 14, 2006
Loading...

Share This Page