Knowing size of a data type

N

Nitin

Hi,

Without using sizeof, is there a way to get to know the size of any data
type on that OS ... ???

one way I could think of is to take diff of the addresses of two variables
defined consecutively.
Any other way .....????

thanks,
nitin
 
N

Nils Petter Vaskinn

Hi,

Without using sizeof, is there a way to get to know the size of any data
type on that OS ... ???

one way I could think of is to take diff of the addresses of two variables
defined consecutively.
Any other way .....????

Don't think they are guaranteed to be consecutive in memory, and the
compiler may use padding between them anyway.

regards
NPV
 
I

Irrwahn Grausewitz

Nitin said:
Hi,

Without using sizeof, is there a way to get to know the size of any data
type on that OS ... ???
I'm curious: may I kindly ask why you would like to stay away from using
'sizeof'?
 
M

Martin Dickopp

Nitin said:
Hi,

Without using sizeof, is there a way to get to know the size of any data
type on that OS ... ???

one way I could think of is to take diff of the addresses of two variables
defined consecutively.

If by "consecutive" you mean "guaranteed to be consecutive", as in an array,
then yes. If `T' is a type, and `t' is a variable of type `T', then the
expression `(size_t)((char *)(&t + 1) - (char *)&t)' yields the same value
as `sizeof (T)'.

Martin
 
P

pete

Nils said:
Don't think they are guaranteed to be consecutive in memory, and the
compiler may use padding between them anyway.

If they are defined as consecutive elements in an array,
then they will be in consecutive memory without padding.
 
A

Atreya, Chaitanya

Martin said:
If by "consecutive" you mean "guaranteed to be consecutive", as in an array,
then yes. If `T' is a type, and `t' is a variable of type `T', then the
expression `(size_t)((char *)(&t + 1) - (char *)&t)' yields the same value
as `sizeof (T)'.
But this will fail to work for datatypes. It only works
for instances/objects/variables of datatypes.
Take a try to write one that works for datatypes......

Bye,
../Chaitanya Atreya
 
J

Jack Klein

Hi,

Without using sizeof, is there a way to get to know the size of any data
type on that OS ... ???

one way I could think of is to take diff of the addresses of two variables
defined consecutively.
Any other way .....????

thanks,
nitin

Yes, forget the nonsense and use sizeof, that's what its there for.
Are you the same person who asked the same question a few months ago?

Why not use sizeof?

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 
N

Nitin

Its not a question of staying away. Just trying to find out alternate ways
of doing it.
 
N

Nitin

I got another one:
------
int a;
printf("%x,%x",&a+1,&a);
------
now take diff of the two output values. This solution doesn't care whether
compiler does padding
between the addresses of two conseutively defined variables.

BTW, does compiler do padding .... ??????

thanks,
..nitin
 
M

Martin Dickopp

Nitin said:
int a;
printf("%x,%x",&a+1,&a);

This invokes undefined behavior. The 'x' format specifier expects an
`unsigned int', but you provide a pointer.

Martin
 
P

Pieter Droogendijk

This invokes undefined behavior. The 'x' format specifier expects an
`unsigned int', but you provide a pointer.

Indeed. %p is there specifically to print a pointer value in hexadecimal.
 
E

Eric Sosman

Pieter said:
Indeed. %p is there specifically to print a pointer value in hexadecimal.

"%p" is there specifically to print a pointer value, period.
Nothing is said about what the printed value should look like.
Also, the pointer value to be printed must be of type `void*',
not `int*' as above.
 
D

Default User

Nitin said:
Its not a question of staying away. Just trying to find out alternate ways
of doing it.

Don't top-post, your replies belong following properly trimmed quotes.

Your whole concept is dopey. Concentrate on learning the language
thoroughly, don't be sidetracked with bizarre alternate ways or odd
experiments. Once you have mastered the language, then you'll generally
have enough information that you'll be able to answer such questions
yourself.

You bascially are wasting your time and ours for no good reason.




Brian Rodenborn
 
J

Jon

your retentive and closed minded comments are not that helpfull. You waste
your own time with pointless appends. idiot
 
J

Joona I Palaste

Jon said:
your retentive and closed minded comments are not that helpfull. You waste
your own time with pointless appends. idiot

*PLONK*

--
/-- Joona Palaste ([email protected]) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"I am looking for myself. Have you seen me somewhere?"
- Anon
 
D

Default User

Jon said:
your retentive and closed minded comments are not that helpfull. You waste
your own time with pointless appends. idiot

I'll say again, DON'T TOP-POST!


Your efforts are foolish and non-instructive for a programmer who
doesn't yet know the language. You are wasting time that should be
devoted to that effort.




Brian Rodenborn
 
T

Thomas Matthews

Nitin said:
Hi,

Without using sizeof, is there a way to get to know the size of any data
type on that OS ... ???

one way I could think of is to take diff of the addresses of two variables
defined consecutively.
Any other way .....????

thanks,
nitin

There is no method to find the size of an OS's data type or structure
without having a declaration or definition of that type. Operating
Systems may have many types that they use.

As others have said, use the "sizeof" operator. The "sizeof" operator
is generally a constant that is evaluated at compile-time. The
difference between the addresses of two consecutive objects is
platform dependent and may be expressed in units of the type of
that object. Casting to an unsigned char or placing the address
into an integer may cause problems. Addresses aren't guaranteed
to be convertable to an integer. Anyway, the difference computation
is a run-time issue.

Remember that there are more than three operating systems. Some
may have displays, others not. Make no assumptions about a
platform, except for what the Standard describes.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
J

Joona I Palaste

I'll say again, DON'T TOP-POST!
Your efforts are foolish and non-instructive for a programmer who
doesn't yet know the language. You are wasting time that should be
devoted to that effort.

Um, Default, Jon isn't the OP here. He's not the one trying to avoid
use of sizeof.

--
/-- Joona Palaste ([email protected]) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"To doo bee doo bee doo."
- Frank Sinatra
 
B

Bob Jacobs

Nitin said:
Without using sizeof, is there a way to get to know the size
of any data type on that OS ... ???

Note that the size of a data type may not be the same for all compilers on a
particular OS.
 

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

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top