473,785 Members | 2,640 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 #1
34 2065
"Raman" <ramanchalo...@ gmail.comwrote:
Is ut possible to calculate size of any standard data types at
run time i.e without using sizeof() operator
http://groups.google.com/group/comp....rch+this+group

--
Peter

Mar 26 '07 #2
Raman said:
Hi All,

Is ut possible to calculate size of any standard data types at run
time i.e without using sizeof() operator
Sure - and this is especially easy since there is no such thing as the
sizeof() operator. All you have to do is instantiate the type, and use
the sizeof operator on the resulting object. This way, you avoid even
the appearance of using what someone has mistakenly described as a
sizeof() operator.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 26 '07 #3
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.

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

This is applicable for all datatypes(even for array).

Regards
jamsheed m(ja*******@gma il.com)
Mar 26 '07 #4
ja*******@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/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 26 '07 #5
In article <11************ **********@l75g 2000hse.googleg roups.com>,
ja*******@gmail .com <ja*******@gmai l.comwrote:
>int a;
printf("size of variable is %d\n",(&a+1)-(&a));

This is applicable for all datatypes(even for array).
.... and always prints 1.

-- Richard
--
"Considerat ion shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Mar 26 '07 #6
Yes it's possible to know. use this macro

#define SIZE(x) ( ((char*)(&x + 1)) - (char*)&(x) )

where 'x' is the variable.

sample.c:
............... .......
#include <stdio.h>

#define SIZE(x) ( ((char*)(&x + 1)) - (char*)&(x) )

int main()
{
char c;
int i;
float f;
double d;

printf(" sizes of char = %d \t int = %d \t float = %d \t double = %d
\n", SIZE(c), SIZE(i), SIZE(f), SIZE(d));

return 0;
}
............... ...............
Regards,
Ram

On Mar 26, 4:43 pm, "jamshe...@gmai l.com" <jamshe...@gmai l.comwrote:
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.

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

This is applicable for all datatypes(even for array).

Regards
jamsheed m(jamshe...@gma il.com)

Mar 26 '07 #7
yes. Its possible. Use the below macro.

sample program:
............... ............... .....
#include <stdio.h>

#define SIZE(x) ( ((char*)(&x + 1)) - (char*)&(x) )

int main()
{
char c;
int i;
float f;
double d;

printf(" sizes of char = %d \t int = %d \t float = %d \t double = %d
\n", SIZE(c), SIZE(i), SIZE(f), SIZE(d));

return 0;
}
............... ............... ............

Regards,
Ram
Raman wrote:
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 #8
ramana said:
yes. Its possible. Use the below macro.

sample program:
............... ............... ....
#include <stdio.h>

#define SIZE(x) ( ((char*)(&x + 1)) - (char*)&(x) )
That doesn't calculate the size of a type.

Proof:

#include <stdio.h>

#define SIZE(x) ( ((char*)(&x + 1)) - (char*)&(x) )

int main(void)
{
printf("%d\n", (int)SIZE(doubl e));
return 0;
}

foo.c: In function `main':
foo.c:7: parse error before `double'
foo.c:7: parse error before `)'

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 26 '07 #9
On 26 Mar 2007 06:14:28 -0700, "ramana" <ra************ *@gmail.com>
wrote in comp.lang.c:

Do not top post in this group, it is considered rude. Text you add
should be after or interspersed with quoted text you are responding
to. I have reformatted your post and added my comments.
On Mar 26, 4:43 pm, "jamshe...@gmai l.com" <jamshe...@gmai l.comwrote:
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.

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

This is applicable for all datatypes(even for array).

Regards
jamsheed m(jamshe...@gma il.com)

Yes it's possible to know. use this macro

#define SIZE(x) ( ((char*)(&x + 1)) - (char*)&(x) )

where 'x' is the variable.

sample.c:
............... ......
#include <stdio.h>

#define SIZE(x) ( ((char*)(&x + 1)) - (char*)&(x) )
The macro is correct.
int main()
{
char c;
int i;
float f;
double d;

printf(" sizes of char = %d \t int = %d \t float = %d \t double = %d
\n", SIZE(c), SIZE(i), SIZE(f), SIZE(d));
The printf() statement above is non-portable at best, and causes
undefined behavior at worst. The subtraction of two pointers yields a
result with a type of ptrdiff_t, which is a signed integer type but
not necessarily a signed int.

If ptrdiff_t happens to be signed int on your implementation, or
signed short (unlikely!), then the code will do what you want.

On implementations where ptrdiff_t is a wider type than int, and I
work with some, the code produces undefined behavior.

The obvious solution is to add a cast to int in your macro. Less
obvious but slightly better is to add a cast to long, and change the
conversion specifiers to %ld.

Best, of course, is get rid of the macro and use sizeof.
return 0;
}
............... ..............
Kindly learn how to post a proper signature line. It is separated
from the body of your message by the character string "-- " appearing
on a line by themselves. See my signature below for an example.
Regards,
Ram
--
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 #10

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

Similar topics

17
6153
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
9645
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
9481
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
10155
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
9953
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
5383
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
5513
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4054
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
3655
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2881
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.