473,769 Members | 6,203 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

implementing sizeof

Hi,

Is it possible to implement sizeof as a C function ?

Jan 6 '07 #1
15 3281
ju**********@ya hoo.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**********@ya hoo.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**********@ya hoo.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(f oo)" 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**********@ya hoo.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**********@ya hoo.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,"in t")==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",m ysizeof(int));
printf("%d\n",m ysizeof(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**********@ya hoo.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**********@ya hoo.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**********@ya hoo.co.in wrote:
Sharath wrote:
ju**********@ya hoo.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

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

Similar topics

70
11663
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 bytes. I am stuck here.
6
1543
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 of bytes reserved and is a reserved keyword which calls a function through the library function. But i want to implement the working of sizeof() without using the
32
2591
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 the size occupied by that datatype in memory in bytes. Thanks :) Abhishek Srivastava
5
3130
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 a char"), and allocates objects of type Generic. My tagged pointer class will support either Generic* (tag = 0b000) or small integers (tag=0b001) for now (in the future maybe add unicode chars, and/or Cons* etc.). A rough sketch of what I...
1
2135
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 how to make a connect between to terminals. I was thinking using a hash table and each key is a linked list to store but I confused about how to parse the strings and even test if my cache even works?? This is the client file: #include...
0
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10211
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10045
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7409
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6673
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5299
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3562
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.