By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,733 Members | 889 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,733 IT Pros & Developers. It's quick & easy.

implementing sizeof

P: n/a
Hi,

Is it possible to implement sizeof as a C function ?

Jan 6 '07 #1
Share this Question
Share on Google+
15 Replies


P: n/a
ju**********@yahoo.co.in said:
Hi,

Is it possible to implement sizeof as a C function ?
No.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jan 6 '07 #2

P: n/a
ju**********@yahoo.co.in wrote:
Hi,

Is it possible to implement sizeof as a C function ?
sizeof is defined as an operator; hence it can't be implemented as a
function.

Jan 6 '07 #3

P: n/a
santosh said:
ju**********@yahoo.co.in wrote:
>Hi,

Is it possible to implement sizeof as a C function ?

sizeof is defined as an operator; hence it can't be implemented as a
function.
But can its functionality be duplicated in a function of some other name
that does not use sizeof itself? I think that's what the questioner meant.

And the reason it can't is that, even if you allow "mysizeof(foo)" rather
than insisting on the provision of "mysizeof foo" syntax for *objects*, you
can't pass *types* to functions, so you're stuffed right at the outset.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jan 6 '07 #4

P: n/a

ju**********@yahoo.co.in wrote:
Hi,

Is it possible to implement sizeof as a C function ?
Not completely, but you can implement part of it. Read:
http://prokutfaq.byethost15.com/FindSizeWithoutSizeof
This really is of no use in practice because sizeof is part of
standard. The methods described in that article is not fully portable,
and has its limitations as Richard pointed out.

Jan 6 '07 #5

P: n/a

ju**********@yahoo.co.in wrote:
Hi,

Is it possible to implement sizeof as a C function ?
answer is no like others said but some code like below can fool you
thinking that mysizeof is similar to sizeof

#include<stdio.h>
#define str(x) #x
#define mysizeof(y) mysizeof1(str(y))

int mysizeof1(char *s)
{
if(strcmp(s,"int")==0) /* what ever the defined data types are
like long double etc */
return 1;

/* The compiler can do this because it stores the mapping
like a is associated with int ,as it scans the program but
* at runtime this information is lost/not known easily */
/* If you can find some way to associate a with type int or say
a mapping you can but i think at this stage it is too late because
promotions could have actually taken place or how would know what type
was passed to the function.
*/

return -1;
}
int main()
{
printf("%d\n",mysizeof(int));
printf("%d\n",mysizeof(a));/* i am going to get -1 here */
/* sizeof has lot of props like it doesn't evaluate the
experssion or the type . So you will have to take care of all that */
/* so what i will do on machines with CHAR_BIT 8 is a=0xFFFF;
now if sizeof(a) is 1, it will only contain FF, if sizeof(a) is 2 it
will contain FFFF, so on but this a very crude or wrong implementation
*/
return 0;
}

Probably you shouldn't be doing this but it is just for the sake of
curiousity and filled with lots of bugs
Question for others does this change for machines where CHAR_BIT is 9
or xyz.
If it is 0xFF on machines where CHAR_BIT is 8
a=0xFF;
should it be a=0x1FF on machines with CHAR_BIT 9
I think the latter but am little confused. Please clarify

Jan 6 '07 #6

P: n/a

santosh wrote:
ju**********@yahoo.co.in wrote:
Hi,

Is it possible to implement sizeof as a C function ?

sizeof is defined as an operator; hence it can't be implemented as a
function.
I don't agree with your explanation completely. "*", "-" are
arithmetic operators, but they can be implemented as a function.

Jan 7 '07 #7

P: n/a

Sharath wrote:
ju**********@yahoo.co.in wrote:
Hi,

Is it possible to implement sizeof as a C function ?

Not completely, but you can implement part of it. Read:
http://prokutfaq.byethost15.com/FindSizeWithoutSizeof
This really is of no use in practice because sizeof is part of
standard. The methods described in that article is not fully portable,
and has its limitations as Richard pointed out.
The link that you pointed seems to have an interesting way of
implementing sizeof. As per the link, the sizeof may be implemented as
follows:

size_t size_obj = (char*)(&obj + 1) - (char*)(&obj);

But, as per C standard, while subtracting two pointers, both should
point to elements of same array object or one past the last element of
the array object.
In the above statement, the pointers need not belong to the same array
object. However, my question is that, under what conditions this
statement would fail ?

Jan 7 '07 #8

P: n/a
Hi,
>
Is it possible to implement sizeof as a C function ?

sizeof is defined as an operator; hence it can't be implemented as a
function.

I don't agree with your explanation completely. "*", "-" are
arithmetic operators, but they can be implemented as a function.
How does one implement + as *A* function that takes pointer+integer,
integer+integer, double+integer, or double+double arguments? And
returns pointer, integer, or double results?

Jan 7 '07 #9

P: n/a
ju**********@yahoo.co.in wrote:
Sharath wrote:
ju**********@yahoo.co.in wrote:
Hi,
>
Is it possible to implement sizeof as a C function ?
Not completely, but you can implement part of it. Read:
http://prokutfaq.byethost15.com/FindSizeWithoutSizeof
This really is of no use in practice because sizeof is part of
standard. The methods described in that article is not fully portable,
and has its limitations as Richard pointed out.

The link that you pointed seems to have an interesting way of
implementing sizeof. As per the link, the sizeof may be implemented as
follows:

size_t size_obj = (char*)(&obj + 1) - (char*)(&obj);

But, as per C standard, while subtracting two pointers, both should
point to elements of same array object or one past the last element of
the array object.
In the above statement, the pointers need not belong to the same array
object.
A pointer to a single object can be treated as a pointer to an array of
1
element. [6.5.6p7]
However, my question is that, under what conditions this
statement would fail ?
Only if the object were bigger than PTRDIFF_MAX bytes.
[Note that PTRDIFF_MAX is typically smaller than SIZE_MAX.]

It can fail if obj has an incomplete type, but then sizeof would fail
too.

--
Peter

Jan 7 '07 #10

P: n/a

Gordon Burditt wrote:
Hi,

Is it possible to implement sizeof as a C function ?

sizeof is defined as an operator; hence it can't be implemented as a
function.
I don't agree with your explanation completely. "*", "-" are
arithmetic operators, but they can be implemented as a function.

How does one implement + as *A* function that takes pointer+integer,
integer+integer, double+integer, or double+double arguments? And
returns pointer, integer, or double results?
I didn't mean to say, that there won't be any "+" operator at all. What
I wanted to ask that although we have a sizeof,+,-,* etc as operators,
but can we somehow implement them using a function?
In case, "+" is implemented as a ADD() function, then we can't use
pointer+pointer as arguments. In that case, pointer+pointer should be
evaluated by calling function ADD(). function.

Jan 7 '07 #11

P: n/a
ju**********@yahoo.co.in wrote:
santosh wrote:
ju**********@yahoo.co.in wrote:
Hi,
>
Is it possible to implement sizeof as a C function ?
sizeof is defined as an operator; hence it can't be implemented as a
function.

I don't agree with your explanation completely. "*", "-" are
arithmetic operators, but they can be implemented as a function.
Well, as Richard pointed out, sizeof must accept types. A function
can't accept a type. It can only accept an object of that type.
Moreover, without using extensions, how can you know the size of the
fundamental types? They're a part of the implementation, thus your
function has to be a part of the implementation too.

Moreover, sizeof is used during compilation. A function you write can't
execute during compilation. I'm sure there're other ceavets that
prevent a function from completely replacing sizeof. If it could, we
probably wouldn't be having sizeof as a part of the language.

Jan 7 '07 #12

P: n/a
ju**********@yahoo.co.in wrote:
Gordon Burditt wrote:
>>>>>Hi,
>
Is it possible to implement sizeof as a C function ?

sizeof is defined as an operator; hence it can't be implemented as a
function.

I don't agree with your explanation completely. "*", "-" are
arithmetic operators, but they can be implemented as a function.

How does one implement + as *A* function that takes pointer+integer,
integer+integer, double+integer, or double+double arguments? And
returns pointer, integer, or double results?


I didn't mean to say, that there won't be any "+" operator at all. What
I wanted to ask that although we have a sizeof,+,-,* etc as operators,
but can we somehow implement them using a function?
In case, "+" is implemented as a ADD() function, then we can't use
pointer+pointer as arguments. In that case, pointer+pointer should be
evaluated by calling function ADD(). function.
You appear to have answered your own question, you can't write a generic
function in C, to do that requites either function overloading, or some
form of generics/templates which C does not have.

So you would either have to write a sizeof function of each type, which
defeats the object, or use another language.

--
Ian Collins.
Jan 7 '07 #13

P: n/a

Peter Nilsson wrote:
ju**********@yahoo.co.in wrote:
Sharath wrote:
ju**********@yahoo.co.in wrote:
Hi,

Is it possible to implement sizeof as a C function ?
>
Not completely, but you can implement part of it. Read:
http://prokutfaq.byethost15.com/FindSizeWithoutSizeof
This really is of no use in practice because sizeof is part of
standard. The methods described in that article is not fully portable,
and has its limitations as Richard pointed out.
The link that you pointed seems to have an interesting way of
implementing sizeof. As per the link, the sizeof may be implemented as
follows:

size_t size_obj = (char*)(&obj + 1) - (char*)(&obj);

But, as per C standard, while subtracting two pointers, both should
point to elements of same array object or one past the last element of
the array object.
In the above statement, the pointers need not belong to the same array
object.

A pointer to a single object can be treated as a pointer to an array of
1
element. [6.5.6p7]
However, my question is that, under what conditions this
statement would fail ?

Only if the object were bigger than PTRDIFF_MAX bytes.
[Note that PTRDIFF_MAX is typically smaller than SIZE_MAX.]
Thanks Peter, for telling this.
It can fail if obj has an incomplete type, but then sizeof would fail
too.
Are you sure that sizeof would also fail, becuase as you pointed out
that PTRDIFF_MAX is typically smaller than SIZE_MAX.
Note: sizeof operator uses the type size_t to return the result.

Jan 7 '07 #14

P: n/a
trm
ju**********@yahoo.co.in schrieb:
Peter Nilsson wrote:
ju**********@yahoo.co.in wrote:
Sharath wrote:
ju**********@yahoo.co.in wrote:
Is it possible to implement sizeof as a C function ?

Not completely, but you can implement part of it. Read:
http://prokutfaq.byethost15.com/FindSizeWithoutSizeof
[...]
The link that you pointed seems to have an interesting way
of implementing sizeof. As per the link, the sizeof may be
implemented as follows:
>
size_t size_obj = (char*)(&obj + 1) - (char*)(&obj);
[...]
However, my question is that, under what conditions this
statement would fail ?
Only if the object were bigger than PTRDIFF_MAX bytes.
[Note that PTRDIFF_MAX is typically smaller than SIZE_MAX.]

It can fail if obj has an incomplete type, but then sizeof
would fail too.
Are you sure that sizeof would also fail, becuase as you pointed
out that PTRDIFF_MAX is typically smaller than SIZE_MAX.
Note: sizeof operator uses the type size_t to return the result.
That's not relevent; he meant something like the following:

struct foo;
size_t foo_size = sizeof(struct foo);

Without a complete definition of struct foo, that can't compile.

Jan 7 '07 #15

P: n/a
"santosh" <sa*********@gmail.comwrote:
ju**********@yahoo.co.in wrote:
santosh wrote:
ju**********@yahoo.co.in wrote:
Is it possible to implement sizeof as a C function ?
>
sizeof is defined as an operator; hence it can't be implemented as a
function.
I don't agree with your explanation completely. "*", "-" are
arithmetic operators, but they can be implemented as a function.

Well, as Richard pointed out, sizeof must accept types. A function
can't accept a type. It can only accept an object of that type.
Moreover, without using extensions, how can you know the size of the
fundamental types? They're a part of the implementation, thus your
function has to be a part of the implementation too.
Of an object, (char *)(&object+1) - (char *)&object will give you the
size of that object. If you _could_ pass types to functions, you could
declare one object of that type and reduce to the previous problem.
Moreover, sizeof is used during compilation.
That's the other fundamental technical problem. Relatedly, a function
call is never constant expression, so you can't, for example, declare
unsigned char buffer[sizeof_function(object) * number], which you can do
with sizeof.

Richard
Jan 8 '07 #16

This discussion thread is closed

Replies have been disabled for this discussion.