473,405 Members | 2,287 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

help me out

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
20 2267
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Dave | last post by:
After following Microsofts admonition to reformat my system before doing a final compilation of my app I got many warnings/errors upon compiling an rtf file created in word. I used the Help...
9
by: Tom | last post by:
A question for gui application programmers. . . I 've got some GUI programs, written in Python/wxPython, and I've got a help button and a help menu item. Also, I've got a compiled file made with...
6
by: wukexin | last post by:
Help me, good men. I find mang books that introduce bit "mang header files",they talk too bit,in fact it is my too fool, I don't learn it, I have do a test program, but I have no correct doing...
3
by: Colin J. Williams | last post by:
Python advertises some basic service: C:\Python24>python Python 2.4.1 (#65, Mar 30 2005, 09:13:57) on win32 Type "help", "copyright", "credits" or "license" for more information. >>> With...
7
by: Corepaul | last post by:
Missing Help Files When I enter "recordset" as the keyword and search the Visual Basic Help index, I get many topics of interest in the resulting list. But there isn't any information available...
5
by: Steve | last post by:
I have written a help file (chm) for a DLL and referenced it using Help.ShowHelp My expectation is that a developer using my DLL would be able to access this help file during his development time...
8
by: Mark | last post by:
I have loaded Visual Studio .net on my home computer and my laptop, but my home computer has an abbreviated help screen not 2% of the help on my laptop. All the settings look the same on both...
10
by: JonathanOrlev | last post by:
Hello everybody, I wrote this comment in another message of mine, but decided to post it again as a standalone message. I think that Microsoft's Office 2003 help system is horrible, probably...
1
by: trunxnirvana007 | last post by:
'UPGRADE_WARNING: Array has a new behavior. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"' 'UPGRADE_WARNING: Couldn't resolve...
0
by: hitencontractor | last post by:
I am working on .NET Version 2003 making an SDI application that calls MS Excel 2003. I added a menu item called "MyApp Help" in the end of the menu bar to show Help-> About. The application...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.