3 ms·
Not like C. They are not comparable. You get a reference to something you allocate on the heap, and you can have primitives on the stack. But you cannot take a
by Thiez 3y ago
Not like C. They are not comparable. You get a reference to something you allocate on the heap, and you can have primitives on the stack. But you cannot take a reference to a primitive, or a reference to another reference, or a reference to the second element of an array. To run C on the JVM you are really swimming uphill.
- kaba0 3y agoYou see &variable in a function body, you allocate a stackframe for that in the heap abstraction and return that’s address.
- Thiez 3y agoAnd now for a reference to a field in a struct living on the heap? Of course you can get C to run on the JVM (any CPU emulator written in Java proves that) but due to JVM limitations you cannot do so efficiently. Which is why it the JVM is not an attractive target for the 'traditional' languages.
- kaba0 3y agoThat struct on the heap is just consecutive bytes in an array, so you just return an integer.
- Thiez 3y agowarrenm was asking whether Java's (and implicitly: the JVMs) concept of references could be used. I'm glad that you agree with me that they cannot. Representing the memory of a compiled C program with a giant byte array would of course work, but also defeat the purpose of running on the JVM in the first place. You lose all the advantages that running on the JVM might have given you, and are slower than a natively compiled program.