Jeegar Patel
Jeegar Patel

Reputation: 27210

Why sizeof is implemented as operator not as macros

I understand the reason behind why sizeof is not implemented as function from Why is sizeof considered as an operator?

But i do not get is why we need to implement sizeof as special type of unary operator which execute as compile time not runtime (i guess As other operators always executed as runtime)

Same things we can achieve by making sizeof as macro isnt it?. What is the difference between such compiled time unary operator and macros?


i just want to know why they did not think for macro first and rather then implementing different operator. When i first thought what is sizeof() i thought it should be macro. didnt think of such unary operator ever.

Upvotes: 2

Views: 222

Answers (3)

AnT stands with Russia
AnT stands with Russia

Reputation: 320421

Your question is not without merit. In fact, standard library already has a very similar feature implemented as a macro: it is the standard offsetof macro. Note also that a large portion of sizeof's usability comes form its compile-time nature (also true for offsetof), which is why a run-time implementation would be significantly inferior. (And, BTW, your assertive statement claiming that all other operators are "always executed at runtime" is completely untrue.)

One reason sizeof could not be implemented as a macro even in the original C language was the fact that it was supposed to accept both types and expressions as arguments. It would be impossible to cover both kinds of arguments with a single macro. We'd need at least two macros: one for types and one for expressions. But even if we agree to separate these two forms of sizeof into two different macros, it still would be rather difficult (or even impossible) to implement a macro that would handle expression arguments properly.

Meanwhile, if we restrict ourselves to the original C (i.e. exclude from consideration C99 VLAs), then we could probably implement sizeof for type arguments as a macro

#define sizeof(T) ((size_t) ((T *) 0 + 1))

This is pretty much the same technique that is used to implement the standard offsetof as a macro.

The above macro implementation is still not perfect, since it will not work properly for, say, int[6] argument, but you get the idea. This, BTW, might be another point in favor of a built-in operator implementation instead of a macro.

But how to do the same thing for expression arguments - I don't know. Do you?

Upvotes: 6

Clifford
Clifford

Reputation: 93476

other operators always executed as runtime.

That is not true; operators in constant expressions are evaluated at compile time. So for example, 15 * 60 is compile time evaluated. The sizeof operator expressions, other than VLAs introduced in C99, are are by definition constant, so are always evaluated at compile time.

Upvotes: 1

Tzach
Tzach

Reputation: 13376

Macros are processed by the preprocessor. Operators are processed by the compiler.

Although sizeof(int) can be easily determined by the preprocessor, think about sizeof(MyCustomStruct). In order to evaluate it, we must know the structure of MyCustomStruct, which is only evaluated by the compiler.

The advantage of compile time over run time is of course performance. If you can do it once in compile time, why doing it many times in run time?

Upvotes: 3

Related Questions