By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,820 Members | 1,187 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,820 IT Pros & Developers. It's quick & easy.

help me out

P: n/a
Hi all,

You are given have a datatype, say X in C. The requirement is to get
the size of the datatype, without declaring a variable or a pointer
variable of that type, And, of course without using sizeof operator !

please tell me how this can be done.

Jun 5 '07 #1
Share this Question
Share on Google+
20 Replies


P: n/a
ujjwal wrote:
Hi all,

You are given have a datatype, say X in C. The requirement is to get
the size of the datatype, without declaring a variable or a pointer
variable of that type, And, of course without using sizeof operator !

please tell me how this can be done.
Why would you want to, except for a homework assignment?

--
Ian Collins.
Jun 5 '07 #2

P: n/a
ujjwal wrote:
Hi all,

You are given have a datatype, say X in C. The requirement is to get
the size of the datatype, without declaring a variable or a pointer
variable of that type, And, of course without using sizeof operator !

please tell me how this can be done.
Why? Any sane programmer would use sizeof() so this must be homework.
Why should we do your homework?

Bjørn
Jun 5 '07 #3

P: n/a
On Jun 5, 2:47 pm, "B. Augestad" <b...@metasystems.nowrote:
ujjwal wrote:
Hi all,
You are given have a datatype, say X in C. The requirement is to get
the size of the datatype, without declaring a variable or a pointer
variable of that type, And, of course without using sizeof operator !
please tell me how this can be done.

Why? Any sane programmer would use sizeof() so this must be homework.
Why should we do your homework?

Bjørn
Hi
you all are right but this not a homework I saw this problem on net
and tried to solve it
and after trying I posted it here

ujjwal

Jun 5 '07 #4

P: n/a
Ian Collins <ia******@hotmail.comwrites:
ujjwal wrote:
> Hi all,

You are given have a datatype, say X in C. The requirement is to get
the size of the datatype, without declaring a variable or a pointer
variable of that type, And, of course without using sizeof operator !

please tell me how this can be done.
Why would you want to, except for a homework assignment?
If he doesn't know, surely asking here is indeed doing his homework
assuming he learns something? It's not like he's asking you to spot the
"ready made" bug in a coding assignment.
Jun 5 '07 #5

P: n/a
Anu
On Jun 5, 2:39 pm, ujjwal <singhujj...@gmail.comwrote:
Hi all,

You are given have a datatype, say X in C. The requirement is to get
the size of the datatype, without declaring a variable or a pointer
variable of that type, And, of course without using sizeof operator !

please tell me how this can be done.

one method :-

//to find size of double :-
printf("%d\n",(int)((char *)((double *)0 + 1)));

Jun 5 '07 #6

P: n/a
On Jun 5, 3:11 pm, Anu <annu_iyer2...@yahoo.co.inwrote:
On Jun 5, 2:39 pm, ujjwal <singhujj...@gmail.comwrote:
Hi all,
You are given have a datatype, say X in C. The requirement is to get
the size of the datatype, without declaring a variable or a pointer
variable of that type, And, of course without using sizeof operator !
please tell me how this can be done.

one method :-

//to find size of double :-
printf("%d\n",(int)((char *)((double *)0 + 1)));
Thnx Annu

Jun 5 '07 #7

P: n/a
ujjwal wrote:
On Jun 5, 2:47 pm, "B. Augestad" <b...@metasystems.nowrote:
>ujjwal wrote:
>> Hi all,
You are given have a datatype, say X in C. The requirement is to get
the size of the datatype, without declaring a variable or a pointer
variable of that type, And, of course without using sizeof operator !
please tell me how this can be done.
Why? Any sane programmer would use sizeof() so this must be homework.
Why should we do your homework?

Bjørn

Hi
you all are right but this not a homework I saw this problem on net
and tried to solve it
and after trying I posted it here

ujjwal
Don't spend time solving stupid problems, spend time learning how to
code properly in C.

Bjørn

Jun 5 '07 #8

P: n/a
ujjwal wrote, On 05/06/07 10:53:
On Jun 5, 2:47 pm, "B. Augestad" <b...@metasystems.nowrote:
>ujjwal wrote:
>> Hi all,
You are given have a datatype, say X in C. The requirement is to get
the size of the datatype, without declaring a variable or a pointer
variable of that type, And, of course without using sizeof operator !
please tell me how this can be done.
Why? Any sane programmer would use sizeof() so this must be homework.
Why should we do your homework?

Hi
you all are right but this not a homework I saw this problem on net
and tried to solve it
and after trying I posted it here
I suggest you search this group for all the reasons attempting to do so
is a bad idea. Basically, sizeof is part of the language because there
is no portable way to implement a sizeof replacement.

If you want people to tell you what is wrong with your attempt the you
can post it here.
--
Flash Gordon
Jun 5 '07 #9

P: n/a
ujjwal said:

<the usual sizeof-sans-sizeof question>
you all are right but this not a homework I saw this problem on net
and tried to solve it
The correct solution is that the requirement is broken. Use sizeof.
That's what it's for.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jun 5 '07 #10

P: n/a
ujjwal said:
On Jun 5, 3:11 pm, Anu <annu_iyer2...@yahoo.co.inwrote:
>>
//to find size of double :-
printf("%d\n",(int)((char *)((double *)0 + 1)));

Thnx Annu
Why are you thanking him for giving you incorrect information? You could
have thought up a wrong answer all by yourself.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jun 5 '07 #11

P: n/a
ujjwal wrote, On 05/06/07 11:38:
On Jun 5, 3:11 pm, Anu <annu_iyer2...@yahoo.co.inwrote:
>On Jun 5, 2:39 pm, ujjwal <singhujj...@gmail.comwrote:
>> Hi all,
You are given have a datatype, say X in C. The requirement is to get
the size of the datatype, without declaring a variable or a pointer
variable of that type, And, of course without using sizeof operator !
please tell me how this can be done.
one method :-

//to find size of double :-
printf("%d\n",(int)((char *)((double *)0 + 1)));

Thnx Annu
Don't thank him too much. It invokes undefined behaviour by doing
arithmetic on a null pointer meaning anything can happen, including
crashing your computer or causing it to mutate in to a badly constructed
ZX80. Also, the conversion from pointer to int is not guaranteed to be
to be a simple byte count (e.g. it is not on the Cray Vector machine)
and even if it is null pointers might convert to something other than an
int value of 0 (although an integer *constant* of zero is a special case
guaranteed to be a null pointer constant).

That's just what I can think of off the top of my head.

Both of you search the group for the repeated long discussions of this.
--
Flash Gordon
Jun 5 '07 #12

P: n/a
On Tue, 05 Jun 2007 11:59:07 +0200, in comp.lang.c , Richard
<rg****@gmail.comwrote:
>Ian Collins <ia******@hotmail.comwrites:
>ujjwal wrote:
>> Hi all,

You are given have a datatype, say X in C. The requirement is to get
the size of the datatype, without declaring a variable or a pointer
variable of that type, And, of course without using sizeof operator !

please tell me how this can be done.
Why would you want to, except for a homework assignment?

If he doesn't know, surely asking here is indeed doing his homework
assuming he learns something? It's not like he's asking you to spot the
"ready made" bug in a coding assignment.
It kinda depends. When someone makes a post which quite literally is a
copypaste of their homework eg they "Q1. write a code fragment to do
xxxx", especially with daft conditions such as "do not use any
function from stdlib.h", they should get short shrift for being
insufficiently motivated even to make an effort.

OTOH If they post something which shows some effort has been made to
solve the problem and ask for pointers, then help should be
forthcoming. .
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Jun 5 '07 #13

P: n/a
On Jun 5, 6:12 am, Flash Gordon <s...@flash-gordon.me.ukwrote:
ujjwal wrote, On 05/06/07 11:38:
On Jun 5, 3:11 pm, Anu <annu_iyer2...@yahoo.co.inwrote:
On Jun 5, 2:39 pm, ujjwal <singhujj...@gmail.comwrote:
> Hi all,
You are given have a datatype, say X in C. The requirement is to get
the size of the datatype, without declaring a variable or a pointer
variable of that type, And, of course without using sizeof operator !
please tell me how this can be done.
one method :-
//to find size of double :-
printf("%d\n",(int)((char *)((double *)0 + 1)));
Thnx Annu

Don't thank him too much. It invokes undefined behaviour by doing
arithmetic on a null pointer meaning anything can happen, including
crashing your computer or causing it to mutate in to a badly constructed
ZX80. Also, the conversion from pointer to int is not guaranteed to be
to be a simple byte count (e.g. it is not on the Cray Vector machine)
and even if it is null pointers might convert to something other than an
int value of 0 (although an integer *constant* of zero is a special case
guaranteed to be a null pointer constant).

That's just what I can think of off the top of my head.

Both of you search the group for the repeated long discussions of this.
#define ICKY_MACRO_SIZEOF(some_type, some_size) \
{ \
some_type arr[2]; \
some_size = (char*)&arr[1] - (char*)&arr[0]; \
}

#include <stdio.h>
typedef struct foobar {
int foo;
double bar;
char barfoo[7];
} foobar_t;

int main(void)
{
size_t some_size;
ICKY_MACRO_SIZEOF(char, some_size);
printf("ICKY_MACRO_SIZEOF for char = %u\n", (unsigned) some_size);
ICKY_MACRO_SIZEOF(short, some_size);
printf("ICKY_MACRO_SIZEOF for short = %u\n", (unsigned)
some_size);
ICKY_MACRO_SIZEOF(int, some_size);
printf("ICKY_MACRO_SIZEOF for int = %u\n", (unsigned) some_size);
ICKY_MACRO_SIZEOF(long, some_size);
printf("ICKY_MACRO_SIZEOF for long = %u\n", (unsigned) some_size);
ICKY_MACRO_SIZEOF(long long, some_size);
printf("ICKY_MACRO_SIZEOF for long long = %u\n", (unsigned)
some_size);
ICKY_MACRO_SIZEOF(size_t, some_size);
printf("ICKY_MACRO_SIZEOF for size_t = %u\n", (unsigned)
some_size);
ICKY_MACRO_SIZEOF(float, some_size);
printf("ICKY_MACRO_SIZEOF for float = %u\n", (unsigned)
some_size);
ICKY_MACRO_SIZEOF(double, some_size);
printf("ICKY_MACRO_SIZEOF for double = %u\n", (unsigned)
some_size);
ICKY_MACRO_SIZEOF(long double, some_size);
printf("ICKY_MACRO_SIZEOF for long double = %u\n", (unsigned)
some_size);
ICKY_MACRO_SIZEOF(char *, some_size);
printf("ICKY_MACRO_SIZEOF for char * = %u\n", (unsigned)
some_size);
ICKY_MACRO_SIZEOF(long double *, some_size);
printf("ICKY_MACRO_SIZEOF for long double * = %u\n", (unsigned)
some_size);
ICKY_MACRO_SIZEOF(foobar_t, some_size);
printf("ICKY_MACRO_SIZEOF for foobar_t = %u\n", (unsigned)
some_size);
ICKY_MACRO_SIZEOF(foobar_t *, some_size);
printf("ICKY_MACRO_SIZEOF for foobar_t * = %u\n", (unsigned)
some_size);
return 0;
}
Jun 6 '07 #14

P: n/a
On Jun 5, 8:20 pm, user923005 <dcor...@connx.comwrote:
On Jun 5, 6:12 am, Flash Gordon <s...@flash-gordon.me.ukwrote:
#define ICKY_MACRO_SIZEOF(some_type, some_size) \
{ \
some_type arr[2]; \
some_size = (char*)&arr[1] - (char*)&arr[0]; \
}
Of course, it fails in at least two distinct instances I can think of.
Jun 6 '07 #15

P: n/a
user923005 wrote, On 06/06/07 04:29:
On Jun 5, 8:20 pm, user923005 <dcor...@connx.comwrote:
>On Jun 5, 6:12 am, Flash Gordon <s...@flash-gordon.me.ukwrote:
#define ICKY_MACRO_SIZEOF(some_type, some_size) \
{ \
some_type arr[2]; \
some_size = (char*)&arr[1] - (char*)&arr[0]; \
}

Of course, it fails in at least two distinct instances I can think of.
That's good, it saves me having to think of instances in which it fails :-)
--
Flash Gordon
Jun 6 '07 #16

P: n/a
On Tue, 05 Jun 2007 02:39:03 -0700, ujjwal <si*********@gmail.comwrote:
Hi all,

You are given have a datatype, say X in C. The requirement is to get
the size of the datatype, without declaring a variable or a pointer
variable of that type, And, of course without using sizeof operator !

please tell me how this can be done.
Here's a suggestion. Would a function definition strictly be considered a
variable? Are there any types that would fail?

#include <stdio.h>

typedef struct
{
int i;
double d;
} S;

static void dummy(){;}
#define GETSIZE(T) (int)((char *)&((T*)dummy)[1] - (char *)&((T*)dummy)[0])
int main()
{
printf("int size = %d\n", GETSIZE(int));
printf("double size = %d\n", GETSIZE(double));
printf("char size = %d\n", GETSIZE(char));
printf("S size = %d\n", GETSIZE(S));
return 0;
}

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Jun 8 '07 #17

P: n/a
ne***********@hotmail.com wrote:
On Tue, 05 Jun 2007 02:39:03 -0700, ujjwal <si*********@gmail.comwrote:
> Hi all,

You are given have a datatype, say X in C. The requirement is to get
the size of the datatype, without declaring a variable or a pointer
variable of that type, And, of course without using sizeof operator !

please tell me how this can be done.

Here's a suggestion. Would a function definition strictly be considered a
variable? Are there any types that would fail?
Depending on the implementation, all types can fail. What you're doing isn't
allowed in standard C.
#include <stdio.h>

typedef struct
{
int i;
double d;
} S;

static void dummy(){;}
#define GETSIZE(T) (int)((char *)&((T*)dummy)[1] - (char
#*)&((T*)dummy)[0])
int main()
{
printf("int size = %d\n", GETSIZE(int));
printf("double size = %d\n", GETSIZE(double));
printf("char size = %d\n", GETSIZE(char));
printf("S size = %d\n", GETSIZE(S));
return 0;
}
getsize.c: In function ‘main’:
getsize.c:13: error: ISO C forbids conversion of function pointer to object
pointer type
getsize.c:13: error: ISO C forbids conversion of function pointer to object
pointer type
getsize.c:14: error: ISO C forbids conversion of function pointer to object
pointer type
getsize.c:14: error: ISO C forbids conversion of function pointer to object
pointer type
getsize.c:15: error: ISO C forbids conversion of function pointer to object
pointer type
getsize.c:15: error: ISO C forbids conversion of function pointer to object
pointer type
getsize.c:16: error: ISO C forbids conversion of function pointer to object
pointer type
getsize.c:16: error: ISO C forbids conversion of function pointer to object
pointer type
Jun 8 '07 #18

P: n/a
On Fri, 08 Jun 2007 22:07:13 +0200, Harald van D?k <tr*****@gmail.comwrote:
>ne***********@hotmail.com wrote:
>On Tue, 05 Jun 2007 02:39:03 -0700, ujjwal <si*********@gmail.comwrote:
>> Hi all,

You are given have a datatype, say X in C. The requirement is to get
the size of the datatype, without declaring a variable or a pointer
variable of that type, And, of course without using sizeof operator !

please tell me how this can be done.

Here's a suggestion. Would a function definition strictly be considered a
variable? Are there any types that would fail?

Depending on the implementation, all types can fail. What you're doing isn't
allowed in standard C.
>#include <stdio.h>

typedef struct
{
int i;
double d;
} S;

static void dummy(){;}
#define GETSIZE(T) (int)((char *)&((T*)dummy)[1] - (char
#*)&((T*)dummy)[0])
int main()
{
printf("int size = %d\n", GETSIZE(int));
printf("double size = %d\n", GETSIZE(double));
printf("char size = %d\n", GETSIZE(char));
printf("S size = %d\n", GETSIZE(S));
return 0;
}

getsize.c: In function ‘main’:
getsize.c:13: error: ISO C forbids conversion of function pointer to object
pointer type
getsize.c:13: error: ISO C forbids conversion of function pointer to object
pointer type
getsize.c:14: error: ISO C forbids conversion of function pointer to object
pointer type
getsize.c:14: error: ISO C forbids conversion of function pointer to object
pointer type
getsize.c:15: error: ISO C forbids conversion of function pointer to object
pointer type
getsize.c:15: error: ISO C forbids conversion of function pointer to object
pointer type
getsize.c:16: error: ISO C forbids conversion of function pointer to object
pointer type
getsize.c:16: error: ISO C forbids conversion of function pointer to object
pointer type
Thank you. This is interesting: in the ISO C99 standard Annex J.5.7
(Portability Issues/Common Extensions) it says "A pointer to a function may be
cast to a pointer to an object or to void, allowing a function to be inspected
or modified." Does that apply to this case?

Neil Ferguson

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Jun 8 '07 #19

P: n/a
ne***********@hotmail.com wrote On 06/08/07 17:02,:
>
Thank you. This is interesting: in the ISO C99 standard Annex J.5.7
(Portability Issues/Common Extensions) it says "A pointer to a function may be
cast to a pointer to an object or to void, allowing a function to be inspected
or modified." Does that apply to this case?
The capability is described as a "common extension,"
that is, as something that many implementations allow
but is not guaranteed by the language. Some compilers
may let you get away with it, but if another compiler
forbids it you have no grounds for complaint.

IMHO, the correct answer to the O.P.'s question
You are given have a datatype, say X in C. The requirement is to get
the size of the datatype, without declaring a variable or a pointer
variable of that type, And, of course without using sizeof operator !
.... is to use `sizeof(X)'. The question doesn't seek to
explore one's knowledge of C, but one's ability to spot
and reject a stupid specification.

--
Er*********@sun.com
Jun 8 '07 #20

P: n/a
Eric Sosman <Er*********@sun.comwrites:
[...]
IMHO, the correct answer to the O.P.'s question
>You are given have a datatype, say X in C. The requirement is to get
the size of the datatype, without declaring a variable or a pointer
variable of that type, And, of course without using sizeof operator !

... is to use `sizeof(X)'. The question doesn't seek to
explore one's knowledge of C, but one's ability to spot
and reject a stupid specification.
I don't *entirely* agree. There are (clumsy) solutions involving
pointer arithmetic. Being able to determine the size of a type
without using sizeof does demonstrate a knowledge of how pointer
arithmetic works in C -- and there could be problems requiring similar
solutions for which the language doesn't provide a built-in operator.
Determining the alignment for a type is one example; offsetof would be
another if it weren't already declared in <stddef.h>.

But of course there is absolutely no reason to determine the size of a
type without using sizeof in the real world. That's what sizeof is
for, after all.

IMHO, a better response to the OP's question is:

*Why* can't you use sizeof?

I wouldn't be inclined to provide a solution until that question is
answered.

--
Keith Thompson (The_Other_Keith) 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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 8 '07 #21

This discussion thread is closed

Replies have been disabled for this discussion.