Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 08-01-2006, 09:29 PM   #1
Registered: Aug 2004
Posts: 137

Rep: Reputation: 15
How to know the size of the variable without using sizeof() ?


I just got this question from Here

You are given have a datatype, say X in C.
The requirement is to get the size of the datatype,
without declaring a variable or a pointer variable
of that type,And, of course without using sizeof operator!

Now as one person suggested, one solution can be

char dummy[256];
int size = (int) &(((X *) dummy)[1]) - (int) &(((X *) dummy)[0]));
//than print size

But it is not working. First of all I am not able to understand what is going on here ? What about other solution on that page(which is even more confusing!)

This is bothering me for some time now

Old 08-02-2006, 04:22 AM   #2
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Personaly I would not waste my time on a question like this, for what is the purpose?
sizeof is done at compile time and does not create an object.
Is somebody could give an explanation of why not to use sizeof then I may give it two seconds thought and use sizeof

Last edited by dmail; 08-02-2006 at 04:27 AM.
Old 08-02-2006, 05:22 AM   #3
Registered: Apr 2004
Distribution: Debian -unstable
Posts: 700

Rep: Reputation: 31
#define sizeof_type( type )  (size_t)((type*)1000 + 1 )-(size_t)((type*)1000)
All you had to do is google for a "sizeof" implementation.
Old 08-02-2006, 07:45 AM   #4
LQ Guru
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 8,455
Blog Entries: 4

Rep: Reputation: 2918Reputation: 2918Reputation: 2918Reputation: 2918Reputation: 2918Reputation: 2918Reputation: 2918Reputation: 2918Reputation: 2918Reputation: 2918Reputation: 2918
The point is, though, that sizeof() is known to be correct, on any and all platforms, in the way that that platform's designer envisioned it to be.

This can be a lot more tricky than it first appears. Consider this:
struct foo {
char bar;
int bletch;
char are_you_a_wizard;
char prove_it_say_the_magic_word;
By now you may have whipped out your magic pencil and figured that this structure occupies "1+4+1+1=7 bytes." But it ain't necessarily so. Not only could the sizes of the individual components of the structure be different, but there could be padding anywhere: before, within, and/or after the elements themselves.

Particularly in high-speed scientific computing, memory is basically free but alignment and data-sizes are everything. These boxes use big, wide, fast data-pipelines that are optimized to store and retrieve "cache-aligned" data... address-multiples of (say) 64 bytes. Compilers will generate structures accordingly.

Furthermore, different compile-options will produce different alignments. "Code that is fast" and "code that is portable" are two different and sometimes mutually-exclusive things.

Through it all, sizeof() can be expected to deliver what you need to know. It may be implemented differently on different systems but the entire point is that you do not have to see, do not have to care about, and certainly you do not want to write code that in any way depends on, how it works on any particular system. Not even your own...
Old 08-02-2006, 09:15 PM   #5
Registered: Sep 2004
Location: south texas
Distribution: fedora core 3,4; gentoo
Posts: 192

Rep: Reputation: 30
if you just want to do this as an excersize, i would look for the source code of the sizeof function and see how it does it.
Old 08-02-2006, 09:54 PM   #6
Senior Member
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 50
sizeof is not a function. It is an operator. The compiler replaces it with a number at compile time, and it does not appear in the compiled code. This is possible because it is the compiler which decides how big something is, so it knows. If you use pointer arithmetic, that accomplishes the same thing because pointer arithmetic is compiled by the compiler to be multiplied with the correct size of the type, so it is also fundamentally using the compiler's knowledge of the size of everything.

I think this exercise is stupid because the language is meant to be used together, and not taken apart where you can use one thing and not the other and see what you can do. You are not finding a new way to do something, but you are using the same information from the compiler in a more convoluted way. This is like saying, wow I can add things without using the addition operator, instead of "a + b" I can do "a - -b", or something like that. That is wonderful, but it does not reveal anything more about the computation.
Old 08-02-2006, 11:04 PM   #7
Senior Member
Registered: Dec 2003
Posts: 3,161
Blog Entries: 4

Rep: Reputation: 136Reputation: 136
It's correct. Dabbling with this kind of knowledge is probably not required by any kind of programmer, unless you're writing your own compiler.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Commit file size to a variable mark_wexler Programming 2 05-26-2006 06:48 AM
default array size for variable in gcc compiler mlaich Programming 13 01-04-2006 05:40 PM
reading a char string of variable size in C introuble Programming 3 05-08-2005 01:07 PM
Sizeof returning wrong packet size ?? Quest101 Linux - Networking 1 03-10-2005 08:37 AM
sizeof() return wrong size of structure?? Hammo Programming 7 02-21-2004 04:08 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:11 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration