eg. Struct bla{
int i;
int j;}bla_bla;
sizeof(bla_bla);
How sizeeof operator count bytes allocated to this bla_bla struct,?
How sizeof() operator (in C programming) returns size of given variable?
Well Though it seems tough, but the logic is very simple.
The SizeOf() Operator is like a program. What it does is very simple. All the data that is stored is stored in a stack. Now whenever you need to calculate the number of bytes that are required to store a particular structure or maybe even a class, you need to access the stack.
Suppose you want to know the size of int, Now it is not true that int is always of size 4 bytes. It can be 2 or maybe even 6 or 8 depending on the Microprocessor, Operating System and the compiler.
Example:
sizeof() checks the starting position of the stack where the integer is stored. then it gets the value of the end of the stack where the int in question ends. To do the same, it makes use of pointers to get the memory location of (int) and then the memory location of (int + 1). The subtraction of the two values gives you the size of int.
099 -----
100 ----- $ -
101 ----- $ |_ Space Occupied By The Integer in Question
102 ----- $ |
103 ----- $ -
104 -----
Hope the above illustration helps. For a More Detailed description, Get in touch with me.
Reply:well the how is easy told
int is 4 byte allways, so you can count them
http://java.sun.com/docs/books/tutorial/...
Reply:you already know that sizeof is not function but operator. operator is provided by compiler. and compiler know what size of all variable storage and what are elements of structure. compiler make tables for variable storage information since compiling time, and the value decided on that time.
Reply:Not always an easy answer- there are multiple levels.
Top level is that it is defined at **compile time** as it is a constant. The compiler clearly "knows" the size of the structure, so it simply substitutes a constant for the sizeof() expression and continues merrily along after that.
Now, it is not possible to tell you what result you will get for the given example without knowing the exact compiler and compiler settings that you are using. Typically "int" is four bytes, so sizeof will return a value of eight. However, that is not true for all cases...
Case 1 - "int" may not be four bytes - two or eight are not unheard of. That clearly will change the result.
Case 2 - packing considerations. Sizeof() does not actually return the minimum number of bytes required for the given structure, but the number of physical bytes that it actually will use - the two can be different. Say that a given CPU has a natural 8-byte boundary and that operations are more efficient if variables always begin on the 8 byte boundary. The designers of a given compiler manufacturer may choose to take advantage of this fact and cause both variables in the structure to align on 8-byte boundaries (wasting half of the space in the structure), causing the value of sizeof() to return 16 rather than 8. Note that the compiler may also pad the end of the structure for the same reason (say a structure which requires 15 bytes minimally may have a sizeof() returned of 16 so that the next structure in an array is aligned the same way.
Not so easy, but hope it helps.
Reply:Are you asking how the SizeOf function works? Or what it counts? It returns the size in bytes of the type you pass to it. HOW it does that is largely irrelevant surely?
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment