ArticleslgStudy

computer science

Dynamic loading

Dynamic loading is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Dynamic loading rather than just read about it. In short: Dynamic loading is a mechanism by which a computer program can, at run time, load a library (or other binary) into memory, retrieve the addresses of functions and variables contained in the library, execute those functions or access those variables, and unload the library from memory. It is one of the three mechanisms by which a computer program can use some other software within the program; the others are static l…

Key takeaways

  • Dynamic loading belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Dynamic loading to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Dynamic loading from memory before moving on to harder problems.

Reference excerpt

Dynamic loading is a mechanism by which a computer program can, at run time, load a library (or other binary) into memory, retrieve the addresses of functions and variables contained in the library, execute those functions or access those variables, and unload the library from memory. It is one of the three mechanisms by which a computer program can use some other software within the program; the others are static linking and dynamic linking. Unlike static linking and dynamic linking, dynamic loading allows a computer program to start up in the absence of these libraries, to discover available libraries, and to potentially gain additional functionality.

History Dynamic loading was a common technique for IBM's operating systems for System/360 such as OS/360, particularly for I/O subroutines, and for COBOL and PL/I runtime libraries, and continues to be used in IBM's operating systems for z/Architecture, such as z/OS. As far as the application programmer is concerned, the loading is largely transparent, since it is mostly handled by the operating system (or its I/O subsystem). The main advantages are:

Fixes (patches) to the subsystems fixed all programs at once, without the need to relink them Libraries could be protected from unauthorized modification IBM's strategic transaction processing system, CICS (1970s onwards) uses dynamic loading extensively both for its kernel and for normal application program loading. Corrections to application programs could be made offline and new copies of changed programs loaded dynamically without needing to restart CICS (which can, and frequently does, run 24/7). Shared libraries were added to Unix in the 1980s, but initially without the ability to let a program load additional libraries after startup.

Uses Dynamic loading is most frequently used in implementing software plugins. For example, the Apache Web Server's *.dso "dynamic shared object" plugin files are libraries which are loaded at runtime with dynamic loading. Dynamic loading is also used in implementing computer programs where multiple different libraries may supply the requisite functionality and where the user has the option to select which library or libraries to provide.

In C/C++ Not all systems support dynamic loading. Unix-like operating systems such as macOS, Linux, and Solaris provide dynamic loading with the C programming language "dl" library. The Windows operating system provides dynamic loading through the Windows API.

Summary

Loading the library Loading the library is accomplished with LoadLibrary or LoadLibraryEx on Windows and with dlopen on Unix-like operating systems. Examples follow:

Most Unix-like operating systems (Solaris, Linux, *BSD, etc.)

macOS As a Unix library:

As a macOS Framework:

Or if the framework or bundle contains Objective-C code:

Windows

Extracting library contents Extracting the contents of a dynamically loaded library is achieved with GetProcAddress on Windows and with dlsym on Unix-like operating systems.

Unix-like operating systems (Solaris, Linux, *BSD, macOS, etc.)

On macOS, when using Objective-C bundles, one can also:

Windows

Converting a library function pointer The result of dlsym() or GetProcAddress() has to be converted to a pointer of the appropriate type before it can be used.

Windows In Windows, the conversion is straightforward, since FARPROC is essentially already a function pointer:

This can be problematic when the address of an object is to be retrieved rather than a function. However, usually one wants to extract functions anyway, so this is normally not a problem.

Unix (POSIX) According to the POSIX specification, the result of dlsym() is a void pointer. However, a function pointer is not required to even have the same size as a data object pointer, and therefore a valid conversion between type void* and a pointer to a function may not be easy to implement on all platforms. On most systems in use today, function and object pointers are de facto convertible. The following code snippet demonstrates one workaround which allows to perform the conversion anyway on many systems:

The above snippet will give a warning on some compilers: warning: dereferencing type-punned pointer will break strict-aliasing rules. Another workaround is:

which disables the warning even if strict aliasing is in effect. This makes use of the fact that reading from a different union member than the one most recently written to (called "type punning") is common, and explicitly allowed even if strict aliasing is in force, provided the memory is accessed through the union type directly. However, this is not strictly the case here, since the function pointer is copied to be used outside the union. Note that this trick may not work on platforms where the size of data pointers and the size of function pointers is not the same.

Solving the function pointer problem on POSIX systems The fact remains that any conversion between function and data object pointers has to be regarded as an (inherently non-portable) implementation extension, and that no "correct" way for a direct conversion exists, since in this regard the POSIX and ISO standards contradict each other. Because of this problem, the POSIX documentation on dlsym() for the outdated issue 6 stated that "a future version may either add a new function to return function pointers, or the current interface may be deprecated in favor of two new functions: one that returns data pointers and the other that returns function pointers". For the subsequent version of the standard (issue 7, 2008), the problem has been discussed and the conclusion was that function pointers have to be convertible to void* for POSIX compliance. This requires compiler makers to implement a working cast for this case. If the contents of the library can be changed (i.e. in the case of a custom library), in addition to the function itself a pointer to it can be exported. Since a pointer to a function pointer is itself an object pointer, this pointer can always be legally retrieved by call to dlsym() and subsequent conversion. However, this approach requires maintaining separate pointers to all functions that are to be used externally, and the benefits are usually small.

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Dynamic loading

Start with the simplest possible case. Write down what Dynamic loading claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Dynamic loading before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Dynamic loading ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Dynamic loading

In research
Dynamic loading appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Dynamic loading in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Dynamic loading is common in secondary-school and first-year university syllabi. It links to neighbouring topics Computer libraries, Operating system technology, so understanding it makes those chapters shorter.
In everyday life
Look for Dynamic loading outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Dynamic loading” →

How to study Dynamic loading in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Dynamic loading means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Dynamic loading out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Dynamic loading in simple terms?

Dynamic loading is a mechanism by which a computer program can, at run time, load a library (or other binary) into memory, retrieve the addresses of functions and variables contained in the library, execute those functions or access those variables, and unload the library from memory. It is one of…

Why does Dynamic loading matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Dynamic loading?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Dynamic loading.

Tags

  • Computer libraries
  • Operating system technology

Keep exploring