how the standard and run-time library works

I'm a bit confused about the differences between the standard and the run-time library in c (and c++)

i get that the c definition defines a set of basic functionality like malloc and printf

soo...is that's the standard library that contains those functions?

but whats the run-time library used for then?

and furthermore, who develops the standard library in the first place?
i mean, i know that i can always just use malloc to allocate memory
but how does malloc itself allocate memory?
Last edited on
soo...is that's the standard library that contains those functions?

Yes.

but whats the run-time library used for then?

The runtime "library" contains various important functions that bind your program to the operating system. For example the runtime contains the startup and shutdown routines.

and furthermore, who develops the standard library in the first place?

The compiler development team/manufacturer.

i mean, i know that i can always just use malloc to allocate memory

Actually malloc() may not always be available in freestanding implementations.

but how does malloc itself allocate memory?

Since you're just beginning to learn the language it is not really important to know exactly how memory is allocated, all you should really need to know at this stage is how to properly use the malloc() function.



I'm a bit confused about the differences between the standard and the run-time library in c (and c++)

"Standard" library is a bit ambiguous. Both C and C++ have libraries that supplement their respective languages. The C library is declared in a series of header files like <stdio.h>. C has no concept of namespaces.
http://www.cplusplus.com/reference/clibrary/
C++ implements the functions in the C library in the std namespace. Hence the C++ reference to the standard library. This provides a mechanism to isolate name collisions. C++ versions of the C header files all begin with c and do not have a .h suffix, like <cstdio>.

"run-time" library usually refers to the routines provided by the implementer in object format. This can refer to either C or C++ run time. For example, printf has to be implemented somewhere so that you can bind it with your code.

and furthermore, who develops the standard library in the first place?

The compiler implementer provides both the header files and the run time library.

but how does malloc itself allocate memory?

That's up to the implementer of the run-time library. On the platform I work on, the OS provides built-in heap management, so malloc is simply a call to the OS to obtain a block of memory from the process' heap (part of the process image). On other platforms, heap management is performed by the run-time library.
https://en.wikipedia.org/wiki/Memory_management

Thanks a lot guys i think i somewhat get it now.

So would it be correct to say that the standard library is, more or less, just a set of header files that defines the standard functions and data types

and the run-time library is (among other things, such as the startup / shutdown routines) what sort of "initializes" those functions by binding them to the operating system?

so for example, the printf function is defined in a header file from the standard library but the actual implementation of printf is within the run-time library?

(i haven't finished reading all of what you guys linked me yet, so i apologize if its already been answered in one of the documents)

or would that be way off?
Last edited on
I think different people use "runtime library" for different things.

Some use that to refer to all binary components of the implementation of a standard library (that's where your printf's definition is). It could be a file like libc.so or msvcrt.dll ("CRT" in that dll's name stands for "C Run Time")

Others (like the editors of https://en.wikipedia.org/wiki/Runtime_library ) refer to something else, to quote

For example, the C programming language requires only a minimal runtime library (commonly called crt0), but defines a large standard library (called C standard library) that has to be provided by each implementation

Last edited on
So would it be correct to say that the standard library is, more or less, just a set of header files that defines the standard functions and data types

No, the standard library is a group of functions and data types that are compiled into a library (object code), that are defined in standard header files.

so for example, the printf function is defined in a header file from the standard library but the actual implementation of printf is within the run-time library?

Yes this is correct, if printf() is actually included by your implementation. Remember a non-hosted environment doesn't necessarily contain all of the functionality of a hosted system. The C-stdio functions are not always required in a non-hosted environment since a non-hosted environment quite often doesn't contain a standard input or output mechanism.

and the run-time library is (among other things, such as the startup / shutdown routines) what sort of "initializes" those functions by binding them to the operating system?

Assuming that you have an operating system, yes, and more. But remember that an operating system is not a requirement.



Topic archived. No new replies allowed.