Implementing

Discussion in 'C Programming' started by cogno_byte, Jan 29, 2006.

  1. cogno_byte

    cogno_byte Guest

    Hi all !!
    I need to know that in C how to implement sizeof() without using the
    keyword?
    The implementation should be generic.
    e.g
    malloc(sizeof(struct));

    Here the sizeof() returns the number of bytes reserved and is a
    reserved keyword which calls a function through the library function.
    But i want to implement the working of sizeof() without using the
    keyword.
    Hope all of ya got my Question.
    Hoping to read from all of you soon
    Thnx in advance.
     
    cogno_byte, Jan 29, 2006
    #1
    1. Advertising

  2. "cogno_byte" <> writes:
    > I need to know that in C how to implement sizeof() without using the
    > keyword?


    Why do you want to do this? It's like asking how to drive a nail
    without using a hammer.

    Just use sizeof().

    If you really do have a reason to want to do this, it's been asked
    before. Check the archives.

    --
    Keith Thompson (The_Other_Keith) <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.
     
    Keith Thompson, Jan 29, 2006
    #2
    1. Advertising

  3. Keith Thompson said:

    > Just use sizeof().


    Better still, use sizeof - which does not require its operand to be
    parenthesised (unless it's a type name).


    --
    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, Jan 29, 2006
    #3
  4. cogno_byte said:

    > Here the sizeof() returns the number of bytes reserved


    No, it doesn't.

    "The sizeof operator yields the size (in bytes) of its operand, which may be
    an expression or the parenthesized name of a type. The size is determined
    from the type of the operand, which is not itself evaluated. The result is
    an integer constant." - C89 3.3.3.4

    > and is a
    > reserved keyword which calls a function through the library function.


    Chapter and verse, please. I know of no requirement for sizeof to call a
    function.

    --
    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, Jan 29, 2006
    #4
  5. cogno_byte

    CBFalconer Guest

    cogno_byte wrote:
    >
    > I need to know that in C how to implement sizeof() without using
    > the keyword?
    > The implementation should be generic.
    > e.g
    > malloc(sizeof(struct));
    >
    > Here the sizeof() returns the number of bytes reserved and is a
    > reserved keyword which calls a function through the library function.
    > But i want to implement the working of sizeof() without using the
    > keyword.


    Can't be done. That is why it is supplied by the system, whose
    designer is allowed to use non-portable things to implement it.

    --
    "The power of the Executive to cast a man into prison without
    formulating any charge known to the law, and particularly to
    deny him the judgement of his peers, is in the highest degree
    odious and is the foundation of all totalitarian government
    whether Nazi or Communist." -- W. Churchill, Nov 21, 1943
     
    CBFalconer, Jan 29, 2006
    #5
  6. cogno_byte

    Thad Smith Guest

    cogno_byte wrote:

    > I need to know that in C how to implement sizeof() without using the
    > keyword?


    Your sentence makes no sense. Please use proper English.

    sizeof is an operator, not a function, in C. For all objects except
    variable length arrays the compiler knows from the declaration how large
    the object or type is and gives that value as the result of the
    operator. The way it is implemented is part of the compilation process,
    with access to the symbol table, not a runtime operation. VLAs are
    handled somewhat differently, since their size is not constant.

    > The implementation should be generic.
    > e.g
    > malloc(sizeof(struct));
    >
    > Here the sizeof() returns the number of bytes reserved and is a
    > reserved keyword which calls a function through the library function.
    > But i want to implement the working of sizeof() without using the
    > keyword.


    You could design your own language with your own keywords and implement
    a compiler for it.

    --
    Thad
     
    Thad Smith, Jan 29, 2006
    #6
  7. cogno_byte

    Jack Klein Guest

    Jack Klein, Jan 29, 2006
    #7
    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. Gomathi

    Implementing Interface

    Gomathi, Nov 17, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    572
    S.M. Altaf [MVP]
    Nov 17, 2005
  2. Eqbal Z
    Replies:
    3
    Views:
    5,215
    Eqbal
    Nov 16, 2003
  3. Dev

    Implementing E1 - E3

    Dev, Sep 9, 2004, in forum: VHDL
    Replies:
    1
    Views:
    760
    Allan Herriman
    Sep 9, 2004
  4. Johnsy Joseph
    Replies:
    2
    Views:
    1,130
    Brian Drummond
    Oct 29, 2004
  5. Riccardo Fregonese

    Need help implementing a proj on SPARTAN3

    Riccardo Fregonese, Dec 2, 2004, in forum: VHDL
    Replies:
    2
    Views:
    744
    Thomas Gebauer
    Jan 3, 2005
Loading...

Share This Page