CodeBlanks Logo Icon CodeBlanks

Free-standing

Learn free-standing C++ and std lib constraints.

Free-standing

The C++ standard defines two implementation kinds: hosted (an operating system is present, tools like std::cout, std::vector are available) and freestanding (no operating system, only minimal headers).

In a freestanding implementation main is not required; the entry point is hardware-specific (Reset_Handler, for example). Available headers are limited: <cstdint>, <cstddef>, <new>, <type_traits>. These are called freestanding headers; they do not require an operating system.

Freestanding headers

The freestanding headers are: <cstdint> (fixed-width integers), <cstddef> (std::size_t, nullptr_t), <limits>, <type_traits>, <new>. They make no operating-system calls.

By contrast <iostream>, <vector>, <string> are hosted headers; they require dynamic memory and an operating system. Embedded systems avoid them; freestanding types like std::uint32_t are used instead. This lets the program run on any hardware.

Practice Write add with freestanding types

Write std::uint32_t add(std::uint32_t a, std::uint32_t b) with body return a + b;. In main write std::cout << add(3, 4);.

Starter code

main.cpp
#include <iostream>
#include <cstdint>

// Define the add function here

int main() {
    // Call add and print it
    return 0;
}

Sample solution

main.cpp
#include <iostream>
#include <cstdint>

std::uint32_t add(std::uint32_t a, std::uint32_t b) {
    return a + b;
}

int main() {
    std::cout << add(3, 4);
    return 0;
}

Expected output

7

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Match each header to its type

Term Meaning
freestanding No operating system
hosted Operating system present
<cstdint> Freestanding header
<iostream> Hosted header

2 Complete the uint32_t type

Question
std::_____t counter = 0;
Answer
std::uint32_t counter = 0;

Explanation: std::uint32_t is a 32-bit unsigned integer.

3 Which header is freestanding?

main.cpp
// #include <...>
  • <cstdint> Correct answer
  • <iostream>
  • <vector>
  • <string>

Explanation: <cstdint> is a freestanding header; it requires no operating system.

All lessons in Expert