473,796 Members | 2,558 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
Ben Pfaff wrote:
"ja*******@gmai l.com" <ja*******@gmai l.comwrites:
>we can never use sizeof(double) or something like this.

It's always funny when people who don't know C try to instruct
others on how to use it.
We can't use `sizeof (type)` to calculate the size of that type at
run time (as the OP was requesting), because `sizeof` is calculated
at compile-time [1]!
[1] Apart from C99's variable-length arrays: are there variable-length
types to go with them?

--
The second Jena user conference! http://hpl.hp.com/conferences/juc2007/
Meaning precedes definition.

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

Mar 27 '07 #21
Chris Dollin said:
Ben Pfaff wrote:
>"ja*******@gma il.com" <ja*******@gmai l.comwrites:
>>we can never use sizeof(double) or something like this.

It's always funny when people who don't know C try to instruct
others on how to use it.

We can't use `sizeof (type)` to calculate the size of that type at
run time (as the OP was requesting), because `sizeof` is calculated
at compile-time [1]!
<shrugSo use an interpreter. That way, there isn't a compile time.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 27 '07 #22
Richard Heathfield wrote:
Chris Dollin said:
>Ben Pfaff wrote:
>>"ja*******@gm ail.com" <ja*******@gmai l.comwrites:

we can never use sizeof(double) or something like this.

It's always funny when people who don't know C try to instruct
others on how to use it.

We can't use `sizeof (type)` to calculate the size of that type at
run time (as the OP was requesting), because `sizeof` is calculated
at compile-time [1]!

<shrugSo use an interpreter. That way, there isn't a compile time.
The Standard distinguishes the time-of-evaluation-of-sizeof and the
time-of-evaluation-of-non-constant-expressions, doesn't it?

Using an interpreter is a detail outside the scope of the standard.

PS We both think the answer is "use sizeof, that's what it's for!".

--
Yes, Virginia, there is a second Jena user conference: Palo Alto, Sep 2007.
"Go not to the Elves for counsel, for they will answer both no and yes."/tLotR/

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

Mar 27 '07 #23
Chris Dollin said:
Richard Heathfield wrote:
>Chris Dollin said:
>>Ben Pfaff wrote:

"ja*******@g mail.com" <ja*******@gmai l.comwrites:

we can never use sizeof(double) or something like this.

It's always funny when people who don't know C try to instruct
others on how to use it.

We can't use `sizeof (type)` to calculate the size of that type at
run time (as the OP was requesting), because `sizeof` is calculated
at compile-time [1]!

<shrugSo use an interpreter. That way, there isn't a compile time.

The Standard distinguishes the time-of-evaluation-of-sizeof and the
time-of-evaluation-of-non-constant-expressions, doesn't it?
But they can still both be at runtime, which is all the question
requires.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 27 '07 #24
Richard Heathfield wrote:
Chris Dollin said:
>Richard Heathfield wrote:
>>Chris Dollin said:

Ben Pfaff wrote:

"ja*******@ gmail.com" <ja*******@gmai l.comwrites:
>
>we can never use sizeof(double) or something like this.
>
It's always funny when people who don't know C try to instruct
others on how to use it.

We can't use `sizeof (type)` to calculate the size of that type at
run time (as the OP was requesting), because `sizeof` is calculated
at compile-time [1]!

<shrugSo use an interpreter. That way, there isn't a compile time.

The Standard distinguishes the time-of-evaluation-of-sizeof and the
time-of-evaluation-of-non-constant-expressions, doesn't it?

But they can still both be at runtime, which is all the question
requires.
Compile-time is when sizeof happens. The /interpreter's/ runtime isn't
visible to a C program.

Urm: equine, deceased, selling/whipping?

--
JUC 2007, submit: http://hpl.hp.com/conferences/juc2007/submission.html
"You're not supposed to /think/ about it, you're supposed to say NO!"
Jill Swinburn, /The Beiderbeck Connection/

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

Mar 27 '07 #25
Chris Dollin said:

<snip>
Urm: equine, deceased, selling/whipping?
<sequitur relevance="non" >
I have an interesting new analogy for why casting is almost always a bad
idea - based on a true story.
</sequitur>

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 27 '07 #26
ramana wrote:
>
.... snip ...
>
I am new to posting to the groups. So i don't know the rules/practices
to follow. Please don't hesitate to tell me any that sort of stuff.
Read the following links:

--
Some useful references about C:
<http://www.ungerhu.com/jxh/clc.welcome.txt >
<http://www.eskimo.com/~scs/C-faq/top.html (C-faq)
<http://benpfaff.org/writings/clc/off-topic.html>
<http://anubis.dkuug.dk/jtc1/sc22/wg14/www/docs/n869/(C99)
<http://www.dinkumware. com/refxc.html (C-library}
<http://gcc.gnu.org/onlinedocs/ (GNU docs)
<http://clc-wiki.net/wiki/C_community:com p.lang.c:Introd uction>

--
Posted via a free Usenet account from http://www.teranews.com

Mar 27 '07 #27
"ja*******@gmai l.com" wrote:
>
.... snip ...
>
its possible
printf("size of variable is %d\n",(char*)(& a+1)-(char*)(&a));
This will print the proper size of variable.
No it won't, it involves undefined behaviour, even if you did
#include <stdio.h>. The type of a pointer subtraction is not int.
When you lie to a variadic function about the type of parameters
all rules are cancelled, and you may launch WWIII. In addition

printf("%d\n", (int)sizeof(int ));

is considerable shorter and more understandable, besides being
defined. From N869:

6.5.3.4 The sizeof operator

Constraints

[#1] The sizeof operator shall not be applied to an
expression that has function type or an incomplete type, to
the parenthesized name of such a type, or to an expression
that designates a bit-field member.

Semantics

.... snip ...

[#4] The value of the result is implementation-defined, and
its type (an unsigned integer type) is size_t, defined in
the <stddef.hheader .

....

6.5.6 Additive operators

.... snip ...

[#9] When two pointers are subtracted, both shall point to
elements of the same array object, or one past the last
element of the array object; the result is the difference of
the subscripts of the two array elements. The size of the
result is implementation-defined, and its type (a signed
integer type) is ptrdiff_t defined in the <stddef.hheader .
If the result is not representable in an object of that
type, the behavior is undefined. In other words, if the
expressions P and Q point to, respectively, the i-th and j-
th elements of an array object, the expression (P)-(Q) has
the value i-j provided the value fits in an object of type
ptrdiff_t. Moreover, if the expression P points either to
an element of an array object or one past the last element
of an array object, and the expression Q points to the last
element of the same array object, the expression ((Q)+1)-(P)
has the same value as ((Q)-(P))+1 and as -((P)-((Q)+1)), and
has the value zero if the expression P points one past the
last element of the array object, even though the expression
(Q)+1 does not point to an element of the array object.79)

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Mar 27 '07 #28
"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
Try this:
Write a file containing the line:

#define REINVENTING_THE _WHEEL sizeof

and save it as newwheel.h.

Then use this:

#include <stdio.h>
#include "newwheel.h "
int main(void)
{
printf("%d\n", (int)REINVENTIN G_THE_WHEEL(int ));
return 0;
}
Mar 29 '07 #29
"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."

I was doing this excerise recently as I am new to C and have been
learning from K&R2. I do not know if the technique I used was the
best.

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. 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 have not yet attempted to the floating-point excercise.

--
ilAn
Mar 31 '07 #30

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
10230
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10012
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
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...
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
2
3731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
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.