473,386 Members | 1,823 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,386 software developers and data experts.

implementing sizeof

Hi,

Is it possible to implement sizeof as a C function ?

Jan 6 '07 #1
15 3247
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
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
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

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

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

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

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
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
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

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
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
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

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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

70
by: Rajan | last post by:
Hi, I am trying to simulate a memcpy like this void* mem_cpy(void* dest, void* src, int bytes) { dest = malloc(bytes); } Now if I want to copy the bytes from src to dest, how do I copy these...
6
by: cogno_byte | last post by:
Hi all !! I need to know that in C how to implement sizeof() without using the keyword? The implementation should be generic. e.g malloc(sizeof(struct)); Here the sizeof() returns the number...
32
by: Abhishek Srivastava | last post by:
Hi, Somebody recently asked me to implement the sizeof operator, i.e. to write a function that accepts a parameter of any type, and without using the sizeof operator, should be able to return...
5
by: alan | last post by:
Hello world, I'm trying to implement a (hopefully portable!) tagged pointer class. Basically, I have my own allocator which will ensure alignment at 8- byte boundaries (where "byte" is "size of...
1
by: Van Dugall | last post by:
What can do to make this client file into a Forward Web Cache -- for static contents, usng the if-modified approach for coherence... I'm not that comfortable with C but it is the only language I know...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.