Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Knowing size of a data type

Reply
Thread Tools

Knowing size of a data type

 
 
Nitin
Guest
Posts: n/a
 
      09-02-2003
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


 
Reply With Quote
 
 
 
 
Nils Petter Vaskinn
Guest
Posts: n/a
 
      09-02-2003
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
 
Reply With Quote
 
 
 
 
Irrwahn Grausewitz
Guest
Posts: n/a
 
      09-02-2003
"Nitin" <(E-Mail Removed)> wrote in
<bj1oek$(E-Mail Removed)>:
>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.
 
Reply With Quote
 
Martin Dickopp
Guest
Posts: n/a
 
      09-02-2003
"Nitin" <(E-Mail Removed)> 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
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      09-02-2003
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
 
Reply With Quote
 
Atreya, Chaitanya
Guest
Posts: n/a
 
      09-02-2003


Martin Dickopp wrote:
> "Nitin" <(E-Mail Removed)> 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


 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      09-03-2003
On Tue, 2 Sep 2003 15:08:24 +0530, "Nitin" <(E-Mail Removed)>
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
 
Reply With Quote
 
Nitin
Guest
Posts: n/a
 
      09-03-2003
Its not a question of staying away. Just trying to find out alternate ways
of doing it.


"Irrwahn Grausewitz" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "Nitin" <(E-Mail Removed)> wrote in
> <bj1oek$(E-Mail Removed)>:
> >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.



 
Reply With Quote
 
Nitin
Guest
Posts: n/a
 
      09-03-2003
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



 
Reply With Quote
 
Martin Dickopp
Guest
Posts: n/a
 
      09-03-2003
"Nitin" <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
knowing the mime type of downloaded image Dan Gelder Javascript 2 01-29-2008 04:43 AM
Knowing the buffer size in C3750 Catalyst switch query.cdac@gmail.com Cisco 0 07-11-2007 09:41 AM
Knowing the memory size of an object Philipp Java 1 06-25-2007 04:48 PM
Initialize array without knowing it size? saturnlee@yahoo.com Java 11 10-16-2006 01:02 PM
Create an object on the fly knowing just an object type Just D. ASP .Net 2 05-07-2005 03:06 AM



Advertisments