473,322 Members | 1,714 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Knowing size of a data type

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
Nov 13 '05 #1
18 9436
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
Nov 13 '05 #2
"Nitin" <ni*****@bell-labs.com> wrote in
<bj********@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.
Nov 13 '05 #3
"Nitin" <ni*****@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
Nov 13 '05 #4
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
Nov 13 '05 #5


Martin Dickopp wrote:
"Nitin" <ni*****@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


Nov 13 '05 #6
On Tue, 2 Sep 2003 15:08:24 +0530, "Nitin" <ni*****@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
Nov 13 '05 #7
Its not a question of staying away. Just trying to find out alternate ways
of doing it.
"Irrwahn Grausewitz" <ir*****@freenet.de> wrote in message
news:rt********************************@4ax.com...
"Nitin" <ni*****@bell-labs.com> wrote in
<bj********@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.

Nov 13 '05 #8
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

Nov 13 '05 #9
"Nitin" <ni*****@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
Nov 13 '05 #10
On 03 Sep 2003 14:48:36 +0200
Martin Dickopp <ex*************@zero-based.org> wrote:
"Nitin" <ni*****@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#;+,)/'///*");}
Nov 13 '05 #11
Pieter Droogendijk wrote:

On 03 Sep 2003 14:48:36 +0200
Martin Dickopp <ex*************@zero-based.org> wrote:
"Nitin" <ni*****@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.

--
Er*********@sun.com
Nov 13 '05 #12
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
Nov 13 '05 #13
Jon
your retentive and closed minded comments are not that helpfull. You waste
your own time with pointless appends. idiot
"Default User" <fi********@company.com> wrote in message
news:3F***************@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

Nov 13 '05 #14
Jon <jo********@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 (pa*****@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
Nov 13 '05 #15
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
Nov 13 '05 #16
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

Nov 13 '05 #17
Default User <fi********@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 (pa*****@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
Nov 13 '05 #18

"Nitin" <ni*****@bell-labs.com> wrote in message
news:bj********@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.

Nov 13 '05 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Tarundeep | last post by:
hi, let us say it is a 32 bit processor then the size of the pointer is 32 bits. now i want to know what would be the size of the class with vtable pointer in it , that is it has few virtual...
2
by: Kums | last post by:
What is the maximum permissible size of a database? Is there any limitation. What is the maximum # of tablespace's allowed in a database? Thanks for your response.
22
by: Wynand Winterbach | last post by:
I think every C programmer can relate to the frustrations that malloc allocated arrays bring. In particular, I've always found the fact that the size of an array must be stored separately to be a...
1
by: Saranjit Singh | last post by:
Is there any way I can enumerate the properties of an object at runtime and assign values to each property of the object depending on its data type? For primitive data types, this is easily done...
4
by: tshad | last post by:
I am having trouble with links in my DataGrid. I have Links all over my page set to smaller and they are consistant all over the page in both Mozilla and IE, except for the DataGrid. Here is a...
2
by: Just D. | last post by:
All, Do we have a simple way to Create an object on the fly knowing just an object type? The usual design-time way is to write a code something like this: CObjectType obj = new CObjectType();...
26
by: alberto | last post by:
Hi. Im newbie in C language. I have a binary file with many character arrays of 50 character defined as char array But in some cases, many of these 50 characters are not being used. I would...
8
by: redefined.horizons | last post by:
I would like to have an array declaration where the size of the array is dependent on a variable. Something like this: /* Store the desired size of the array in a variable named "array_size". */...
7
by: Christof Warlich | last post by:
Hi, the subject says it all: I need to instantiate an array of objects where each object "knows" its arrary index. So far, this is easy as long as index is not a compile-time constant: class ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.