473,725 Members | 2,248 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sizeof

Hi,
How can we write a function, which functionality is similar to sizeof
function
any one send me source code
Reddy

Jun 18 '06 #1
90 8432
pn*********@gma il.com said:
Hi,
How can we write a function, which functionality is similar to sizeof
function


We can't, because sizeof is not a function. (And there's a good reason why
it's not a function.)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jun 18 '06 #2
Richard Heathfield wrote:
pn*********@gma il.com said:
Hi,
How can we write a function, which functionality is similar to sizeof
function


We can't, because sizeof is not a function. (And there's a good reason why
it's not a function.


Can you tell us that reason, Richard?

Regards,
Madhav.

Jun 18 '06 #3
Madhav said:
Richard Heathfield wrote:
pn*********@gma il.com said:
> Hi,
> How can we write a function, which functionality is similar to sizeof
> function


We can't, because sizeof is not a function. (And there's a good reason
why it's not a function.


Can you tell us that reason, Richard?


Spend a little time trying to write such a function, and you'll discover it
for yourself quickly enough. Hint: what type should its parameter be?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jun 18 '06 #4
pn*********@gma il.com wrote:
Hi,
How can we write a function, which functionality is similar to sizeof
function
any one send me source code


the problem is that the result of sizeof() is interpolated at compile
time. Can any function do it that way! :-)

Xicheng

Jun 18 '06 #5

"Xicheng Jia" <xi*****@gmail. com> wrote in message
pn*********@gma il.com wrote:
Hi,
How can we write a function, which functionality is similar to sizeof
function
any one send me source code


the problem is that the result of sizeof() is interpolated at compile
time. Can any function do it that way! :-)

A compiler is permitted to inline functions, and then to simplify code. A
really good compiler might simplify constructs like acos(0) to produce half
PI, but this is rare.
--
Buy my book 12 Common Atheist Arguments (refuted)
$1.25 download or $7.20 paper, available www.lulu.com/bgy1mm
Jun 18 '06 #6
Xicheng Jia posted:
Can any function do it that way! :-)

A macro function perhaps:
#define SizeOf(Type) \
( \
(const char*)128 - \
\
(const char*)( (const Type*)128 - 1 ) \
)
--

Frederick Gotham
Jun 18 '06 #7

Richard Heathfield wrote:
Spend a little time trying to write such a function, and you'll discover it
for yourself quickly enough. Hint: what type should its parameter be?


I think it is because we need C language to tell us the sizes of
various types and objects by sizeof. It's the purpose of sizeof itself
and why it is called the name operator. We have no alternative choice.

lovecreatesbeau ty

Jun 18 '06 #8
Frederick Gotham schrieb:
Xicheng Jia posted:
Can any function do it that way! :-)

A macro function perhaps:
#define SizeOf(Type) \
( \
(const char*)128 - \
\
(const char*)( (const Type*)128 - 1 ) \
)


Undefined behaviour - your pointer most likely does not point to any
valid object. And even if it does on certain implementations , it makes
no sense in c.l.c as conversion from integer to pointer types is
implementation defined and thus not portable.

--
Marc Thrun
http://www.tekwarrior.de/
Jun 18 '06 #9
Marc Thrun posted:
Frederick Gotham schrieb:
Xicheng Jia posted:
Can any function do it that way! :-)

A macro function perhaps:
#define SizeOf(Type) \
( \
(const char*)128 - \
\
(const char*)( (const Type*)128 - 1 ) \
)


Undefined behaviour - your pointer most likely does not point to any
valid object. And even if it does on certain implementations , it makes
no sense in c.l.c as conversion from integer to pointer types is
implementation defined and thus not portable.

Yes I'm aware of that.

This thread isn't about anything serious; it's more of a little mind game
to see who can write a "sizeof".
--

Frederick Gotham
Jun 18 '06 #10

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

Similar topics

3
3553
by: Sunil Menon | last post by:
Dear All, A class having no member variables and only a method sizeof(object) will return 1byte in ANSI and two bytes in Unicode. I have the answer for this of how in works in ANSI. But I don't know it returns two bytes in UniCode. Please help... For ANSI: In ISO/ANSI C++ Standard, 5.3.3 § 1, it stays: "The sizeof operator yields the number of bytes in the object representation of its
2
2462
by: Xiangliang Meng | last post by:
Hi, all. What will we get from sizeof(a class without data members and virtual functions)? For example: class abnormity { public: string name() { return "abnormity"; }
19
9229
by: Martin Pohlack | last post by:
Hi, I have a funtion which shall compute the amount for a later malloc. In this function I need the sizes of some struct members without having an instance or pointer of the struct. As "sizeof(int)" is legal I assumed "sizeof(struct x.y)" to be legal too. But is is not: #include <dirent.h>
9
3021
by: M Welinder | last post by:
This doesn't work with any C compiler that I can find. They all report a syntax error: printf ("%d\n", (int)sizeof (char)(char)2); Now the question is "why?" "sizeof" and "(char)" have identical precedence and right-to-left parsing, so why isn't the above equivalent to printf ("%d\n", (int)sizeof ((char)(char)2));
7
1933
by: dam_fool_2003 | last post by:
#include<stdio.h> int main(void) { unsigned int a=20,b=50, c = sizeof b+a; printf("%d\n",c); return 0; } out put: 24
42
2398
by: Christopher C. Stacy | last post by:
Some people say sizeof(type) and other say sizeof(variable). Why?
8
2534
by: junky_fellow | last post by:
Consider the following piece of code: #include <stddef.h> int main (void) { int i, j=1; char c; printf("\nsize =%lu\n", sizeof(i+j));
4
292
by: cs | last post by:
#include <stdio.h> #include <stdlib.h> typedef struct{ unsigned a; unsigned *b; unsigned k; }tp; tp e, *ee;
5
2896
by: Francois Grieu | last post by:
Does this reliably cause a compile-time error when int is not 4 bytes ? enum { int_size_checked = 1/(sizeof(int)==4) }; Any better way to check the value of an expression involving sizeof before runtime ? I also have: { void check_foo_size(void);
0
8889
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
8752
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
9401
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
9116
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
8099
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
6702
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
6011
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
3228
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
2637
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.