Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

Simulation of Sizeof operator

 
 
sabarish
Guest
Posts: n/a
 
      10-05-2005
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 ?

 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      10-05-2005
In article <(E-Mail Removed) .com>,
sabarish <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
Alexei A. Frounze
Guest
Posts: n/a
 
      10-05-2005
"sabarish" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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


 
Reply With Quote
 
Alexei A. Frounze
Guest
Posts: n/a
 
      10-05-2005
"Alexei A. Frounze" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "sabarish" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...
> > 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


 
Reply With Quote
 
Skarmander
Guest
Posts: n/a
 
      10-05-2005
Alexei A. Frounze wrote:
> "Alexei A. Frounze" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
>>"sabarish" <(E-Mail Removed)> wrote in message
>>news:(E-Mail Removed) groups.com...
>>
>>>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.
 
Reply With Quote
 
Kenneth Brody
Guest
Posts: n/a
 
      10-05-2005
"Alexei A. Frounze" wrote:
>
> "Alexei A. Frounze" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > "sabarish" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed) oups.com...
> > > 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: <(E-Mail Removed)>


 
Reply With Quote
 
Alexei A. Frounze
Guest
Posts: n/a
 
      10-05-2005
"Kenneth Brody" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "Alexei A. Frounze" wrote:
> >
> > "Alexei A. Frounze" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> > > "sabarish" <(E-Mail Removed)> wrote in message
> > > news:(E-Mail Removed) oups.com...
> > > > 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


 
Reply With Quote
 
Zara
Guest
Posts: n/a
 
      10-06-2005
On Wed, 5 Oct 2005 16:38:12 +0000 (UTC), http://www.velocityreviews.com/forums/(E-Mail Removed)-cnrc.gc.ca
(Walter Roberson) wrote:

>In article <(E-Mail Removed) .com>,
>sabarish <(E-Mail Removed)> 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
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      10-06-2005
Zara <(E-Mail Removed)> writes:
> On Wed, 5 Oct 2005 16:38:12 +0000 (UTC), (E-Mail Removed)-cnrc.gc.ca
> (Walter Roberson) wrote:
>>In article <(E-Mail Removed) .com>,
>>sabarish <(E-Mail Removed)> 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.velocityreviews.com/forums/(E-Mail Removed) <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.
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      10-06-2005
Zara wrote:
>
> On Wed, 5 Oct 2005 16:38:12 +0000 (UTC), (E-Mail Removed)-cnrc.gc.ca
> (Walter Roberson) wrote:
>
> >In article <(E-Mail Removed) .com>,
> >sabarish <(E-Mail Removed)> 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
 
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