E
Eugeny Myunster
Hello all,
How can i emulate sizeof()
only for integers?
How can i emulate sizeof()
only for integers?
Eugeny said:Hello all,
How can i emulate sizeof()
only for integers?
Kenneth Brody said: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.
Hello all,
How can i emulate sizeof()
only for integers?
Hello all,
How can i emulate sizeof()
only for integers?
Of course, it is lunacy to use something else when you want a size,
since the sizeof operator is perfect for that task.
That having been said, you can make an array of 2 objects, and get an
unsigned char pointer to the first and second object and subtract the
pointer difference to find the size.
E.g.
#define StupidEvilTwistedSizeof(type,ans) { \
type a[2]; \
unsigned char *start = (unsigned char *)&a[0]; \
unsigned char *end = (unsigned char *)&a[1]; \
*ans=end-start;}
#include <stdio.h>
#include <stdlib.h>
typedef struct thingy {
int a;
char b;
long c;
} thingy;
int main(void)
{
size_t size;
size_t *psize = &size;
StupidEvilTwistedSizeof(char, psize);
printf("I am ashamed to say that the size of char is %u\n",
(unsigned) size);
StupidEvilTwistedSizeof(short, psize);
printf("I am ashamed to say that the size of short is %u\n",
(unsigned) size);
StupidEvilTwistedSizeof(long, psize);
printf("I am ashamed to say that the size of long is %u\n",
(unsigned) size);
StupidEvilTwistedSizeof(long long, psize);
printf("I am ashamed to say that the size of long long is %u\n",
(unsigned) size);
StupidEvilTwistedSizeof(float, psize);
printf("I am ashamed to say that the size of float is %u\n",
(unsigned) size);
StupidEvilTwistedSizeof(double, psize);
printf("I am ashamed to say that the size of double is %u\n",
(unsigned) size);
StupidEvilTwistedSizeof(long double, psize);
printf("I am ashamed to say that the size of long double is %u\n",
(unsigned) size);
StupidEvilTwistedSizeof(thingy, psize);
printf("I am ashamed to say that the size of thingy is %u\n",
(unsigned) size);
return 0;}
/*
Possible output:
I am ashamed to say that the size of char is 1
I am ashamed to say that the size of short is 2
I am ashamed to say that the size of long is 4
I am ashamed to say that the size of long long is 8
I am ashamed to say that the size of float is 4
I am ashamed to say that the size of double is 8
I am ashamed to say that the size of long double is 8
I am ashamed to say that the size of thingy is 12
*/
Of course, it is lunacy to use something else when you want a size,
since the sizeof operator is perfect for that task.That having been said, you can make an array of 2 objects, and get an
unsigned char pointer to the first and second object and subtract the
pointer difference to find the size.E.g.
#define StupidEvilTwistedSizeof(type,ans) { \
type a[2]; \
unsigned char *start = (unsigned char *)&a[0]; \
unsigned char *end = (unsigned char *)&a[1]; \
*ans=end-start;}#include <stdio.h>
#include <stdlib.h>typedef struct thingy {
int a;
char b;
long c;} thingy;int main(void)
{
size_t size;
size_t *psize = &size;
StupidEvilTwistedSizeof(char, psize);
printf("I am ashamed to say that the size of char is %u\n",
(unsigned) size);
StupidEvilTwistedSizeof(short, psize);
printf("I am ashamed to say that the size of short is %u\n",
(unsigned) size);
StupidEvilTwistedSizeof(long, psize);
printf("I am ashamed to say that the size of long is %u\n",
(unsigned) size);
StupidEvilTwistedSizeof(long long, psize);
printf("I am ashamed to say that the size of long long is %u\n",
(unsigned) size);
StupidEvilTwistedSizeof(float, psize);
printf("I am ashamed to say that the size of float is %u\n",
(unsigned) size);
StupidEvilTwistedSizeof(double, psize);
printf("I am ashamed to say that the size of double is %u\n",
(unsigned) size);
StupidEvilTwistedSizeof(long double, psize);
printf("I am ashamed to say that the size of long double is %u\n",
(unsigned) size);
StupidEvilTwistedSizeof(thingy, psize);
printf("I am ashamed to say that the size of thingy is %u\n",
(unsigned) size);return 0;}/*
Possible output:
I am ashamed to say that the size of char is 1
I am ashamed to say that the size of short is 2
I am ashamed to say that the size of long is 4
I am ashamed to say that the size of long long is 8
I am ashamed to say that the size of float is 4
I am ashamed to say that the size of double is 8
I am ashamed to say that the size of long double is 8
I am ashamed to say that the size of thingy is 12
*/
Updated version:
#define StupidEvilTwistedSizeof(type,ans) { \
type a[2]; \
unsigned char *start = (unsigned char *)&a[0]; \
unsigned char *end = (unsigned char *)&a[1]; \
*ans=end-start;}
#include <stdio.h>
#include <stdlib.h>
typedef struct thingy {
int a;
char b;
long c;
} thingy;
typedef double (*PFI) ( double );
int main(void)
{
size_t size;
size_t *psize = &size;
StupidEvilTwistedSizeof(char, psize);
printf("I am ashamed to say that the size of char is %u\n",
(unsigned) size);
StupidEvilTwistedSizeof(short, psize);
printf("I am ashamed to say that the size of short is %u\n",
(unsigned) size);
StupidEvilTwistedSizeof(long, psize);
printf("I am ashamed to say that the size of long is %u\n",
(unsigned) size);
StupidEvilTwistedSizeof(long long, psize);
printf("I am ashamed to say that the size of long long is %u\n",
(unsigned) size);
StupidEvilTwistedSizeof(float, psize);
printf("I am ashamed to say that the size of float is %u\n",
(unsigned) size);
StupidEvilTwistedSizeof(double, psize);
printf("I am ashamed to say that the size of double is %u\n",
(unsigned) size);
StupidEvilTwistedSizeof(long double, psize);
printf("I am ashamed to say that the size of long double is %u\n",
(unsigned) size);
StupidEvilTwistedSizeof(thingy, psize);
printf("I am ashamed to say that the size of thingy is %u\n",
(unsigned) size);
StupidEvilTwistedSizeof(char *, psize);
printf("I am ashamed to say that the size of char * is %u\n",
(unsigned) size);
StupidEvilTwistedSizeof(int *, psize);
printf("I am ashamed to say that the size of int * is %u\n",
(unsigned) size);
StupidEvilTwistedSizeof(long *, psize);
printf("I am ashamed to say that the size of long * is %u\n",
(unsigned) size);
StupidEvilTwistedSizeof(void *, psize);
printf("I am ashamed to say that the size of void * is %u\n",
(unsigned) size);
StupidEvilTwistedSizeof(PFI, psize);
printf("I am ashamed to say that the size of PFI is %u\n",
(unsigned) size);
StupidEvilTwistedSizeof(void **, psize);
printf("I am ashamed to say that the size of void ** is %u\n",
(unsigned) size);
return 0;}
/*
Possible output:
I am ashamed to say that the size of char is 1
I am ashamed to say that the size of short is 2
I am ashamed to say that the size of long is 4
I am ashamed to say that the size of long long is 8
I am ashamed to say that the size of float is 4
I am ashamed to say that the size of double is 8
I am ashamed to say that the size of long double is 8
I am ashamed to say that the size of thingy is 12
I am ashamed to say that the size of char * is 4
I am ashamed to say that the size of int * is 4
I am ashamed to say that the size of long * is 4
I am ashamed to say that the size of void * is 4
I am ashamed to say that the size of PFI is 4
I am ashamed to say that the size of void ** is 4
*/
You don't even need an array of two: just a single object,Of course, it is lunacy to use something else when you want a size,
since the sizeof operator is perfect for that task.
Concur.
That having been said, you can make an array of 2 objects, and get an
unsigned char pointer to the first and second object and subtract the
pointer difference to find the size.
The biggest problem with this method is that it works ONLY on types
and NOT on objects of a given type.
The sizeof operator works nicely on both.
It's not lunacy, if a woman tells you she'll sleep with you if can write
a program that calculates the size of an object without using the
built-in sizeof operator. It becomes a very worthwhile endeavor.
That's the problem with a lot of the answers given here. They lack
context.
It's not lunacy, if a woman tells you she'll sleep with you if can write
a program that calculates the size of an object without using the
built-in sizeof operator. It becomes a very worthwhile endeavor.
That's the problem with a lot of the answers given here. They lack
context.
Kenny McCormack said:I think (against the tide, I understand) that you should assume that a
poster has a reason for posting (as he does).
Yes, but for most of these sizeof() questions the context seems most
likely to be "I can't do the questions on my C test".
-- Richard
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.
--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody |www.hvcomputer.com| #include |
| kenbrody/at\spamcop.net |www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
Kenny said:"Passing the class" is probably an even more important - and more deserving
reason - than "I wanna get laid".
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.