What's the point of "Feature X requires runtime support"?

Quoting Bjarne Stroustrup's C ++ Programming Language (4th ed., P. 15):

Except operators new

, delete

, typeid

, dynamic_cast

and throw

, and try

-block, individual expressions and operators of C ++ does not need to be supported at runtime. This can be important for embedded and high performance applications.

In what sense auto x = new int;

is runtime support needed and auto x = 2 + 2;

not? Naively, I thought the compiler would just convert any example to some kind of object code. What does object code that creates an object int

that is conceptually different from object code that does some bit arithmetic, which is called "runtime support needed", does?

Further on the same page are some examples of Runtime Support (IIUC):

However, there are good reasons for using C ++ in environments that provide significantly more runtime support. Services such as dynamic loading, incremental compilation, and a type definition database can be used effectively without affecting the language.

What language features don't work without these tools?

+3


source to share


1 answer


new int

dynamically allocates memory. This means there must be room to allocate memory. This requires runtime support.

Oh yes, it auto x = 2 + 2;

also allocates memory. But this is an automatic variable, not one whose lifetime is determined by the user who explicitly deletes it. Storage for automatic (and static) variables can be implementation-limited, but some of them must exist.

In contrast, you can write a C ++ implementation that has no "free storage", no space to allocate memory.

It should also be noted that Stroustrup deals primarily with ::operator new

and ::operator delete

. Placement new

does not require runtime support.




In a broader sense, what Stroustrup means when he says that "runtime support" is what effectively needs to call a function at runtime. This does not necessarily call a function, but does what is very similar to calling a function.

For example, static_cast

ing for a derived class from a base class requires no more than pointer offset. The offset value is statically determined at compile time. At run time, all that happens is that you add a register to a constant value.

dynamic_cast

ing requires reading a lot of runtime data and compiler generated data structures. This is actually a function call, even if it doesn't look like one (although it does). And it certainly has performance characteristics.

Throwing exception / catching is a complex process that must interact with several elements, some of which may be part of the underlying operating environment.

+4


source







All Articles