What memory address spaces are there?

后端 未结 4 2028
离开以前
离开以前 2020-12-29 05:59

What forms of memory address spaces have been used?

Today, a large flat virtual address space is common. Historically, more complicated address spaces have been used

4条回答
  •  [愿得一人]
    2020-12-29 06:37

    I would say you are asking the wrong question, except as historical curiosity.

    Even if your system happens to use a flat address space -- indeed, even if every system from now until the end of time uses a flat address space -- you still cannot treat pointers as integers.

    The C and C++ standards leave all sorts of pointer arithmetic "undefined". That can impact you right now, on any system, because compilers will assume you avoid undefined behavior and optimize accordingly.

    For a concrete example, three months ago a very interesting bug turned up in Valgrind:

    https://sourceforge.net/p/valgrind/mailman/message/29730736/

    (Click "View entire thread", then search for "undefined behavior".)

    Basically, Valgrind was using less-than and greater-than on pointers to try to determine if an automatic variable was within a certain range. Because comparisons between pointers in different aggregates is "undefined", Clang simply optimized away all of the comparisons to return a constant true (or false; I forget).

    This bug itself spawned an interesting StackOverflow question.

    So while the original pointer arithmetic definitions may have catered to real machines, and that might be interesting for its own sake, it is actually irrelevant to programming today. What is relevant today is that you simply cannot assume that pointers behave like integers, period, regardless of the system you happen to be using. "Undefined behavior" does not mean "something funny happens"; it means the compiler can assume you do not engage in it. When you do, you introduce a contradiction into the compiler's reasoning; and from a contradiction, anything follows... It only depends on how smart your compiler is.

    And they get smarter all the time.

提交回复
热议问题