LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to know the size of the variable without using sizeof() ? (https://www.linuxquestions.org/questions/programming-9/how-to-know-the-size-of-the-variable-without-using-sizeof-469920/)

indian 08-01-2006 09:29 PM

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

dmail 08-02-2006 04:22 AM

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 :)

introuble 08-02-2006 05:22 AM

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.

sundialsvcs 08-02-2006 07:45 AM

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...

sirclif 08-02-2006 09:15 PM

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.

spooon 08-02-2006 09:54 PM

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.

vharishankar 08-02-2006 11:04 PM

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. :)


All times are GMT -5. The time now is 10:35 PM.