Find the size of a datatype

R

Rahul K

Hello all
Suppose I am given a datatype say X in C. How can i find its size
without declaring a variable or pointer variable of that type, and of
course without using sizeof operator.
 
C

Chris Dollin

Rahul said:
Suppose I am given a datatype say X in C. How can i find its size
without declaring a variable or pointer variable of that type, and of
course without using sizeof operator.

Who cares? Use `sizeof`, that's what it's /for/.
 
V

Vladimir Oka

Rahul said:
Hello all
Suppose I am given a datatype say X in C. How can i find its size
without declaring a variable or pointer variable of that type, and of
course without using sizeof operator.

Why "of course", and why without `sizeof`. That's what it's for, after
all.

One recourse remaining to you seems to be grepping/eyeballing for the
type definition in the code (or looking into the C Standard for
built-in types) and figuring it "manually".
 
P

pete

Rahul said:
Hello all
Suppose I am given a datatype say X in C. How can i find its size
without declaring a variable or pointer variable of that type, and of
course without using sizeof operator.

You can do it with lucky guess.
 
I

Ico

Rahul K said:
Hello all
Suppose I am given a datatype say X in C. How can i find its size
without declaring a variable or pointer variable of that type, and of
course without using sizeof operator.

Such hypotethical questions can only come from homework. Like everybody
here, I'm glad to help you doing your assignments. Try this :

----------------------------------------------------------

#include <stdio.h>

/* Place your type in 'X' */

#define X char

/* Some constants */

#define Q (Y-Y)
#define R (Y/Y)
#define S (V Q)
#define T (S+R)
#define U (Z T)
#define V (X W)
#define W (*)
#define Y (0xffff-1)
#define Z (int)

int main(void)
{
printf("size of X is %d\n", U);

return 0;
}
 
S

Sunil Varma

Ico said:
Such hypotethical questions can only come from homework. Like everybody
here, I'm glad to help you doing your assignments. Try this :

----------------------------------------------------------

#include <stdio.h>

/* Place your type in 'X' */

#define X char

/* Some constants */

#define Q (Y-Y)
#define R (Y/Y)
#define S (V Q)
#define T (S+R)
#define U (Z T)
#define V (X W)
#define W (*)
#define Y (0xffff-1)
#define Z (int)

int main(void)
{
printf("size of X is %d\n", U);

return 0;
}

Thank you.
It's very nice of you.

Regards
Sunil
 
K

Keith Thompson

Vladimir Oka said:
Why "of course", and why without `sizeof`. That's what it's for, after
all.

One recourse remaining to you seems to be grepping/eyeballing for the
type definition in the code (or looking into the C Standard for
built-in types) and figuring it "manually".

That's not possible in general. No amount of looking into the C
standard will tell you how big an int is.
 
S

santosh

Sunil said:
Thank you.
It's very nice of you.

If you care for clearing your subject, you'll try to understand the
above code before blindly submitting it for your homework.
 
R

Richard Heathfield

Keith Thompson said:
No amount of looking into the C standard will tell you how big an int is.

You can deduce from the C standard that an int is sizeof(int) bytes in size.

The OP's obsession with avoiding sizeof should be ignored; frankly, it's
ridiculous.
 
R

Roberto Waltman

Richard Heathfield said:
Keith Thompson said:
You can deduce from the C standard that an int is sizeof(int) bytes in size.
The OP's obsession with avoiding sizeof should be ignored; frankly, it's
ridiculous.

My guess is that the source of this inquire is a tricky interview
question, with the expected answer being something along the lines of
this:

#include <stdio.h>
#include <stdlib.h>

int how_not_to_get_the_sizeof_int()
{
int array[2];
char *i1 = (char *) array;
char *i2 = (char *) (array+1);
return i2 - i1;
}

int main()
{
printf("The size of int seems to be: %d\n",
how_not_to_get_the_sizeof_int());
return EXIT_SUCCESS;
}

i.e., something that shows some understanding of C and machine
architecture, but would not be used in production code.
 
R

Roberto Waltman

Richard Heathfield said:
Keith Thompson said:
You can deduce from the C standard that an int is sizeof(int) bytes in size.
The OP's obsession with avoiding sizeof should be ignored; frankly, it's
ridiculous.

My guess is that the source of this inquire is a tricky interview
question, with the expected answer being something along the lines of
this:

#include <stdio.h>
#include <stdlib.h>

int how_not_to_get_the_sizeof_int()
{
int array[2];
char *i1 = (char *) array;
char *i2 = (char *) (array+1);
return i2 - i1;
}

int main()
{
printf("The size of int seems to be: %d\n",
how_not_to_get_the_sizeof_int());
return EXIT_SUCCESS;
}

i.e., something that shows some understanding of C and machine
architecture, but would not be used in production code.

Oops! I missed the part about "not declaring a variable of the
type..." No valid answer then, as far as I know.
 
K

Keith Thompson

Richard Heathfield said:
Keith Thompson said:

You can deduce from the C standard that an int is sizeof(int) bytes in size.
True.

The OP's obsession with avoiding sizeof should be ignored; frankly, it's
ridiculous.

Of course it's ridiculous, but the OP needs to understand that it's
ridiculous. Simply ignoring it doesn't accomplish that.
 
P

pete

Ico said:
Such hypotethical questions can only come from homework.
Like everybody
here, I'm glad to help you doing your assignments. Try this :

----------------------------------------------------------

#include <stdio.h>

/* Place your type in 'X' */

#define X char

/* Some constants */

#define Q (Y-Y)
#define R (Y/Y)
#define S (V Q)
#define T (S+R)
#define U (Z T)
#define V (X W)
#define W (*)
#define Y (0xffff-1)
#define Z (int)

int main(void)
{
printf("size of X is %d\n", U);

return 0;
}

I don't think that the result of adding one
to a null pointer constant, is defined.
 
J

jjf

Roberto said:
My guess is that the source of this inquire is a tricky interview
question, with the expected answer being something along the lines of
this:

#include <stdio.h>
#include <stdlib.h>

int how_not_to_get_the_sizeof_int()
{
int array[2];
char *i1 = (char *) array;
char *i2 = (char *) (array+1);
return i2 - i1;
}

int main()
{
printf("The size of int seems to be: %d\n",
how_not_to_get_the_sizeof_int());
return EXIT_SUCCESS;
}

i.e., something that shows some understanding of C and machine
architecture, but would not be used in production code.

Oops! I missed the part about "not declaring a variable of the
type..." No valid answer then, as far as I know.

.... where the OP asked
Suppose I am given a datatype say X in C. How can i find its size
without declaring a variable or pointer variable of that type, and of
course without using sizeof operator.

Your approach is fine - you don't have any variables of the forbidden
types. You're using a variable of type <array of X>.
 
I

Ico

[ ..snip questionable code.. ]
I don't think that the result of adding one to a null pointer
constant, is defined.

I'm not sure about this either, but 0 is not NULL, so this might be
valid. But this stuff is what you get if you insist on not using
sizeof(), ofcourse.
 
F

Flash Gordon

Ico said:
[ ..snip questionable code.. ]
I don't think that the result of adding one to a null pointer
constant, is defined.

I'm not sure about this either, but 0 is not NULL, so this might be

NULL is a macro. If 0 is used in a pointer context then it give you a
null pointer. Adding to a null pointer or a null pointer constant is
undefined. All this has been discussed on the group several times.
valid. But this stuff is what you get if you insist on not using
sizeof(), ofcourse.

Which is why sizeof and offsetof exist. They can't be implemented portably.
 
V

vire

you can see U as (int)((X *)0+1);
0 is converted to a pointer point to a X.
and the value of that pointer is 0(not NULL,it was just point to the
address 0).
then we add 1 to that pointer.so it point to the next X. the address
the pointer point to now is 0+sizeof(X);
so we get an address if we use (int)((X*)0+1)
maybe you can try these:
(X*)1+1;
(X*)1+2;
(X*)2+1;
 
F

Flash Gordon

vire wrote:

Please do not top post. Your reply belongs under the text you are
replying to.
you can see U as (int)((X *)0+1);
0 is converted to a pointer point to a X.
and the value of that pointer is 0(not NULL,it was just point to the
address 0).

No, it does *not* point to address 0. (X *)0 is a null pointer (NULL is
a macro not a value). Pointer arithmetic is only defined for pointers to
objects or 1 past the end of an object (both initial value and result
having to fit). The null pointer explicitly does *not* point at an object.
then we add 1 to that pointer.so it point to the next X. the address
the pointer point to now is 0+sizeof(X);
so we get an address if we use (int)((X*)0+1)
maybe you can try these:
(X*)1+1;
(X*)1+2;
(X*)2+1;

None of those are defined by the C language.

<snip text which should have been above>
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc

Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php
 

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
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top