On 8/7/2017 5:00 PM, Alf P. Steinbach wrote:
> On 07.08.2017 13:38, Juha Nieminen wrote:
>
>> (Also, a Java reference might not be directly pointing to the value
>> it's referring to, as is the case in C++. Quite often it will have
>> at least one level of indirection.)
>
> Practically true, but worth noting that strictly speaking it's not the
> case that pointers have no indirection C++.
I disagree: C++ (and C) pointers have no indirection as far as the
language goes, and its implementation too.
The fact is that what you refer to is not part of any language
implementation: it is how the hardware and the OS handle virtual memory.
More precisely, virtual memory is a technology that is supported by
modern CPUs (including the good old 80386 and subsequent processors)
that expose a virtual (either linear or segmented) address space to
processes, that is converted into paged (and/or swapped) memory storage
/internally/ by the processor. Support from the OS is also required to
set up all required structures and operations, including a.o. context
switching between processes (which are each assigned its virtual address
space), and swap storage management.
When I say managed /internally/ by the processor, I mean that any
userspace process code that needs to access memory does so by setting
some memory register (e.g. rdi) with an address in /virtual/ address
space, and the conversion into physical memory address is performed by
the hardware - there is no userspace process code for this. Even in the
case of a page fault, the CPU raises an exception which is handled by OS
code to load the page from swap space - this is all transparent to
userspace code.
From the Intel software developer manual:
"When an operating system or executive uses paging, the paging mechanism
is transparent to an application program. All that the application sees
is linear address space."
Not even userspace asm code can see this - in order to modify the
virtual memory system, privileged instructions are required.
This is how any program that is compiled into native machine code runs,
there is no language interaction with any of this.
By the way, virtual address resolution in the CPU is /fast/, it is
optimized directly in the silicon, since protected mode "is the native
operating mode of the processor" (from Intel's "system programming guide")
Java pointers indirection has nothing to do with virtual memory
addressing, since a Java program is hosted inside the Java VM (i.e. the
Java runtime that is the Java interpreter), an it is the Java VM that is
the actual process that runs on the CPU. Anything that happens in a Java
program happens on top of the Java runtime.
This is why I wrote in another thread that a Java reference/pointer is a
handle instead of a memory address, since it references an object that
is hosted by the runtime, and not an object that is directly allocated
in RAM as for a C and C++ object.
This may not be that much of a difference with respect to pointer
semantics (although it is in specific cases), but it is a substantial
difference in how pointer operation is implemented.