How does a virtual machine work?

Posted by Martin on Stack Overflow See other posts from Stack Overflow or by Martin
Published on 2009-05-29T21:00:54Z Indexed on 2010/04/05 4:13 UTC
Read the original article Hit count: 381

I've been looking into how programming languages work, and some of them have a so-called virtual machines. I understand that this is some form of emulation of the programming language within another programming language, and that it works like how a compiled language would be executed, with a stack. Did I get that right?

With the proviso that I did, what bamboozles me is that many non-compiled languages allow variables with "liberal" type systems. In Python for example, I can write this:

x = "Hello world!"
x = 2**1000

Strings and big integers are completely unrelated and occupy different amounts of space in memory, so how can this code even be represented in a stack-based environment? What exactly happens here? Is x pointed to a new place on the stack and the old string data left unreferenced? Do these languages not use a stack? If not, how do they represent variables internally?

© Stack Overflow or respective owner

Related posts about virtual-machine

Related posts about dynamic-typing