Understanding “Flat memory model” and “Segmented memory model”

computer-architecturememoryoperating systems

I am trying to understand Flat memory model and Segmented memory model.

Are Flat memory model and Segmented memory model determined by CPU architecture or OS or both?

Or are they not determined by either of CPU architecture and OS, but instead they are available options for programmers on any computer with any CPU architecture and installed OS?

If it is the latter, what kinds of programming need to consider Flat memory model and Segmented memory model? Assembly, C and/or other high-level programming languages?

Here are some quotes from Wikipeda that confuse me.

From http://en.wikipedia.org/wiki/Address_space#Memory_models:

Early x86 computers used the segmented memory model addresses based on
a combination of two numbers: a memory segment, and an offset within
that segment. Some segments were implicitly treated as code segments,
dedicated for instructions, stack segments, or normal data segments.
Although the usages were different, the segments did not have
different memory protections reflecting this.

Now, many programmers prefer to use a flat memory model, in which all
segments (segment registers) are generally set to zero, and only
offsets are variable.

From http://en.wikipedia.org/wiki/Flat_memory_model#Comparison:

Flat memory model

  • Not suitable for general computing or multitasking operating
    systems, unless enhanced with additional memory management
    hardware/software; but this is almost always the case in modern CISC
    processors, which implement advanced memory management and protection
    technology over a flat memory model. Linux e.g. uses a flat memory
    model compare X86_memory_segmentation#Practices.

Segmented memory model

  • Implemented in original Intel 8086, 8088, 80186, 80286 and
    supported by 80386 and all subsequent x86 machines through to present
    day Pentium and Core 2 processors. This memory model has remained ever
    since in the x86 machines, which now provide multi-mode operation and
    rarely operate in the compatible segmented mode anyway.

Best Answer

The memory model in use is determined by two things:

  1. The CPU in use, and what modes it supports
  2. The Operating System and what it uses

From a programmer's point of view, unless you're working on kernel code, you get what the OS gives you. Which in most modern operating systems is a paged memory model.

For code that will execute on a modern operating system in userspace, the memory model as far as the process is concerned is flat. This is because the OS provides a virtual memory space to the process that hides all of the paging, swapping, and other things that can happen to memory. This layer of abstraction is quite, quite useful since it doesn't force programmers to play nice with memory for the good of the whole system.

Historically, this wasn't always the case.

Windows 3.1x, MacOS up to v9, and Novell NetWare all lacked some of the memory protections we take for granted in Linux/Windows/OSX. NetWare in specific had one big memory space it provided to all running code, so one bug that attempted to access memory it wasn't actually assigned could actually get that memory and more often than not crash the whole system. Programmers programming for NetWare had to be very careful about memory management for this reason, and to an extent that Linux/windows/OSX programmers don't need to bother with.

And yet even NetWare used a Paged Model of memory, it just didn't provide each process with a virtual memory-space, it gave it the actual memory space.

The memory model in use is not flexible for all programming but the closest to the hardware. Kernel programming and embedded device programming are the areas where such things are very important.

Related Question