Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why need not a hosted environment be provided?

The C11 standard says:

§5.1.2.2 Hosted environment
A hosted environment need not be provided, but shall conform to the following specifications if present.

I'm confused about it, how can I execute a program without an execution environment? Is it implied that a hosted environment is not necessary, while a freestanding environment does?

like image 316
nalzok Avatar asked Oct 23 '25 15:10

nalzok


2 Answers

The differentiation between a hosted and a freedstanding environment and implementation is the presence of the C standard library. Language features themselves are available for both (except for few optional features).

But iff an environment is called "hosted", it has to provide the full (library) features as required by the standard. There is no half-way.

A C program written for a hosted environment can expect all mandatory features to be present, thus can use malloc, etc. Once such a feature is missing, e.g. dynamic memory allocation, it is not a hosted environment, but a freestanding. This does not mean the latter cannot supply some of the standard libraries (or some functions of it). Actually, even for a freestanding environment there might be e.g. conversion functions avilable.

So if there is no standard library available, how can a program operate? Simply put: It does not use these functions. Instead, it has to use its own functions to access the underlying hardware or a specialised non-standard library. If you do not have a car, you have to walk, bike or build one on your own. There is no fundamental law you have to use e.g. printf, but can also send the characters to a serial interface directly or call the OS function to send a character to stdout.

Typical scenario are bare-metal (i.e. without a full-grown OS) embedded systems. They often do not need to print characters, but toogle an LED, control a motor, detect a push-button by monitoring a digital input, etc.

As others already mentioned, an OS is another example. Obviously, you cannot expect an OS to be available for the OS - there's no turtles all the way down.

like image 55
too honest for this site Avatar answered Oct 26 '25 04:10

too honest for this site


I'm confused about it, how can I execute a program without an execution environment?

"Hosted" basically means with an OS underlying. Given the targets C aims to, requiring one could be very limiting, however. Consider many microcontrollers or real-time systems: the only program running is the one you coded, therefore without limitations; that's why it's said to be free-standing.

As N1124 says itself (emphasis mine)

In a freestanding environment (in which C program execution may take place without any benefit of an operating system)


Is it implied that a hosted environment is not necessary, while a freestanding environment does?

The standard defines the two types of environments the C code is expected to run on along with the requirements for them. Freestanding environments have lower requirements than hosted ones: this could mean that some features may not be available whereas they would instead on hosted environments, which would mean that it's "necessary" to have one.

like image 24
edmz Avatar answered Oct 26 '25 06:10

edmz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!