Knowing size of a data type

  • Follow


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


0
Reply nitinma (5) 9/2/2003 9:38:24 AM

On Tue, 02 Sep 2003 15:08:24 +0530, Nitin wrote:

> 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
0
Reply no784 (309) 9/2/2003 10:08:03 AM


"Nitin" <nitinma@bell-labs.com> wrote in
<bj1oek$flh@netnews.proxy.lucent.com>:
>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'?

--
Air is water with holes in it.
0
Reply irrwahn (250) 9/2/2003 10:52:22 AM

"Nitin" <nitinma@bell-labs.com> writes:

> 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
0
Reply expires-nov2003 (64) 9/2/2003 11:13:06 AM

Nils Petter Vaskinn wrote:
> 
> On Tue, 02 Sep 2003 15:08:24 +0530, Nitin wrote:
> 
> > 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.

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

-- 
pete
0
Reply pfiland (6613) 9/2/2003 11:24:47 AM


Martin Dickopp wrote:
> "Nitin" <nitinma@bell-labs.com> writes:
> 
> 
>>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)'.
> 
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

> Martin

0
Reply atreya (5) 9/2/2003 3:16:08 PM

On Tue, 2 Sep 2003 15:08:24 +0530, "Nitin" <nitinma@bell-labs.com>
wrote in comp.lang.c:

> 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
0
Reply jackklein (3932) 9/3/2003 2:54:35 AM

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


"Irrwahn Grausewitz" <irrwahn@freenet.de> wrote in message
news:rts8lvshqlqb0es6abl9847k07nvkt1fg5@4ax.com...
> "Nitin" <nitinma@bell-labs.com> wrote in
> <bj1oek$flh@netnews.proxy.lucent.com>:
> >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'?
>
> --
> Air is water with holes in it.


0
Reply nitinma (5) 9/3/2003 12:20:42 PM

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

> > On Tue, 02 Sep 2003 15:08:24 +0530, Nitin wrote:
> >
> > 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.
>
> --
> pete


0
Reply nitinma (5) 9/3/2003 12:24:19 PM

"Nitin" <nitinma@bell-labs.com> writes:

> 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
0
Reply expires-nov2003 (64) 9/3/2003 12:48:36 PM

On 03 Sep 2003 14:48:36 +0200
Martin Dickopp <expires-nov2003@zero-based.org> wrote:
> "Nitin" <nitinma@bell-labs.com> writes:
> 
> > 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.

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

-- 
char*x(c,k,s)char*k,*s;{if(!k)return*s-36?x(0,0,s+1):s;if(s)if(*s)c=10+(c?(x(
c,k,0),x(c,k+=*s-c,s+1),*k):(x(*s,k,s+1),0));else c=10;printf(&x(~0,0,k)[c-~-
c+"1"[~c<-c]],c);}main(){x(0,"^[kXc6]dn_eaoh$%c","-34*1'.+(,03#;+,)/'///*");}
0
Reply gin (83) 9/3/2003 12:57:53 PM

Pieter Droogendijk wrote:
> 
> On 03 Sep 2003 14:48:36 +0200
> Martin Dickopp <expires-nov2003@zero-based.org> wrote:
> > "Nitin" <nitinma@bell-labs.com> writes:
> >
> > > 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.
> 
> 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.

-- 
Eric.Sosman@sun.com
0
Reply Eric.Sosman (4228) 9/3/2003 1:41:48 PM

Nitin wrote:
> 
> 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
0
Reply first.last4 (288) 9/3/2003 5:41:37 PM

your retentive and closed minded comments are not that helpfull. You waste
your own time with pointless appends. idiot
"Default User" <first.last@company.com> wrote in message
news:3F5627D1.715C6C07@company.com...
> Nitin wrote:
> >
> > 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


0
Reply jon_martin (1) 9/6/2003 10:49:05 PM

Jon <jon_martin@uk.ibm.com> scribbled the following:
> your retentive and closed minded comments are not that helpfull. You waste
> your own time with pointless appends. idiot

*PLONK*

-- 
/-- Joona Palaste (palaste@cc.helsinki.fi) ---------------------------\
| 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
0
Reply palaste (2323) 9/8/2003 8:07:26 AM

Jon wrote:
> 
> 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
0
Reply first.last4 (288) 9/8/2003 5:30:39 PM

Nitin wrote:

> 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

0
Reply Thomas_MatthewsHatesSpam (304) 9/8/2003 6:18:22 PM

Default User <first.last@company.com> scribbled the following:
> Jon wrote:
>> 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.

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

-- 
/-- Joona Palaste (palaste@cc.helsinki.fi) ---------------------------\
| 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
0
Reply palaste (2323) 9/8/2003 6:45:52 PM

"Nitin" <nitinma@bell-labs.com> wrote in message
news:bj1oek$flh@netnews.proxy.lucent.com...
>
> 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.



0
Reply news061 (13) 9/13/2003 6:55:33 PM

18 Replies
29 Views

(page loaded in 0.355 seconds)


Reply: