Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Simulation of Sizeof operator

Reply
Thread Tools

Simulation of Sizeof operator

 
 
zikaizhang@gmail.com
Guest
Posts: n/a
 
      10-06-2005
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 ?

 
Reply With Quote
 
 
 
 
Flash Gordon
Guest
Posts: n/a
 
      10-06-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.
 
Reply With Quote
 
 
 
 
kaikai
Guest
Posts: n/a
 
      10-07-2005
Flash Gordon wrote:

> (E-Mail Removed) 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?

 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      10-07-2005
kaikai wrote:
> Flash Gordon wrote:
>
>>(E-Mail Removed) 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.
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      10-07-2005
Flash Gordon wrote:
>
> kaikai wrote:
> > Flash Gordon wrote:
> >
> >>(E-Mail Removed) 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
 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      10-07-2005
pete wrote:
> Flash Gordon wrote:
>
>>kaikai wrote:
>>
>>>Flash Gordon wrote:
>>>
>>>>(E-Mail Removed) 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.
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      10-07-2005
pete wrote:
>
> Flash Gordon wrote:
> >
> > kaikai wrote:
> > > Flash Gordon wrote:
> > >
> > >>(E-Mail Removed) 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
 
Reply With Quote
 
kaikai
Guest
Posts: n/a
 
      10-08-2005
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.

 
Reply With Quote
 
Dave Thompson
Guest
Posts: n/a
 
      10-17-2005
On 7 Oct 2005 22:14:35 -0700, "kaikai" <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
The sizeof operator : sizeof(++i) Kislay C Programming 10 10-19-2007 09:28 PM
#define ARR_SIZE sizeof(arr)/sizeof(arr[0]) Vinu C Programming 13 05-12-2005 06:00 PM
How to calculate size of an int without using the sizeof operator but using bitwise operator Manish_Ganvir C Programming 13 02-14-2005 07:24 PM
sizeof(enum) == sizeof(int) ??? Derek C++ 7 10-14-2004 05:11 PM
sizeof(str) or sizeof(str) - 1 ? Trevor C Programming 9 04-10-2004 05:07 PM



Advertisments