sizeof

P

pnreddy1976

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

Richard Heathfield

(e-mail address removed) 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.)
 
M

Madhav

Richard said:
(e-mail address removed) said:


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.
 
R

Richard Heathfield

Madhav said:
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?
 
X

Xicheng Jia

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
 
M

Malcolm

Xicheng Jia said:
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.
 
F

Frederick Gotham

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 ) \
)
 
L

lovecreatesbeauty

Richard said:
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.

lovecreatesbeauty
 
M

Marc Thrun

Frederick said:
Xicheng Jia posted:



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.
 
F

Frederick Gotham

Marc Thrun posted:
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".
 
A

Andrew Poelstra

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.
 
X

Xicheng Jia

Andrew said:
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
 
K

Keith Thompson

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.)
 
R

Richard Heathfield

ada said:

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.
 
P

pete

Madhav said:
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.
 
A

ada

Andrew Poelstra said:
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?
 
L

lovecreatesbeauty

Richard said:
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$
$

lovecreatesbeauty

/*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*/
 
D

Dave Vandervies

Madhav said:


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_AND_UNCHECKED_POINTER_ARITHMETIC);
T* x=0;
T* y=x+1;
return static_cast<std::size_t>(
static_cast<char*>(y)-static_cast<char*>(x));
}
--------

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


dave
 
L

lovecreatesbeauty

Dave said:
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.

lovecreatesbeauty
 
D

Dave Vandervies

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
(procrastinating doing homework of my own)

--
Dave Vandervies (e-mail address removed)
Yup. Now, how does one fit this into a four-line .sig?
"People are stupid."
--Arvid Grotting and Mike Sphar in the Scary Devil Monastery
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,184
Messages
2,570,979
Members
47,579
Latest member
CharaS3188

Latest Threads

Top