-6

Here we have sizeof operator in C. It is returning the size of datatypes,

printf("%d",sizeof(some datatype));

and result is 4 (OS dependent).

I want to know how it is calculating. what is the logic behind that operator. If I ask you to write code for sizeof operator. what will be your answer?

Note : I want to know how sizeof written as a code so it is capable to calculate size.

As may be I am not able to ask my question properly,I am trying here again.

Write one function which we can use instead of sizeof operator.

someone
  • 1,638
  • 3
  • 21
  • 36
  • 1
    This is not duplicate... I want to know how sizeof written as a code so it is capable to calculate size. – someone Jul 25 '13 at 12:14
  • is there anyone who can take it a general question??? I wrote int for just as a example... – someone Jul 25 '13 at 12:18
  • 1
    What code? `sizeof` is an operator, not a function. – Andrejs Cainikovs Jul 25 '13 at 12:18
  • @Krishna The implementation approach is perfectly well described in the accepted answer from the question which I linked - this is true for all types, not just int – Andreas Fester Jul 25 '13 at 12:19
  • 1
    If you want to know how `sizeof` is implemented, there are many C compilers with source freely available that you can use, not all of them complicated like GCC or clang. – Some programmer dude Jul 25 '13 at 12:26
  • check the followings http://stackoverflow.com/questions/14171117/implementation-of-sizeof-operator http://stackoverflow.com/questions/13938086/how-compiler-works-to-evaluate-sizeof-operator-in-c – pradipta Jul 25 '13 at 12:26

4 Answers4

5

The compiler knows the size of all types, it has to or it would not be able to generate code correctly. This information is used by the sizeof operator.

Also note that sizeof is not some function being called at runtime, it's an operator that is fully evaluated during compilation.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Can you write the code if I ask you to develop one more sizeof operator with your name...I hope this will elaborate my question. – someone Jul 25 '13 at 12:19
  • If you write `sizeof("Hello")` the compiler just replaces it with constant `6` - 5 letters plus the null character. As Joachim just wrote, it's a compile time operator, nothing is magically computed in runtime to know the size of something. – João Fernandes Jul 25 '13 at 12:25
  • 2
    @Krishna I am pretty sure Joachim can :-) - essentially, it would result in something like `return 16`. This is what the compiler does - it looks up the type and since the compiler knows the length of each type it can replace it with a constant – Andreas Fester Jul 25 '13 at 12:25
  • 1
    note : Since C99 and variable length arrays, it is computed at run time when a variable length array is part of the expression in the sizeof operand. – BLUEPIXY Jul 25 '13 at 12:44
1

In the compilation time, sizeof(int) is replaced with a constant, specific to a platform.sizeof operator is completely implemented in the C compiler. It's not a function call, it's not working in a run-time.

The code for sizeof in the compiler would be pretty simple. It has hard-coded values that, for example, on 32 bit machine int has 4 bytes, char has 1 byte etc. So, it's just a simple look-up and replacement of string sizeof(int) with 4.

By the way, you don't need parentheses on unary expressions, so having int t[10] you can just write sizeof t. However, you will need them for a type-name expression, for example: sizeof(int). Just an interesting thing.

Adam Stelmaszczyk
  • 19,665
  • 4
  • 70
  • 110
  • That's not correct. `sizeof int` is wrong, because `int` is a type name, not a unary expression. The things you can use as the argument to the `sizeof` expression are (1) a unary expression, or (2) a type name _in parentheses_. So if you have `int *foo[30]`, you can write `sizeof foo`, but you still have to right `sizeof (int)`. – This isn't my real name Jul 25 '13 at 18:19
  • @ElchononEdelson Ops, I made a mistake, thank you, I've edited my post. – Adam Stelmaszczyk Jul 25 '13 at 21:47
1

Quoting from wikipedia:

In most cases, sizeof is a compile-time operator, which means that during compilation sizeof expressions get replaced by constant result-values.

Iosif Murariu
  • 2,019
  • 1
  • 21
  • 27
0

At compilation time sizeof(int) is replaced by appropriate size dependent upon the operating system(16 bit, 32 bit, 64 bit)

You can write your own size of function for strings

or a MACRO

#define SIZEOF(x) ((char*)(&(x) + 1) - (char*)&(x))