srikar2097 said:
I have got a struct and I want to find the size of this struct without
using "sizeof()".
Why on Earth would you want to do something silly like that?
I'm not trying to be insulting, but the sizeof operator exists for a
very good reason: it gives you the size of something. There are other
valid ways to determine the size of something, but none that are as
convenient. *Why* exactly do you want to find the size of a struct
without using sizeof?
Here's the code-
struct node
{
int data;
struct node *next;
};
int main()
{
struct node *tmp = NULL;
int sizeof_struct = (int) ++tmp;
printf("Size of struct without sizeof(): %d\n", sizeof_struct);
return 0;
}
Here are my doubts:-
1) Why does this method work? (is it because just while initializing a
string C compiler puts in a '\0' to indicate end of string, is
something like this is happening here?)
Ok, I can understand why that could would *appear* to "work" on some
systems.
tmp is initially a null pointer. A null pointer, on most
implementations, is represented as all-bits-zero. Incrementing a
pointer to "struct node" advances the address it points to by
sizeof(struct node) bytes -- *if* it points to an actual object of
type "struct node". So the compiler is likely, on many systems, to
implement ++tmp by treating tmp as if it were an integer object and
adding the value sizeof(struct node) to it. (Incidentally, one of the
least serious problems with the code is its gratuitous use of the "++"
operator; "tmp + 1" would work as well (or as poorly) here.) tmp is
then converted from a pointer to type int; the result of such a
conversion is implementation-defined, but it's common for it simply to
reinterpret the representation. The final result is *likely* to be
that the int object sizeof_struct will contain the value of
sizeof(struct node).
Note that there are about half a dozen qualifications in my
description. The code depends very heavily on features that are
typical of many implementations, but that are absolutely not
guaranteed by the standard. The code, on some systems, happens to
compute a correct value for sizeof(struct node). It does *not* "work"
in any reasonable sense. Compile the same code on another system, or
with another compiler, or with another version of the same compiler,
or with different optimization options, or when it's dark on a
Tuesday, and you could get different results, or no results at all, or
it could fail to compile, or reformat your hard drive.
2) Are there any other methods? I tried a few more ways to get this,
I'll post them here.
METHOD 2-
-------------------
/*Home Grown Method 2.*/
struct node *trial=NULL;
int *i;
printf ("Size of Struct - Home made2: %d\n", (&i - &trial));
Did you try this? I can't think of any reason, even a grossly
system-specific one, why this would work. It shouldn't even compile.
(I could write something similar to this that might "work" on some
systems, but I won't.)
METHOD 3-
------------------
/*Home Grown Method 3.*/
struct node trial2[2];
printf ("Size of Struct - Home made3: %d\n", &trial2[1] - &trial2
[0]);
Subtracting two pointer values yields a result of type ptrdiff_t. In
this case, the result of &trial2[1] - &trial2[0] is guaranteed to be
exactly 1 (the difference is computed in terms of the size of the type
the pointers point to, not in bytes). Using "%d" to print a ptrdiff_t
value *might* work, but it can fail badly. Again, did you try this?
I want to drive a screw, but I don't want to use a screwdriver; can I
use a hammer? I want to steer my car, but I don't want to use the
steering wheel; can I just slide along the railing at the side of the
road?
The best answer to "How do I compute the size of something without
using sizeof?" is "Use sizeof anyway." Or at least provide a clear
reason why you don't want to use sizeof.