tags:

views:

680

answers:

4

Let us assume I have declared the variable 'i' of certain datatype (might be int, char, float or double) ...

NOTE: Simply consider that 'i' is declared and dont bother if it is an int or char or float or double datatype. Since I want a generic solution I am simply mentioning that variable 'i' can be of any one of the datatypes namely int, char, float or double.

Now can I find the size of the variable 'i' without sizeof operator?

+1  A: 

Look up the documentation for the compiler.

Mehrdad Afshari
+14  A: 

You can use the following macro, taken from here:

#define sizeof_var( var ) ((size_t)(&(var)+1)-(size_t)(&(var)))

The idea is to use pointer arithmetic ((&(var)+1)) to determine the offset of the variable, and then subtract the original address of the variable, yielding its size. For example, if you have an int16_t i variable located at 0x0002, you would be subtracting 0x0002 from 0x0006, thereby obtaining 0x4 or 4 bytes.

However, I don't really see a valid reason not to use sizeof, but I'm sure you must have one.

JG
Darn ... trying it and getting the syntax right for my first C code in ages made me slow ;-)
Joachim Sauer
I think this logic would be helpful to find the size of a datatype but how about a variable of unknown datatype
codingfreak
What do you mean by unknown, how would you declare it ? If you have a variable like, say, `int i`, `sizeof_var(i)` will give you its size.
JG
Let us suppose you have simply forgot for what datatype you have declared the variable 'i'. What I mean to say is irrespective of the datatype to which it has been declared we should still be able to find the size of the variable 'i'
codingfreak
Responding to your edit, the macro works independently of the type of `var`. You can have either `char i` or `long i`, and `sizeof_var(i)` will return the correct result on both counts.
JG
@JG Yeah this works fine.
codingfreak
A: 

Assuming the types you will check are built-in types, you may try to assign certain roof values to them (8-bit, 16-bit, 32-bit and 64-bit roof values) and see if it throws an exception.

Dunya Degirmenci
Since when does C throw exceptions for out-of-range values? (An implementation *can* do this for signed types, but in practice none do.)
R..
+1  A: 

It's been ages since I wrote any C code and I was never good at it, but this looks about right:

int i = 1;
size_t size = (char*)(&i+1)-(char*)(&i);
printf("%zi\n", size);

I'm sure someone can tell me plenty of reasons why this is wrong, but it prints a reasonable value for me.

Joachim Sauer
I've no C compiler near me, but I wonder if it works. Can someone confirm this ? Really unexpected way to get the size of something
Clement Herreman
Hey I mentioned it clearly that i can be of any datatype of the 4. In the above code you were simply using it as a int to say that the size of variable 'i' is 4 bytes
codingfreak
The only thing you need to fix is change `void *` to `char *` - you're not supposed to do pointer arithmetic with `void *`.
caf
@caf: really? i remember listening that `void *` acts the same was as `char *` in pointer arithmetic and `gcc` didn't complain about that even with `-Wall`.
Joachim Sauer
@codingfreak: So? Only the type of i needs to be changed to get a different result. The type of `size` should probably be `size_t` for correctness and clarity.
Joachim Sauer
there is some issue with padding, the compiler can insert some free space between variables (well, it could even reorder them but I know of no compiler that do it).
kriss
Yes the code seems to be working .... even if I replace int with char or float
codingfreak
@kriss: i know of padding, but I don't think it applies here, as I'm only referencing a single variable. Alignment might be another issue, which might falsify the result (or are alignment -))
Joachim Sauer
I checked, it's OK with padding sizeof does the same and includes padding in the size of var. Hence it woks exactly the same.
kriss
Using (char*) instead of (void*) would avoid compiler warnings about void* arithmetic and does give the same result because a char has always size 1.
drhirsch
@drhirsch: I don't doubt that but I can't get gcc 4.3.3 to emit those warnings (even with "`-Werror -Wall -Wextra -pedantic`"). Which compiler (or which compiler switches) should I use to get those warnings?
Joachim Sauer
Correction: actually "-pedantic" enables -Wpointer-arith which emits the warning about pointer arithmetic with `void *`.
Joachim Sauer
This will work on processors that have byte addressable memory.I am not sure whether this would work on the DSP56300 series processors where char and int are both stored in individual 24 bit wide memory locations. The processor does not have byte addressing so the difference between the pointer values for successive chars or ints will both be 1.
Ian
@Ian: irrelevant. `sizeof` is defined in terms of `char` units, so the result will be correct. If your implementation uses 24 bit chars but artificially restricts them to 8-bit range, then it's not C. C mandates that `char` does not have padding bits.
R..