help me out

U

ujjwal

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.
 
I

Ian Collins

ujjwal said:
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?
 
B

B. Augestad

ujjwal said:
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
 
U

ujjwal

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
 
R

Richard

Ian Collins said:
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.
 
A

Anu

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)));
 
F

Flash Gordon

ujjwal wrote, On 05/06/07 10:53:
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.
 
B

B. Augestad

ujjwal said:
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
 
R

Richard Heathfield

ujjwal said:

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.
 
R

Richard Heathfield

ujjwal said:
Thnx Annu

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

Flash Gordon

ujjwal wrote, On 05/06/07 11:38:
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.
 
M

Mark McIntyre

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
 
U

user923005

ujjwal wrote, On 05/06/07 11:38:



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;
}
 
U

user923005

#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.
 
F

Flash Gordon

user923005 wrote, On 06/06/07 04:29:
#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 :)
 
N

neildferguson

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;
}
 
H

Harald van =?UTF-8?B?RMSzaw==?=

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
 
N

neildferguson

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
 
E

Eric Sosman

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.
 

Ask a Question

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.

Ask a Question

Members online

Forum statistics

Threads
474,183
Messages
2,570,968
Members
47,524
Latest member
ecomwebdesign

Latest Threads

Top