sizeof dataTypes at run time

R

Raman

Hi All,

Is ut possible to calculate size of any standard data types at run
time i.e without using sizeof() operator

Thanks and Regards,
Raman
 
C

CBFalconer

Raman said:
Is ut possible to calculate size of any standard data types at
run time i.e without using sizeof() operator

Maybe this should be in the FAQ under the heading 'Foolish
Questions'?
 
R

Richard Heathfield

Raman said:
Hi All,

Is ut possible to calculate size of any standard data types at run
time i.e without using sizeof() operator

Sure - and this is especially easy since there is no such thing as the
sizeof() operator. All you have to do is instantiate the type, and use
the sizeof operator on the resulting object. This way, you avoid even
the appearance of using what someone has mistakenly described as a
sizeof() operator.
 
J

jamsheedm

Hi All,

Is ut possible to calculate size of any standard data types at run
time i.e without using sizeof() operator

Thanks and Regards,
Raman

Hi All,

U can get a sizeof any variable without even knowing what is its type
or where it is used. The way is explained below as an example.

int a;
printf("size of variable is %d\n",(&a+1)-(&a));

This is applicable for all datatypes(even for array).

Regards
jamsheed m([email protected])
 
R

Richard Heathfield

(e-mail address removed) said:
U can get a sizeof any variable without even knowing what is its type
or where it is used. The way is explained below as an example.

int a;
printf("size of variable is %d\n",(&a+1)-(&a));

This is applicable for all datatypes(even for array).

Wrong wrong wrong wrong wrong, as you'd know if you'd tried the above
code yourself.

The correct answer, always, is: use sizeof. That is what it is *for*.
 
R

ramana

Yes it's possible to know. use this macro

#define SIZE(x) ( ((char*)(&x + 1)) - (char*)&(x) )

where 'x' is the variable.

sample.c:
......................
#include <stdio.h>

#define SIZE(x) ( ((char*)(&x + 1)) - (char*)&(x) )

int main()
{
char c;
int i;
float f;
double d;

printf(" sizes of char = %d \t int = %d \t float = %d \t double = %d
\n", SIZE(c), SIZE(i), SIZE(f), SIZE(d));

return 0;
}
..............................


Regards,
Ram
 
R

ramana

yes. Its possible. Use the below macro.

sample program:
...................................
#include <stdio.h>

#define SIZE(x) ( ((char*)(&x + 1)) - (char*)&(x) )

int main()
{
char c;
int i;
float f;
double d;

printf(" sizes of char = %d \t int = %d \t float = %d \t double = %d
\n", SIZE(c), SIZE(i), SIZE(f), SIZE(d));

return 0;
}
..........................................

Regards,
Ram
 
R

Richard Heathfield

ramana said:
yes. Its possible. Use the below macro.

sample program:
..................................
#include <stdio.h>

#define SIZE(x) ( ((char*)(&x + 1)) - (char*)&(x) )

That doesn't calculate the size of a type.

Proof:

#include <stdio.h>

#define SIZE(x) ( ((char*)(&x + 1)) - (char*)&(x) )

int main(void)
{
printf("%d\n", (int)SIZE(double));
return 0;
}

foo.c: In function `main':
foo.c:7: parse error before `double'
foo.c:7: parse error before `)'
 
C

CBFalconer

.... snip ...

U can get a sizeof any variable without even knowing what is its
type or where it is used. The way is explained below as an example.

int a;
printf("size of variable is %d\n",(&a+1)-(&a));

This is applicable for all datatypes(even for array).

U doesn't read this newsgroup any more, and even if he did, he
would be ill-advised to take your advice, which involves undefined
and implementation defined behaviour. I am NOT referring to the
missing includes.
 
J

Jack Klein

On 26 Mar 2007 06:14:28 -0700, "ramana" <[email protected]>
wrote in comp.lang.c:

Do not top post in this group, it is considered rude. Text you add
should be after or interspersed with quoted text you are responding
to. I have reformatted your post and added my comments.
Yes it's possible to know. use this macro

#define SIZE(x) ( ((char*)(&x + 1)) - (char*)&(x) )

where 'x' is the variable.

sample.c:
.....................
#include <stdio.h>

#define SIZE(x) ( ((char*)(&x + 1)) - (char*)&(x) )

The macro is correct.
int main()
{
char c;
int i;
float f;
double d;

printf(" sizes of char = %d \t int = %d \t float = %d \t double = %d
\n", SIZE(c), SIZE(i), SIZE(f), SIZE(d));

The printf() statement above is non-portable at best, and causes
undefined behavior at worst. The subtraction of two pointers yields a
result with a type of ptrdiff_t, which is a signed integer type but
not necessarily a signed int.

If ptrdiff_t happens to be signed int on your implementation, or
signed short (unlikely!), then the code will do what you want.

On implementations where ptrdiff_t is a wider type than int, and I
work with some, the code produces undefined behavior.

The obvious solution is to add a cast to int in your macro. Less
obvious but slightly better is to add a cast to long, and change the
conversion specifiers to %ld.

Best, of course, is get rid of the macro and use sizeof.
return 0;
}
.............................

Kindly learn how to post a proper signature line. It is separated
from the body of your message by the character string "-- " appearing
on a line by themselves. See my signature below for an example.
Regards,
Ram

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 
J

Jack Klein

Hi All,

U can get a sizeof any variable without even knowing what is its type
or where it is used. The way is explained below as an example.

U doesn't post here anymore, we killed him.
int a;
printf("size of variable is %d\n",(&a+1)-(&a));

The printf() call above produces undefined behavior on systems where
the underlying type of ptrdiff_t is wider than int. Many such systems
exist.
This is applicable for all datatypes(even for array).

Regards
jamsheed m([email protected])

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 
J

jamsheedm

(e-mail address removed) said:




Wrong wrong wrong wrong wrong, as you'd know if you'd tried the above
code yourself.

The correct answer, always, is: use sizeof. That is what it is *for*.


its possible
printf("size of variable is %d\n",(char*)(&a+1)-(char*)(&a));
This will print the proper size of variable.
 
R

Richard Heathfield

(e-mail address removed) said:

printf("size of variable is %d\n",(char*)(&a+1)-(char*)(&a));
This will print the proper size of variable.

He didn't ask for the size of a variable, but for the size of a type.
Check the subject line: "sizeof dataTypes at run time".
 
J

jamsheedm

(e-mail address removed) said:



He didn't ask for the size of a variable, but for the size of a type.
Check the subject line: "sizeof dataTypes at run time".

Then there is no way to know the size of the type directly without
crating a variable of that type.
we can never use sizeof(double) or something like this.
Please let me know if anyone knows the solution for that.
 
R

ramana

Sorry for my misunderstanding.
Then there is no way to know the size of the type directly without
crating a variable of that type.
we can never use sizeof(double) or something like this.
Please let me know if anyone knows the solution for that.

No its possible to know the size using the type itself. Check the
sample program below.

#include <stdio.h>

#define SIZE(x) ( (long)(((char*)((x*)0 + 1)) - (char*)((x*)0)) )

int main()
{

printf(" sizes of char = %ld \t int = %ld \t float = %ld \t double =
%ld\n", SIZE(char), SIZE(int), SIZE(float), SIZE(double));

return 0;
}

I am new to posting to the groups. So i don't know the rules/practices
to follow. Please don't hesitate to tell me any that sort of stuff.

Thanks in advance.
 
C

Chris Dollin

ramana said:
No its possible to know the size using the type itself. Check the
sample program below.

#include <stdio.h>

#define SIZE(x) ( (long)(((char*)((x*)0 + 1)) - (char*)((x*)0)) )

int main()
{

printf(" sizes of char = %ld \t int = %ld \t float = %ld \t double =
%ld\n", SIZE(char), SIZE(int), SIZE(float), SIZE(double));

return 0;
}

The expression `(x*)0 + 1` (for whatever type `x`) is undefined.

So, although it may work on some (perhaps many; even, perhaps, all)
implementations, it's not guaranteed to work at all.

--
Is it a bird? It is a plane? No, it's: http://hpl.hp.com/conferences/juc2007/
"He's dead, Jim, but not as we know it." Unsaid /Trek/

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England
 
R

Richard Heathfield

(e-mail address removed) said:
Then there is no way to know the size of the type directly without
crating a variable of that type.

Sure there is. It's called sizeof.
we can never use sizeof(double) or something like this.

Yes, we can.
 

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,174
Messages
2,570,940
Members
47,484
Latest member
JackRichard

Latest Threads

Top