Simulation of Sizeof operator

Discussion in 'C Programming' started by sabarish, Oct 5, 2005.

  1. sabarish

    sabarish Guest

    Hi to one and all

    one of the developer asked me "Can u implement one user defined funtion
    similar to the sizeof operaor in C Programming Language".
    is it possible in C ?
    sabarish, Oct 5, 2005
    #1
    1. Advertising

  2. In article <>,
    sabarish <> wrote:
    >one of the developer asked me "Can u implement one user defined funtion
    >similar to the sizeof operaor in C Programming Language".
    >is it possible in C ?


    We went over this at length just a few weeks ago.

    The quick summary is: No, not portably. But you can do it for
    machines in which the NULL pointer -happens- to be address 0.

    --
    University of Calgary researcher Christopher Auld has found that
    milk is the most "rational addiction" amongst the several studied.
    Walter Roberson, Oct 5, 2005
    #2
    1. Advertising

  3. "sabarish" <> wrote in message
    news:...
    > one of the developer asked me "Can u implement one user defined funtion
    > similar to the sizeof operaor in C Programming Language".
    > is it possible in C ?


    Most probably yes, but I'm not sure whether or not there are any nasal
    demons, portability issues :).
    Consider sizeof(type) being mimicked as ((size_t)((type*)0+1)).

    Alex
    Alexei A. Frounze, Oct 5, 2005
    #3
  4. "Alexei A. Frounze" <> wrote in message
    news:...
    > "sabarish" <> wrote in message
    > news:...
    > > one of the developer asked me "Can u implement one user defined funtion
    > > similar to the sizeof operaor in C Programming Language".
    > > is it possible in C ?

    >
    > Most probably yes, but I'm not sure whether or not there are any nasal
    > demons, portability issues :).
    > Consider sizeof(type) being mimicked as ((size_t)((type*)0+1)).
    >
    > Alex


    Right, as Walter just said, there can be problems if NULL (or 0) pointer
    isn't address 0.

    Alex
    Alexei A. Frounze, Oct 5, 2005
    #4
  5. sabarish

    Skarmander Guest

    Alexei A. Frounze wrote:
    > "Alexei A. Frounze" <> wrote in message
    > news:...
    >
    >>"sabarish" <> wrote in message
    >>news:...
    >>
    >>>one of the developer asked me "Can u implement one user defined funtion
    >>>similar to the sizeof operaor in C Programming Language".
    >>>is it possible in C ?

    >>
    >>Most probably yes, but I'm not sure whether or not there are any nasal
    >>demons, portability issues :).
    >>Consider sizeof(type) being mimicked as ((size_t)((type*)0+1)).
    >>
    >>Alex

    >
    >
    > Right, as Walter just said, there can be problems if NULL (or 0) pointer
    > isn't address 0.
    >

    Actually, it's more subtle; you can avoid the null pointer issue, but
    you can't avoid the possibility of 0 (or 0 + sizeof type) not being a
    valid address, even if not dereferenced.

    Then again, that's why we *have* sizeof, and people should stop asking
    this. :)

    S.
    Skarmander, Oct 5, 2005
    #5
  6. "Alexei A. Frounze" wrote:
    >
    > "Alexei A. Frounze" <> wrote in message
    > news:...
    > > "sabarish" <> wrote in message
    > > news:...
    > > > one of the developer asked me "Can u implement one user defined funtion
    > > > similar to the sizeof operaor in C Programming Language".
    > > > is it possible in C ?


    Is there some C class out there that keeps getting this inane question
    over and over?

    I suppose one _could_ look at it as "how well do you understand C?"

    > > Most probably yes, but I'm not sure whether or not there are any nasal
    > > demons, portability issues :).
    > > Consider sizeof(type) being mimicked as ((size_t)((type*)0+1)).
    > >
    > > Alex

    >
    > Right, as Walter just said, there can be problems if NULL (or 0) pointer
    > isn't address 0.


    Not if you cast it to (char*) and subtract (char*)NULL from it, as
    someone's posting of offsetof() did. (ie: think of sizeof in terms
    of offsetof() the start of type_array[1].)

    --
    +-------------------------+--------------------+-----------------------------+
    | Kenneth J. Brody | www.hvcomputer.com | |
    | kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------------+
    Don't e-mail me at: <mailto:>
    Kenneth Brody, Oct 5, 2005
    #6
  7. "Kenneth Brody" <> wrote in message
    news:...
    > "Alexei A. Frounze" wrote:
    > >
    > > "Alexei A. Frounze" <> wrote in message
    > > news:...
    > > > "sabarish" <> wrote in message
    > > > news:...
    > > > > one of the developer asked me "Can u implement one user defined

    funtion
    > > > > similar to the sizeof operaor in C Programming Language".
    > > > > is it possible in C ?

    >
    > Is there some C class out there that keeps getting this inane question
    > over and over?


    I suppose, yes. And I believe, there're *many*.

    > I suppose one _could_ look at it as "how well do you understand C?"


    Certainly.

    > > > Most probably yes, but I'm not sure whether or not there are any nasal
    > > > demons, portability issues :).
    > > > Consider sizeof(type) being mimicked as ((size_t)((type*)0+1)).
    > > >
    > > > Alex

    > >
    > > Right, as Walter just said, there can be problems if NULL (or 0) pointer
    > > isn't address 0.

    >
    > Not if you cast it to (char*) and subtract (char*)NULL from it, as
    > someone's posting of offsetof() did. (ie: think of sizeof in terms
    > of offsetof() the start of type_array[1].)


    Maybe. Actually, I've though about something similar recently -- I'd simply
    declare an array of 1 element of the given type and return difference
    between address of 1st and 0th elements... But that would only work for
    small types like base types numeric and pointers.

    Alex
    Alexei A. Frounze, Oct 5, 2005
    #7
  8. sabarish

    Zara Guest

    On Wed, 5 Oct 2005 16:38:12 +0000 (UTC), -cnrc.gc.ca
    (Walter Roberson) wrote:

    >In article <>,
    >sabarish <> wrote:
    >>one of the developer asked me "Can u implement one user defined funtion
    >>similar to the sizeof operaor in C Programming Language".
    >>is it possible in C ?

    >
    >We went over this at length just a few weeks ago.
    >
    >The quick summary is: No, not portably. But you can do it for
    >machines in which the NULL pointer -happens- to be address 0.


    IMHO, the answer is yes, and in a portable manner:

    #define SIZEOF(return,type)\
    {\
    type set[2];\
    return=(char*)(set+1)-(char *)set;\
    }

    But it adds szie to code and decreases speed, so it is infefficient
    Zara, Oct 6, 2005
    #8
  9. Zara <> writes:
    > On Wed, 5 Oct 2005 16:38:12 +0000 (UTC), -cnrc.gc.ca
    > (Walter Roberson) wrote:
    >>In article <>,
    >>sabarish <> wrote:
    >>>one of the developer asked me "Can u implement one user defined funtion
    >>>similar to the sizeof operaor in C Programming Language".
    >>>is it possible in C ?

    >>
    >>We went over this at length just a few weeks ago.
    >>
    >>The quick summary is: No, not portably. But you can do it for
    >>machines in which the NULL pointer -happens- to be address 0.

    >
    > IMHO, the answer is yes, and in a portable manner:
    >
    > #define SIZEOF(return,type)\
    > {\
    > type set[2];\
    > return=(char*)(set+1)-(char *)set;\
    > }
    >
    > But it adds szie to code and decreases speed, so it is infefficient


    That macro expands to a compound statement, which can't be used as an
    expression. (The use of "return" as a macro argument name is, um,
    interesting, but I don't think it will cause any problems.)

    --
    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, Oct 6, 2005
    #9
  10. sabarish

    pete Guest

    Zara wrote:
    >
    > On Wed, 5 Oct 2005 16:38:12 +0000 (UTC), -cnrc.gc.ca
    > (Walter Roberson) wrote:
    >
    > >In article <>,
    > >sabarish <> wrote:
    > >>one of the developer asked me "Can u implement one user defined funtion
    > >>similar to the sizeof operaor in C Programming Language".
    > >>is it possible in C ?

    > >
    > >We went over this at length just a few weeks ago.
    > >
    > >The quick summary is: No, not portably. But you can do it for
    > >machines in which the NULL pointer -happens- to be address 0.

    >
    > IMHO, the answer is yes, and in a portable manner:
    >
    > #define SIZEOF(return,type)\
    > {\
    > type set[2];\
    > return=(char*)(set+1)-(char *)set;\
    > }
    >


    If "type" is a user defined type, like a very large array,
    then it's possible that:
    1 ((char*)(set+1)-(char *)set) might excede
    the limits of ptrdiff_t and cause undefined behavior, or
    2 there's the possibility that type set[2] is too big too compile.

    --
    pete
    pete, Oct 6, 2005
    #10
  11. sabarish

    Guest

    since NULL may not be 0, why not try this?

    #define Sizeof(type) ((char *)((type*)NULL + 1) - NULL)
    #define SizeofObject(obj) ((char *)(&obj + 1) - (char*)&obj)

    btw. what will NULL be if it is not equal to 0 ?

    the problem says "user defined *funtion* similar to the sizeof
    operaor". what if it must NOT be a macro ?
    , Oct 6, 2005
    #11
  12. sabarish

    Flash Gordon Guest

    wrote:
    > since NULL may not be 0, why not try this?
    >
    > #define Sizeof(type) ((char *)((type*)NULL + 1) - NULL)


    Pointer arithmetic is only defined for pointers that point to an object
    null pointers do not point to objects. So this is invokes undefined and
    anything could happen.

    > #define SizeofObject(obj) ((char *)(&obj + 1) - (char*)&obj)


    This is fine. You are allowed to calculate the address of one passed an
    object.

    > btw. what will NULL be if it is not equal to 0 ?
    >
    > the problem says "user defined *funtion* similar to the sizeof
    > operaor". what if it must NOT be a macro ?


    Then you are stuck since there is no method to pass a type in to an object.

    I've yet to see what I would consider a good reason for wanting to do
    this anyway since C has a sizeof operator.
    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
    Flash Gordon, Oct 6, 2005
    #12
  13. sabarish

    kaikai Guest

    Flash Gordon wrote:

    > wrote:
    > > since NULL may not be 0, why not try this?
    > >
    > > #define Sizeof(type) ((char *)((type*)NULL + 1) - NULL)

    >
    > Pointer arithmetic is only defined for pointers that point to an object
    > null pointers do not point to objects. So this is invokes undefined and
    > anything could happen.


    I think pointer arithmetic is performed at compile time, so I guess if
    the compiler knows the type it may do that. Do you mean some compilers
    will check the object and refuse to do pointer arithmetic when the
    object is invalid?
    kaikai, Oct 7, 2005
    #13
  14. sabarish

    Flash Gordon Guest

    kaikai wrote:
    > Flash Gordon wrote:
    >
    >> wrote:
    >>
    >>>since NULL may not be 0, why not try this?
    >>>
    >>>#define Sizeof(type) ((char *)((type*)NULL + 1) - NULL)

    >>
    >>Pointer arithmetic is only defined for pointers that point to an object
    >>null pointers do not point to objects. So this is invokes undefined and
    >>anything could happen.

    >
    >
    > I think pointer arithmetic is performed at compile time, so I guess if
    > the compiler knows the type it may do that.


    The compiler *might* do it at compilation time, but it is under no
    obligation to do that.

    > Do you mean some compilers
    > will check the object and refuse to do pointer arithmetic when the
    > object is invalid?


    A processor might trap at run time when an invalid address is loaded in
    to a pointer variable.

    At compile time, if it does the pointer arithmetic at compile time, and
    NULL represented as something other than all bits zero the compiler
    might suffer from an integer overflow when doing the arithmetic causing
    to to produce an incorrect result. Undefined behaviour does not require
    a diagnostic, so the compiler could silently produce incorrect code in
    this instance and still be conforming.

    The point is that it is undefined behaviour so *anything* can happen,
    including what you want. However, C has the sizeof operator and the
    offsetof macro provided as part of the standard, so you don't have any
    need to do such tricks.

    Well, on embedded systems you do sometimes have to strange things when,
    for example, accessing memory mapped devices, but by then you are well
    in to the realms of system specific code anyway so as long as your
    compiler guarantees it you are OK.
    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
    Flash Gordon, Oct 7, 2005
    #14
  15. sabarish

    pete Guest

    Flash Gordon wrote:
    >
    > kaikai wrote:
    > > Flash Gordon wrote:
    > >
    > >> wrote:
    > >>
    > >>>since NULL may not be 0, why not try this?


    NULL may be zero.

    > >>>#define Sizeof(type) ((char *)((type*)NULL + 1) - NULL)
    > >>
    > >>Pointer arithmetic is only defined for pointers
    > >>that point to an object
    > >>null pointers do not point to objects.
    > >>So this is invokes undefined and anything could happen.


    Undefined behavior, yes.

    > > I think pointer arithmetic is performed at compile time,
    > > so I guess if
    > > the compiler knows the type it may do that.

    >
    > The compiler *might* do it at compilation time, but it is under no
    > obligation to do that.


    It's "constant expressions" which the compiler has the option
    to calculate during compile time.

    There's nothing special about pointer arithmetic,
    a far as compile time vs. run time issues are concerned.

    > > Do you mean some compilers
    > > will check the object and refuse to do pointer arithmetic when the
    > > object is invalid?

    >
    > A processor might trap at run time when an
    > invalid address is loaded in to a pointer variable.


    I'm not following.
    There's neither objects nor variables in that Sizeof() macro.

    (type*)NULL is a null pointer.

    (null pointer + 1) is undefined: it's just that simple.

    N869
    7.17 Common definitions <stddef.h>
    [#3] The macros are
    NULL
    which expands to an implementation-defined null pointer
    constant

    6.3.2.3 Pointers
    [#3] If a null pointer constant is
    converted to a pointer type, the resulting pointer, called a
    null pointer, is guaranteed to compare unequal to a pointer
    to any object or function.

    6.5.6 Additive operators
    [#8] If both the
    pointer operand and the result point to elements of the same
    array object, or one past the last element of the array
    object, the evaluation shall not produce an overflow;
    otherwise, the behavior is undefined.

    --
    pete
    pete, Oct 7, 2005
    #15
  16. sabarish

    Flash Gordon Guest

    pete wrote:
    > Flash Gordon wrote:
    >
    >>kaikai wrote:
    >>
    >>>Flash Gordon wrote:
    >>>
    >>>> wrote:
    >>>>
    >>>>>since NULL may not be 0, why not try this?

    >
    > NULL may be zero.


    Indeed.

    >>>>>#define Sizeof(type) ((char *)((type*)NULL + 1) - NULL)
    >>>>
    >>>>Pointer arithmetic is only defined for pointers
    >>>>that point to an object
    >>>>null pointers do not point to objects.
    >>>>So this is invokes undefined and anything could happen.

    >
    > Undefined behavior, yes.


    Yes.

    >>>I think pointer arithmetic is performed at compile time,
    >>>so I guess if
    >>>the compiler knows the type it may do that.

    >>
    >>The compiler *might* do it at compilation time, but it is under no
    >>obligation to do that.

    >
    > It's "constant expressions" which the compiler has the option
    > to calculate during compile time.
    >
    > There's nothing special about pointer arithmetic,
    > a far as compile time vs. run time issues are concerned.


    Yes. What I said applies to *all* constant expressions. The compiler can
    choose to compute any or all of them at run time.

    >>>Do you mean some compilers
    >>>will check the object and refuse to do pointer arithmetic when the
    >>>object is invalid?

    >>
    >>A processor might trap at run time when an
    >>invalid address is loaded in to a pointer variable.

    >
    > I'm not following.
    > There's neither objects nor variables in that Sizeof() macro.
    >
    > (type*)NULL is a null pointer.


    Yes.

    > (null pointer + 1) is undefined: it's just that simple.


    <snip stuff I agree with>

    Yes. I'm sure I have stated that in this thread somewhere.

    I was trying to point out to kaikai one of the infinite number of ways
    the code could actually fail because people are often more willing to
    accept that something might not always work if you can show them
    something that will cause it to fail.
    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
    Flash Gordon, Oct 7, 2005
    #16
  17. sabarish

    pete Guest

    pete wrote:
    >
    > Flash Gordon wrote:
    > >
    > > kaikai wrote:
    > > > Flash Gordon wrote:
    > > >
    > > >> wrote:
    > > >>
    > > >>>since NULL may not be 0, why not try this?

    >
    > NULL may be zero.
    >
    > > >>>#define Sizeof(type) ((char *)((type*)NULL + 1) - NULL)
    > > >>
    > > >>Pointer arithmetic is only defined for pointers
    > > >>that point to an object
    > > >>null pointers do not point to objects.
    > > >>So this is invokes undefined and anything could happen.

    >
    > Undefined behavior, yes.
    >
    > > > I think pointer arithmetic is performed at compile time,
    > > > so I guess if
    > > > the compiler knows the type it may do that.

    > >
    > > The compiler *might* do it at compilation time, but it is under no
    > > obligation to do that.

    >
    > It's "constant expressions" which the compiler has the option
    > to calculate during compile time.
    >
    > There's nothing special about pointer arithmetic,
    > a far as compile time vs. run time issues are concerned.
    >
    > > > Do you mean some compilers
    > > > will check the object and refuse to do pointer arithmetic when the
    > > > object is invalid?

    > >
    > > A processor might trap at run time when an
    > > invalid address is loaded in to a pointer variable.

    >
    > I'm not following.
    > There's neither objects nor variables in that Sizeof() macro.
    >
    > (type*)NULL is a null pointer.


    .... which is also a constant expresssion.

    N869
    6.6 Constant expressions
    [#9] An address constant is a null pointer, a pointer to an
    lvalue designating an object of static storage duration, or
    to a function designator;

    > (null pointer + 1) is undefined: it's just that simple.
    >
    > N869
    > 7.17 Common definitions <stddef.h>
    > [#3] The macros are
    > NULL
    > which expands to an implementation-defined null pointer
    > constant
    >
    > 6.3.2.3 Pointers
    > [#3] If a null pointer constant is
    > converted to a pointer type, the resulting pointer, called a
    > null pointer, is guaranteed to compare unequal to a pointer
    > to any object or function.
    >
    > 6.5.6 Additive operators
    > [#8] If both the
    > pointer operand and the result point to elements of the same
    > array object, or one past the last element of the array
    > object, the evaluation shall not produce an overflow;
    > otherwise, the behavior is undefined.


    --
    pete
    pete, Oct 7, 2005
    #17
  18. sabarish

    kaikai Guest

    oh... I used to think compilers will convert pointer arithmetic to
    numerical arithmetic by multiplying sizeof pointed type.
    sth. like
    Type *pA;
    pA = pA + n;
    will become
    (char*)pA = (char*)pA + n * sizeof(Type);
    at compile time.
    A real processor does not know what a type is, it just add pA with a
    constant value.
    kaikai, Oct 8, 2005
    #18
  19. On 7 Oct 2005 22:14:35 -0700, "kaikai" <> wrote:

    regarding (type*)NULL + 1, without quoting: as posted here thousands
    of times over the past few weeks, if using the google broken beta, do
    not just click Reply, click on Show Options and use that Reply to get
    the previous article quoted, and then following normal good practice
    edit it down to the point(s) you are responding to. And complain to
    google that they made correct practice more difficult.

    > oh... I used to think compilers will convert pointer arithmetic to
    > numerical arithmetic by multiplying sizeof pointed type.
    > sth. like
    > Type *pA;
    > pA = pA + n;
    > will become
    > (char*)pA = (char*)pA + n * sizeof(Type);
    > at compile time.
    > A real processor does not know what a type is, it just add pA with a
    > constant value.


    That syntax is a GCC extension, standardly you need
    pA = (Type*)( (char*)pA + n * sizeof(Type) );

    With that trivial fix, in practice on mainstream byte-addressed
    machines they do. Similarly on _most_ implementations offsetof(a,b)
    can be just (size_t) & ((a*)NULL) ->b . But the standard doesn't
    require it, and implementations can differ, as Flash Gordon said.
    - David.Thompson1 at worldnet.att.net
    Dave Thompson, Oct 17, 2005
    #19
    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,324
    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. Manish_Ganvir
    Replies:
    13
    Views:
    1,569
    Keith Thompson
    Feb 14, 2005
  4. Vinu
    Replies:
    13
    Views:
    1,414
    Lawrence Kirby
    May 12, 2005
  5. Kislay

    The sizeof operator : sizeof(++i)

    Kislay, Oct 18, 2007, in forum: C Programming
    Replies:
    10
    Views:
    684
    Peter Pichler
    Oct 19, 2007
Loading...

Share This Page