473,796 Members | 2,520 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sizeof dataTypes at run time

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

Mar 26 '07
34 2067
ilan pillemer <il**@pillemer. netwrites:
But this is what I did. I set the bit values of the basic types to ~0.
For unsigned basic types this gave me the range.
It won't necessarily work for unsigned integer types wider than
int. For these you'll need to either use the proper type for 0,
as in e.g. ~0UL. Or you can just assign -1 to an object of the
unsigned integer type, which doesn't require such additional
care.

By the way, it's best to think of C objects as having values, not
representations or "bit values". Most operations in C operate on
values, not on representations .
And signed the maximum negative value. I could also determine
thus by a comparison if 2's complement or 1's complement was
being used. I thus had my answer through direct computation.
I think that ~0 can actually yield a trap representation.
--
Comp-sci PhD expected before end of 2007
Seeking industrial or academic position *outside California* in 2008
Mar 31 '07 #31
ilan pillemer <il**@pillemer. netwrites:
"Army1987" <pl********@for .itwrites:
>"Raman" <ra***********@ gmail.comha scritto nel messaggio
news:11******* *************** @l75g2000hse.go oglegroups.com. ..
Is ut possible to calculate size of any standard data types at run
time i.e without using sizeof() operator

I believe this is an excercise in K&R2,p 36.

"Exercise 2-1. Write a program to determine the ranges of char,
short, int and long variables, both signed and unsigned, by printing
appropriate values from standard headers and by direct
computation. Harder if you compute them: determine the ranges of
various floating point types."
[...]

The exercise computes the *ranges* of the types (e.g., 0..65535), not
their sizes (e.g., 2 bytes or 16 bits).

(These are examples; I'm not assuming that a byte is 8 bits.)

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 31 '07 #32
Keith Thompson <ks***@mib.orgw rites:
ilan pillemer <il**@pillemer. netwrites:
"Army1987" <pl********@for .itwrites:
"Raman" <ra***********@ gmail.comha scritto nel messaggio
news:11******** **************@ l75g2000hse.goo glegroups.com.. .
Is ut possible to calculate size of any standard data types at run
time i.e without using sizeof() operator
I believe this is an excercise in K&R2,p 36.

"Exercise 2-1. Write a program to determine the ranges of char,
short, int and long variables, both signed and unsigned, by printing
appropriate values from standard headers and by direct
computation. Harder if you compute them: determine the ranges of
various floating point types."
[...]

The exercise computes the *ranges* of the types (e.g., 0..65535), not
their sizes (e.g., 2 bytes or 16 bits).

(These are examples; I'm not assuming that a byte is 8 bits.)

...hmm.. Would I look the at the number of bits in a char to determine
how many bits in a byte?

If this is a correct I could then fill a unsigned char with bits set
to one and then left shift them until all the bits are zeroed?

--
ilAn
Apr 1 '07 #33
ilan pillemer said:
Keith Thompson <ks***@mib.orgw rites:
<snip>
>>
The exercise computes the *ranges* of the types (e.g., 0..65535), not
their sizes (e.g., 2 bytes or 16 bits).

(These are examples; I'm not assuming that a byte is 8 bits.)


..hmm.. Would I look the at the number of bits in a char to determine
how many bits in a byte?
Yes, because a char is exactly one byte wide. Incidentally, it is also
exactly CHAR_BIT bits wide.
If this is a correct I could then fill a unsigned char with bits set
to one and then left shift them until all the bits are zeroed?
Yes, you could do that, or you could simply do this:

unsigned char x = 1;
int char_bit = 0;
while(x 0)
{
++char_bit;
x <<= 1;
}

or of course you could just use CHAR_BIT from <limits.h>.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Apr 1 '07 #34
Richard Heathfield <rj*@see.sig.in validwrites:
ilan pillemer said:
Keith Thompson <ks***@mib.orgw rites:
<snip>
>
The exercise computes the *ranges* of the types (e.g., 0..65535), not
their sizes (e.g., 2 bytes or 16 bits).

(These are examples; I'm not assuming that a byte is 8 bits.)

..hmm.. Would I look the at the number of bits in a char to determine
how many bits in a byte?

Yes, because a char is exactly one byte wide. Incidentally, it is also
exactly CHAR_BIT bits wide.
If this is a correct I could then fill a unsigned char with bits set
to one and then left shift them until all the bits are zeroed?

Yes, you could do that, or you could simply do this:

unsigned char x = 1;
int char_bit = 0;
while(x 0)
{
++char_bit;
x <<= 1;
}

or of course you could just use CHAR_BIT from <limits.h>.
I know this. I am also learning from "Advanced Programmig in the UNIX
environment." On page 27, figure 2.6 Stevens/Rago lists 19 limits that
can be found in <limits.h>. The K&R2 exercise said I should also as an
exercise determine this kind of information by direct
computation. (Excerise 2-1 on page 36). That is why I am trying to do
this without referring to <limits.h>.

--
ilAn
Apr 1 '07 #35

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

Similar topics

17
6154
by: John Bentley | last post by:
John Bentley: INTRO The phrase "decimal number" within a programming context is ambiguous. It could refer to the decimal datatype or the related but separate concept of a generic decimal number. "Decimal Number" sometimes serves to distinguish Base 10 numbers, eg "15", from Base 2 numbers, Eg "1111". At other times "Decimal Number" serves to differentiate a number from an integer. For the rest of this post I shall only use either...
1
10627
by: Jan Agermose | last post by:
Im writing information into an existing excel document using a connection string like: strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Filename + ";Extended Properties=\"Excel 8.0;HDR=yes;\""; and one of to methods for inserting data. First I simply tried building insert statements as strings "insert into (, ) values ('Some text', 42)"
2
2335
by: Mark Gibson | last post by:
Hello, I've been experimenting with dblink recently, and have encountered some limitations I'd like to discuss. I've been trying to create views of remote tables, like so: CREATE VIEW stuff AS SELECT * FROM dblink(' ... remote connection info ... ', 'SELECT id, title, title_idx FROM stuff')
0
9680
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9528
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10456
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9052
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7548
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6788
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5442
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4118
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2926
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.