473,756 Members | 1,881 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
90 8465
On 2006-06-18, Xicheng Jia <xi*****@gmail. com> wrote:
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! :-)


Yes, any standard function could do it that way; the compiler has control
over all the libraries included with it. Not only that, but with C99 VLA's,
sizeof /cannot/ be computer at compile time in some cases.

--
Andrew Poelstra < http://www.wpsoftware.net/blog >
To email me, use "apoelstra" at the above address.
I know that area of town like the back of my head.
Jun 18 '06 #11
ada
Andrew Poelstra <ap*******@loca lhost.localdoma in> writes:
On 2006-06-18, Xicheng Jia <xi*****@gmail. com> wrote:
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! :-)


Yes, any standard function could do it that way; the compiler has control
over all the libraries included with it. Not only that, but with C99 VLA's,
sizeof /cannot/ be computer at compile time in some cases.

Why?
--
Andrew Poelstra < http://www.wpsoftware.net/blog >
To email me, use "apoelstra" at the above address.
I know that area of town like the back of my head.

Jun 18 '06 #12
Andrew Poelstra wrote:
On 2006-06-18, Xicheng Jia <xi*****@gmail. com> wrote:
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! :-)


Yes, any standard function could do it that way; the compiler has control
over all the libraries included with it. Not only that, but with C99 VLA's,
sizeof /cannot/ be computer at compile time in some cases.


Sorry, I should have said that the operand of sizeof() is interpolated
at compile-time. so i++ in sizeof(i++) does not increment i and the
following code prints 'i = 1' instead of 'i = 2'..

#include<stdio. h>
int main()
{
int i = 1, sz;
sz = sizeof(i++);
printf("i = %d\n", i);
return 0;
}

In ISO C, Macro is a different thing from function. and I am not sure
if inline function can do things like sizeof() does though . any hints
about this, thanks..:)

Xicheng

Jun 18 '06 #13
pn*********@gma il.com writes:
How can we write a function, which functionality is similar to sizeof
function
Why would you want to do that?
any one send me source code


Nope.

The only plausible reason to do something like this is as a homework
assignment (though it's a particularly bad one). If this is the case
here, we're certainly not going to do your homework for you.

If you have some other reason, tell us what it is. Tell us what
you're *really* trying to accomplish, and we might be able to help you
find a better solution. (Based on your question, a better solution is
obvious: use the sizeof operator, that's what it's for.)

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jun 18 '06 #14
ada said:
Andrew Poelstra <ap*******@loca lhost.localdoma in> writes:


<snip>
Not only that, but with C99
VLA's, sizeof /cannot/ be computer at compile time in some cases.

Why?


Here's one reason:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
puts("How many array elements would you like?");
char buf[32];
if(fgets(buf, sizeof buf, stdin) != NULL)
{
unsigned long n = strtoul(buf, NULL, 10);
if(n > 0)
{
int arr[n];
/* ... */

There is simply no way that the compiler can know at compile time how many
elements the user will ask for at run time.

--
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 #15
Madhav wrote:

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?


The result of the sizeof operator must be a constant expression,
whenever the operand isn't a variable length array.

--
pete
Jun 19 '06 #16
Richard Heathfield wrote:
There is simply no way that the compiler can know at compile time how many
elements the user will ask for at run time.


Is following the inconsistent between the standard and implementations ?

#include <stdio.h>

int main(void)
{
int len = 10;
char a[len];

switch (sizeof a)
{
case 10:
printf("%s", "10 elements");
break;
default:
printf("<unknow >");
break;
}

fflush(stdout);

return 0;
}

$ gcc test.c
$ ./a.out
10 elements$
$

lovecreatesbeau ty

/*quoting begins*/
6.6 Constant expressions
2 A constant expression can be evaluated during translation rather than
runtime, <snip>

6.8.4.2 The switch statement
3 The expression of each case label shall be an integer constant
expression <snip>
/*quoting ends*/

Jun 19 '06 #17
In article <df************ ********@bt.com >,
Richard Heathfield <in*****@invali d.invalid> wrote:
Madhav said:
Richard Heathfield wrote:
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?


--------
template<class T>
std::size_t my_sizeof(const T&)
{
assert(HAS_FLAT _MEMORY_SPACE_A ND_UNCHECKED_PO INTER_ARITHMETI C);
T* x=0;
T* y=x+1;
return static_cast<std ::size_t>(
static_cast<cha r*>(y)-static_cast<cha r*>(x));
}
--------

If you'll excuse me while I go find a pointy stick to poke my sigmonster
with, I'll be right back...
dave

--
Dave Vandervies dj******@csclub .uwaterloo.ca
Suggesting to comp.lang.c that they use C++ compilers and inline assembly is
like suggesting to an American that you can get nice and warm by burning an
American flag. --Richard Heathfield in comp.lang.c
Jun 19 '06 #18

Dave Vandervies wrote:
template<class T>
std::size_t my_sizeof(const T&)


C language has no template, namespace, :: and function overloading
things. Even you can't find any word same as "template", "namespace" or
"overload" in the standard document.

lovecreatesbeau ty

Jun 19 '06 #19
In article <11************ *********@g10g2 000cwb.googlegr oups.com>,
lovecreatesbeau ty <lo************ ***@gmail.com> wrote:

Dave Vandervies wrote:
template<class T>
std::size_t my_sizeof(const T&)
C language has no template, namespace, :: and function overloading
things. Even you can't find any word same as "template", "namespace" or
"overload" in the standard document.


If you hadn't stopped reading there, and had been willing and able to
engage your brain while continuing to read, you would probably have
realized that I was aware of that.

(It also subtly fails to meet RH's spec, while providing enough
information to allow the OP to write (non-function) code to meet his
original spec without doing his homework for him. Which, really, is
all anybody could ask for in a homework thread.)
dave
(procrastinatin g doing homework of my own)

--
Dave Vandervies dj******@csclub .uwaterloo.caYup. Now, how does one fit this into a four-line .sig?

"People are stupid."
--Arvid Grotting and Mike Sphar in the Scary Devil Monastery
Jun 19 '06 #20

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

Similar topics

3
3555
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
2469
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
9235
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
3025
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
1939
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
2405
by: Christopher C. Stacy | last post by:
Some people say sizeof(type) and other say sizeof(variable). Why?
8
2538
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
2899
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
9287
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
10046
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...
1
9857
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8723
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...
0
6542
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
5155
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
5318
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3817
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
2677
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.