Re: How can i emulate sizeof()



Kenneth Brody <kenbrody@xxxxxxxxxxx> writes:
Eugeny Myunster wrote:

Hello all,
How can i emulate sizeof()
only for integers?

I'd really love to know which instructors keep giving this assignment.

Why do you want to "emulate sizeof", when sizeof exists just for this
purpose?

How about:

#define MySizeof(x) sizeof(x)

Now you can "emulate sizeof" by using "MySizeof(int)", for example.

Except that the macro, unlike the sizeof operator, requires a
parenthesized argument; you can't write "MySizeof 42".

I'd use:

#define MySizeof sizeof

which is equally useful (i.e., not at all).

A serious answer to the original poster: Why do you want to do this?
There are ways to determine the size of an object without using the
sizeof operator, but there's no point in using them; the sizeof
operator exists for exactly this purpose.

Q: How do I pound in a nail? I don't want to use a hammer.
A: Use a hammer anyway; that's what it's for.

Having said that, it's not entirely pointless *as an exercise*.
Figuring out how to compute the size of something without using sizeof
does present an opportunity to demonstrate that you understand certain
aspects of the language. The resulting piece of code isn't going to
be useful in itself, but then neither is the classic "hello, world"
program; if I really want to print "hello, world" on stdout, I'll use
echo. (There's a GNU "hello" program, for example, but it exists
purely as a demo; it's not actually useful.) That's just the nature
of homework assignments.

Oh, and speaking of homework assignments: DO IT YOURSELF. What will
you learn if we just give you the answer? We're willing to offer
hints if you try to do it yourself, but if you want us to solve the
problem for you, please give us your instructor's e-mail address so we
can submit our solutions directly.

--
Keith Thompson (The_Other_Keith) <kst-u@xxxxxxx>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
.



Relevant Pages

  • Re: CreateInstance
    ... I have read the link and I found as you mentioned sizeof never results in 0 ... How do you think for a class without data members, ... Virtual Base Classes ...
    (microsoft.public.vc.language)
  • Re: Passing Type as arugment for C.
    ... The sizeof operator is evaulated by the compiler and most of the ... magic with stdarg to deduce the size of the operands pushed onto the stack. ...
    (comp.programming)
  • Re: Regarding sizeof Operator
    ... "the operand of a sizeof operator is usually not evaluated" ... Although sizeof must evaluate the size of a variable length array under c99 ... "If the type of the operand is a variable length array type, ...
    (comp.lang.c)
  • Re: C Interpreter and sizeof operator
    ... standard that requires the sizeof operator to yield the same value for two different variables of the same type? ... "The sizeof operator yields the size of its operand, which may be an expression or the parenthesized name of a type. ... The size is determined from the type of the operand. ...
    (comp.lang.c)
  • Re: why still use C?
    ... in C90, and most of the world is still using C90, so they are best avoided, ... > while the sizeof that gets expressions as parameter do not ... The sizeof operator shall not be applied to an expression that has ... The sizeof operator yields the size of its operand, ...
    (comp.lang.c)