473,782 Members | 2,458 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
15 3283

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**********@ya hoo.co.in wrote:
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.
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**********@ya hoo.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+integ er, 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**********@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.]
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**********@ya hoo.co.in schrieb:
Peter Nilsson wrote:
ju**********@ya hoo.co.in wrote:
Sharath wrote:
ju**********@ya hoo.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*********@gm ail.comwrote:
ju**********@ya hoo.co.in wrote:
santosh wrote:
ju**********@ya hoo.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
11667
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
2593
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
2136
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
9641
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
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10146
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...
0
9944
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7494
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
6735
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();...
1
4044
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2875
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.