Re: How can i emulate sizeof()
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Wed, 30 Apr 2008 11:41:13 -0700
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"
.
- References:
- How can i emulate sizeof()
- From: Eugeny Myunster
- Re: How can i emulate sizeof()
- From: Kenneth Brody
- How can i emulate sizeof()
- Prev by Date: hot new stories
- Next by Date: Mixed case?
- Previous by thread: Re: How can i emulate sizeof()
- Next by thread: hot new stories
- Index(es):
Relevant Pages
|