LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 08-01-2006, 10:29 PM   #1
indian
Member
 
Registered: Aug 2004
Posts: 137

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


Hi,

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

Regards
 
Old 08-02-2006, 05:22 AM   #2
dmail
Member
 
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 05:27 AM.
 
Old 08-02-2006, 06:22 AM   #3
introuble
Member
 
Registered: Apr 2004
Distribution: Debian -unstable
Posts: 700

Rep: Reputation: 31
Code:
#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, 08:45 AM   #4
sundialsvcs
Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 5,452

Rep: Reputation: 1172Reputation: 1172Reputation: 1172Reputation: 1172Reputation: 1172Reputation: 1172Reputation: 1172Reputation: 1172Reputation: 1172
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:
Quote:
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, 10:15 PM   #5
sirclif
Member
 
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, 10:54 PM   #6
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 48
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-03-2006, 12:04 AM   #7
vharishankar
Senior Member
 
Registered: Dec 2003
Posts: 3,142
Blog Entries: 4

Rep: Reputation: 121Reputation: 121
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.
 
  


Reply


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 07:48 AM
default array size for variable in gcc compiler mlaich Programming 13 01-04-2006 06:40 PM
reading a char string of variable size in C introuble Programming 3 05-08-2005 02:07 PM
Sizeof returning wrong packet size ?? Quest101 Linux - Networking 1 03-10-2005 09:37 AM
sizeof() return wrong size of structure?? Hammo Programming 7 02-21-2004 05:08 PM


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

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