473,890 Members | 1,213 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to implement sizeof operator

Can anybody tell me how sizeof operator internally implemented. In my
project i want to implemnent my own sizeof operator function (like
mysizeof). How i can write the code? Please help me.

Aug 23 '06 #1
8 7623
Mallesh wrote:
Can anybody tell me how sizeof operator internally implemented. In my
project i want to implemnent my own sizeof operator function (like
mysizeof). How i can write the code? Please help me.
Look back through this group's archives, this has been asked before.

--
Ian Collins.
Aug 23 '06 #2
Mallesh wrote:
Can anybody tell me how sizeof operator internally implemented. In my
project i want to implemnent my own sizeof operator function (like
mysizeof). How i can write the code? Please help me.
It's implemented the same way the `(int)' operator
is implemented: By the compiler.

--
Eric Sosman
es*****@acm-dot-org.invalid
Aug 23 '06 #3
Mallesh wrote:
Can anybody tell me how sizeof operator internally implemented. In my
project i want to implemnent my own sizeof operator function (like
mysizeof). How i can write the code? Please help me.
Look at the Type* typ(int et, Type *d); function at,
http://cm.bell-labs.com/sources/plan...c/cmd/cc/sub.c

It distributes the size of types based on an array , ewidth[et],
which is just a table for the various sizes of types for the
current platform and assigns that to the type..

(Ofcourse you must have some way of deducing the type of
operands and expressions, this code is part of Ken Thompsons
C compiler. C compilers are good at doing just that.)
Aug 23 '06 #4
Mallesh wrote:
>
Can anybody tell me how sizeof operator internally implemented.
"Magic".

Since it is the compiler that determines the layout and size of any
type (whether built-in, or user-defined), it knows the exact size of
any and all types.
In my
project i want to implemnent my own sizeof operator function (like
mysizeof).
Why? Is there some reason, aside from "my instructor told me to",
to not use the sizeof operator, which was designed specifically for
this purpose?
How i can write the code? Please help me.
This comes up every now and then in this group. (Perhaps the
teacher assigns this "problem" several times a year?) Check the
archives for numerous "solutions" .

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer .h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th***** ********@gmail. com>
Aug 23 '06 #5

Mallesh wrote:
Can anybody tell me how sizeof operator internally implemented. In my
project i want to implemnent my own sizeof operator function (like
mysizeof). How i can write the code? Please help me.
There are two major cases:

(1) You have a variable or type name and at *compile time* you need
the size of the variable or type. You can "sort of" get a number
that's loosely related to the size by trickery, but not recommended by
purists. Hint: macros can declare variables. Variables are often, but
not always, snuggled together in memory.

(2) You are passed an arbitrary variable address at *run time* and you
want to find the size of the variable. Not doable except in very
special circumstances, such as the variable was allocated by malloc()
and you have a malloc() hook.

But in general this question is better asked in some other group, this
group is for purists, and what you're asking is in no way "pure".

Aug 23 '06 #6
Ancient_Hacker wrote:
>
Mallesh wrote:
Can anybody tell me how sizeof operator internally implemented.
(2) You are passed an arbitrary variable address at
*run time* and you
want to find the size of the variable. Not doable except in very
special circumstances, such as the variable was allocated by malloc()
and you have a malloc() hook.
If you had an operation that could tell you
how many bytes were allocated for an object by malloc,
then that operation wouldn't be doing what sizeof does,
because sizeof can't do that.
But in general this question is better asked in some other group, this
group is for purists, and what you're asking is in no way "pure".
--
pete
Aug 24 '06 #7
"Ancient_Hacker " <gr**@comcast.n etwrote:
Mallesh wrote:
Can anybody tell me how sizeof operator internally implemented. In my
project i want to implemnent my own sizeof operator function (like
mysizeof). How i can write the code? Please help me.

There are two major cases:

(1) You have a variable or type name and at *compile time* you need
the size of the variable or type. You can "sort of" get a number
that's loosely related to the size by trickery, but not recommended by
purists. Hint: macros can declare variables. Variables are often, but
not always, snuggled together in memory.
Then you're thinking of the wrong tricks.

If you have an object, you can, not sort of, but definitely, get the
size of that object in bytes, without resorting to any non-ISO or
system-dependent hacks. Hint: [1].
If you have a type, you can almost equally simply get the size of that
type, by declaring a single object of that type, and then resorting to
the previous trick. The real snag here will be that you will have to
find a variable name that is safe to declare in this macro. You can find
such an identifier with almost, but not quite, 100% certainty.

What you cannot do in ISO C is declare a macro which does both the
object case _and_ the type case. The preprocessor doesn't know the
difference between foo and foo, where the first foo was declared using
int foo; and the second using typedef int foo;.
But in general this question is better asked in some other group, this
group is for purists, and what you're asking is in no way "pure".
No, at compile-time, it can be done in pure ISO C. The real problem with
the question is: why? Nobody who has access to sizeof itself (and that
is everybody who uses ISO C) should need to fake it.

Richard
Aug 24 '06 #8

Nils O. Selåsdal wrote:
Mallesh wrote:
Can anybody tell me how sizeof operator internally implemented. In my
project i want to implemnent my own sizeof operator function (like
mysizeof). How i can write the code? Please help me.

Look at the Type* typ(int et, Type *d); function at,
http://cm.bell-labs.com/sources/plan...c/cmd/cc/sub.c

It distributes the size of types based on an array , ewidth[et],
which is just a table for the various sizes of types for the
current platform and assigns that to the type..

(Ofcourse you must have some way of deducing the type of
operands and expressions, this code is part of Ken Thompsons
C compiler. C compilers are good at doing just that.)
Thanks
Nils O. Selåsdal

Aug 24 '06 #9

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

Similar topics

2
397
by: dharmesh Gupta | last post by:
Hi All, the following program gives out put as 4 1 but i am unable to understand it , coulf anyone help me out Thanks Dharmesh
5
2243
by: Aloo | last post by:
dear fellas, this is anupam aka aloo and i have small problem the problem ststes :-- >> IMPLEMENT THE 'sizeof' OPERATOR IN C. i.e find the size of any data without using 'sizeof' operator. >> One soluton is that if the pointer to the data is given then even if we don't know the type we cn just increment the pointer and see by how much it is incremented as the c compiler itself increments it according to its size.
12
36740
by: Christopher Pragash | last post by:
Hello All, Is there an equivalent of SizeOf operator in VB.NET? What I'm trying to achieve is to determine at runtime what the size of hashtable is in terms of the memory it occupies. Thanks in advance, chris
12
1899
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 assume that the interpreter does conform to the range values for, say, type int, but allocates storage for the variables based on their value. So, for two variables foo and bar int foo = 0; /* interpreter allocates two bytes */ int bar =...
12
2430
by: sonu | last post by:
#include<stdio.h> main() { int x=10,y; y=sizeof(++x); printf("x=%d\ny=%d\n",x,y); } Oput Put
32
2608
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
4
17188
by: vijay | last post by:
hi, if you see assembly then sizeof operator has sizeof(type) or sizeof(variable) at compile time. How does C compiler gets value at compiler time.? How can we implement sizeof operator? the implementation as macro is as below #define sizeof_op1(val) (&val +1 ) - &val // for variable ex
0
2041
by: citystud | last post by:
I am trying to convert the c++ code to C#, but find difficulty to convert the overloading operator. Here is the source code. I don't really know how to implement the operator = and operator () in C#. #ifndef _OFFMAT_HPP_ #define _OFFMAT_HPP_ #include "fdct_wrapping_inc.hpp" using std::ostream; using std::istream;
10
3475
by: maheshgupta024 | last post by:
Can anyone tell me how to implement sizeof in C language
0
11227
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
10462
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
9630
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
8016
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
7169
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
6041
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4676
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
2
4270
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3276
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.