473,796 Members | 2,583 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 2066
On 26 Mar 2007 04:43:28 -0700, "ja*******@gmai l.com"
<ja*******@gmai l.comwrote in comp.lang.c:
On Mar 26, 10:54 am, "Raman" <ramanchalo...@ gmail.comwrote:
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

Hi All,

U can get a sizeof any variable without even knowing what is its type
or where it is used. The way is explained below as an example.
U doesn't post here anymore, we killed him.
int a;
printf("size of variable is %d\n",(&a+1)-(&a));
The printf() call above produces undefined behavior on systems where
the underlying type of ptrdiff_t is wider than int. Many such systems
exist.
This is applicable for all datatypes(even for array).

Regards
jamsheed m(ja*******@gma il.com)
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Mar 27 '07 #11
"ja*******@gmai l.com" wrote:
>
.... snip ...
>
U can get a sizeof any variable without even knowing what is its
type or where it is used. The way is explained below as an example.

int a;
printf("size of variable is %d\n",(&a+1)-(&a));

This is applicable for all datatypes(even for array).
U doesn't read this newsgroup any more, and even if he did, he
would be ill-advised to take your advice, which involves undefined
and implementation defined behaviour. I am NOT referring to the
missing includes.

--
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 #12
Raman wrote:
>
Is ut possible to calculate size of any standard data types at
run time i.e without using sizeof() operator
Maybe this should be in the FAQ under the heading 'Foolish
Questions'?

--
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 #13
On Mar 26, 4:53 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
jamshe...@gmail .com said:
U can get a sizeof any variable without even knowing what is its type
or where it is used. The way is explained below as an example.
int a;
printf("size of variable is %d\n",(&a+1)-(&a));
This is applicable for all datatypes(even for array).

Wrong wrong wrong wrong wrong, as you'd know if you'd tried the above
code yourself.

The correct answer, always, is: use sizeof. That is what it is *for*.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999http://www.cpax.org.uk
email: rjh at the above domain, - www.

its possible
printf("size of variable is %d\n",(char*)(& a+1)-(char*)(&a));
This will print the proper size of variable.

Mar 27 '07 #14
ja*******@gmail .com said:

<snip>
printf("size of variable is %d\n",(char*)(& a+1)-(char*)(&a));
This will print the proper size of variable.
He didn't ask for the size of a variable, but for the size of a type.
Check the subject line: "sizeof dataTypes at run 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 #15
On Mar 27, 2:10 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
jamshe...@gmail .com said:

<snip>
printf("size of variable is %d\n",(char*)(& a+1)-(char*)(&a));
This will print the proper size of variable.

He didn't ask for the size of a variable, but for the size of a type.
Check the subject line: "sizeof dataTypes at run time".

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999http://www.cpax.org.uk
email: rjh at the above domain, - www.
Then there is no way to know the size of the type directly without
crating a variable of that type.
we can never use sizeof(double) or something like this.
Please let me know if anyone knows the solution for that.

Mar 27 '07 #16
On Mar 27, 3:20 pm, "jamshe...@gmai l.com" <jamshe...@gmai l.comwrote:
On Mar 27, 2:10 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
jamshe...@gmail .com said:
<snip>
printf("size of variable is %d\n",(char*)(& a+1)-(char*)(&a));
This will print the proper size of variable.
He didn't ask for the size of a variable, but for the size of a type.
Check the subject line: "sizeof dataTypes at run time".
Sorry for my misunderstandin g.
>
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999http://www.cpax.org.uk
email: rjh at the above domain, - www.

Then there is no way to know the size of the type directly without
crating a variable of that type.
we can never use sizeof(double) or something like this.
Please let me know if anyone knows the solution for that.
No its possible to know the size using the type itself. Check the
sample program below.

#include <stdio.h>

#define SIZE(x) ( (long)(((char*) ((x*)0 + 1)) - (char*)((x*)0)) )

int main()
{

printf(" sizes of char = %ld \t int = %ld \t float = %ld \t double =
%ld\n", SIZE(char), SIZE(int), SIZE(float), SIZE(double));

return 0;
}

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.

Thanks in advance.

--
Regards,
Ram

Mar 27 '07 #17
ramana wrote:
No its possible to know the size using the type itself. Check the
sample program below.

#include <stdio.h>

#define SIZE(x) ( (long)(((char*) ((x*)0 + 1)) - (char*)((x*)0)) )

int main()
{

printf(" sizes of char = %ld \t int = %ld \t float = %ld \t double =
%ld\n", SIZE(char), SIZE(int), SIZE(float), SIZE(double));

return 0;
}
The expression `(x*)0 + 1` (for whatever type `x`) is undefined.

So, although it may work on some (perhaps many; even, perhaps, all)
implementations , it's not guaranteed to work at all.

--
Is it a bird? It is a plane? No, it's: http://hpl.hp.com/conferences/juc2007/
"He's dead, Jim, but not as we know it." Unsaid /Trek/

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

Mar 27 '07 #18
ja*******@gmail .com said:
On Mar 27, 2:10 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
>>
He didn't ask for the size of a variable, but for the size of a type.
Check the subject line: "sizeof dataTypes at run time".

Then there is no way to know the size of the type directly without
crating a variable of that type.
Sure there is. It's called sizeof.
we can never use sizeof(double) or something like this.
Yes, we can.

--
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 #19
"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.
--
Ben Pfaff
http://benpfaff.org
Mar 27 '07 #20

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
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,...
1
10173
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10006
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...
1
7547
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
5441
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...
0
5573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4116
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
2925
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.