views:

1112

answers:

8

Hi all
I have a datatype say X and I want to know its size without declaring a variable or pointer of that type and of course without using sizeof operator.
Is this possible. I thought of using standard header files which contain size and range of datatypes but that doesn't work with user defined datatype.
Any help will be appreciated.

Thanx

+2  A: 

Look into the compiler sources. You will get :

  • the size of standard data types.
  • the rules for padding of structs

and from this, the expected size of anything.

If you could at least allocate space for the variable, and fill some sentinel value into it, you could change it bit by bit, and see if the value changes, but this still would not tell you any information about padding.

Stefano Borini
The compiler is always free to add additional padding anywhere, so there is no 'expected size' - except for sizeof(char), which is defined to be 1. Note that char may have more than 8 bits, however...
bdonlan
Can it really be more than 8 bits in C? In C++ it is defined as exactly 1 byte (logic inference: sizeof() returns the size of the argument in bytes, sizeof(char)==1 => char is exactly one byte)
David Rodríguez - dribeas
+7  A: 

Look, sizeof is the language facility for this. The only one, so it is the only portable way to achieve this.

For some special cases you could generate un-portable code that used some other heuristic to understand the size of particular objects[*] (probably by making them keep track of their own size), but you'd have to do all the bookkeeping yourself.

[*] Objects in a very general sense rather than the OOP sense.

dmckee
+3  A: 

The possibility of padding prevent all hopes without the knowledge of the rules used for introducing it. And those are implementation dependent.

AProgrammer
+3  A: 

You could puzzle it out by reading the ABI for your particular processor, which explains how structures are laid out in memory. It's potentially different for each processor. But unless you're writing a compiler it's surprising you don't want to just use sizeof, which is the One Right Way to solve this problem.

Norman Ramsey
+21  A: 

To my mind, this fits into the category of "how do I add two ints without using ++, += or + ?". It's a waste of time. You can try and avoid the monsters of undefined behaviour by doing something like this.

size_t size = (size_t)(1 + ((X*)0));

Note that I don't declare a variable of type or pointer to X.

Charles Bailey
This is certainly not standards compliant, but will work on most implementations anyway.
nos
Oh my, that's the most horrible thing I've seen in my whole programmer life...
Stefano Borini
+1 by the way, I'm impressed anyway.
Stefano Borini
@Stephano: it may be the most horrible thing you've seen in your life, but it's also more or less how offsetof is typically implemented...
Steve Jessop
While 0 is guaranteed to mean "null pointer" in all implementations, it doesn't have to be represented internally by address 0, in which case this will fail. The C-FAQ claims that there do exist such machines http://c-faq.com/null/machexamp.html but they're not anything you're likely to encounter. Clever answer, by the way. :)
Tyler McHenry
Good point, I wonder if there are machines on which `(size_t)(1 + ((X*)0) - ((X*)0))` is any more likely to work. Note, I'm using the guaranteed left to right grouping of _addition-expression_.
Charles Bailey
+3  A: 

Well, I am an amateur..but I tried out this problem and I got the right answer without using sizeof. Hope this helps.. I am trying to find the size of an integer.

int *a,*s, v=10;

a=&v;

s=a;

a++;

int intsize=(int)a-(int)s;

printf("%d",intsize);
Swabha
+3  A: 

The correct answer to this interview question is "Why would I want to do that, when sizeof() does that for me, and is the only portable method of doing so?"

Chris Kaminski
+1  A: 

int a; printf("%u\n",(int)(&a+1)-(int)(&a));

Majid