473,394 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Simulation of Sizeof operator

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 ?

Nov 15 '05 #1
18 1957
In article <11**********************@o13g2000cwo.googlegroups .com>,
sabarish <su******@gmail.com> 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.
Nov 15 '05 #2
"sabarish" <su******@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr 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
Nov 15 '05 #3
"Alexei A. Frounze" <al*****@chat.ru> wrote in message
news:3q************@individual.net...
"sabarish" <su******@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr 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
Nov 15 '05 #4
Alexei A. Frounze wrote:
"Alexei A. Frounze" <al*****@chat.ru> wrote in message
news:3q************@individual.net...
"sabarish" <su******@gmail.com> wrote in message
news:11**********************@o13g2000cwo.google 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.
Nov 15 '05 #5
"Alexei A. Frounze" wrote:

"Alexei A. Frounze" <al*****@chat.ru> wrote in message
news:3q************@individual.net...
"sabarish" <su******@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr 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: <mailto:Th*************@gmail.com>
Nov 15 '05 #6
"Kenneth Brody" <ke******@spamcop.net> wrote in message
news:43***************@spamcop.net...
"Alexei A. Frounze" wrote:

"Alexei A. Frounze" <al*****@chat.ru> wrote in message
news:3q************@individual.net...
"sabarish" <su******@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr 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
Nov 15 '05 #7
On Wed, 5 Oct 2005 16:38:12 +0000 (UTC), ro******@ibd.nrc-cnrc.gc.ca
(Walter Roberson) wrote:
In article <11**********************@o13g2000cwo.googlegroups .com>,
sabarish <su******@gmail.com> 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
Nov 15 '05 #8
Zara <yo****@terra.es> writes:
On Wed, 5 Oct 2005 16:38:12 +0000 (UTC), ro******@ibd.nrc-cnrc.gc.ca
(Walter Roberson) wrote:
In article <11**********************@o13g2000cwo.googlegroups .com>,
sabarish <su******@gmail.com> 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) ks***@mib.org <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.
Nov 15 '05 #9
Zara wrote:

On Wed, 5 Oct 2005 16:38:12 +0000 (UTC), ro******@ibd.nrc-cnrc.gc.ca
(Walter Roberson) wrote:
In article <11**********************@o13g2000cwo.googlegroups .com>,
sabarish <su******@gmail.com> 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
Nov 15 '05 #10
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 ?

Nov 15 '05 #11
zi********@gmail.com 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.
Nov 15 '05 #12
Flash Gordon wrote:
zi********@gmail.com 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?

Nov 15 '05 #13
kaikai wrote:
Flash Gordon wrote:
zi********@gmail.com 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.
Nov 15 '05 #14
Flash Gordon wrote:

kaikai wrote:
Flash Gordon wrote:
zi********@gmail.com 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
Nov 15 '05 #15
pete wrote:

Flash Gordon wrote:

kaikai wrote:
Flash Gordon wrote:

>zi********@gmail.com 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
Nov 15 '05 #16
pete wrote:
Flash Gordon wrote:
kaikai wrote:
Flash Gordon wrote:

zi********@gmail.com 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.
Nov 15 '05 #17
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.

Nov 15 '05 #18
On 7 Oct 2005 22:14:35 -0700, "kaikai" <zi********@gmail.com> 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
Nov 15 '05 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Constandinos Mavromoustakis | last post by:
Dear all, first we apologize if you receive multiple copies of this announcement. please see below if you are interested. Thank you in advance....
70
by: Roy Yao | last post by:
Does it mean "(sizeof(int))* (p)" or "sizeof( (int)(*p) )" ? According to my analysis, operator sizeof, (type) and * have the same precedence, and they combine from right to left. Then this...
0
by: Constandinos Mavromoustakis | last post by:
http://agent.csd.auth.gr/~cmavrom -------------------------------------------------- ============================================================================ = 37th Annual Simulation...
31
by: Anjali M | last post by:
#include <stdio.h> int main() { int number = 10; printf("Number is: %d\n", number); printf("Sizeof of number is: %d\n", sizeof(number++)); printf("Number now is: %d\n", number);
7
by: dam_fool_2003 | last post by:
#include<stdio.h> int main(void) { unsigned int a=20,b=50, c = sizeof b+a; printf("%d\n",c); return 0; } out put: 24
12
by: ozbear | last post by:
If one were writing a C interpreter, is there anything in the standard standard that requires the sizeof operator to yield the same value for two different variables of the same type? Let's...
15
by: Alex Vinokur | last post by:
Why does one need to use two kinds of sizeof operator: * sizeof unary-expression, * sizeof (type-name) ? Their behavior seem not to be different (see an example below). ------ C++ code...
43
by: Richard | last post by:
Could someone point me to why "sizeof x" is/isnt preferable to "sizeof(x)",
28
by: Howard Bryce | last post by:
I have come across code containing things like sizeof int How come that one can invoke sizeof without any parentheses surrounding its argument? Is this admissible within the standard? Can it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.