Re: Difference between C# and C++ or Java
Quote:
Originally Posted by MadHatter
if there is any documentation (or somebody can show that this is the case) on the matter, I'd love to see it, but everything I've ever read on .net said that assemblies are completely compiled at start up, and are executed as any other non-.net assembly after that.
I'd only question (in this) your notion of a virtual machine. If we defined a virtual machine as a well formed and specified interface to a particular computer cpu architecture, then the VMs only job is to get whatever code it reads in, into the architecture to do something useful.. ergo whatever part of the framework is responsible for "compiling assemblies at startup and [running them on the cpu]" is a virtual machine.. it may differ in modus operandi to the jvm, but its the same goal; a well formed language A is translated to well formed language B which is suitable for execution by the cpu.. both VMs
Re: Difference between C# and C++ or Java
a virtual machine is something that emulates its environment. Java's does so because its run across huge hardware and software differences.
.net simply does not do this. It runs on one platform, one instruction set, and is not in any way shape or form intrepreted in a "virtual" anything. Each assembly is loaded up into a runtime host which controls memory allocation and manages type and assembly resolution. that does not emulate anything. that contains no level of hardware or software abstraction and as far as I'm concerned cannot in any situation be considered a "machine" virtual or otherwise.
System.Windows.Control directly invokes Win32 code. System.IO.Stream calls the same platform api's in an object oriented manner. there is absolutely no "virtual" anything executing here, which is why a normally compiled C++ application may use managed extentions. what happens in that instance? is .NET pervasive enough to wrap all that native code in some sort of managed world? no, it doesnt. end of discussion.
C# and Java are compared quite often because they are the most recent programming languages, from 2 of the world superpowers in computing. Java's claim to fame is the ability to run a single code base on any computer. microsoft's has no such claim, nor vision. the bottom line is that people have no clue what they are using. .NET is similar to java in the same way that anyother C family of language is. because a comparison is made, and because .NET requires additional OS features to operate, people ignorantly jump to the conclusion that .NET is doing the same thing that Java does with respect to a virtual machine, running a layer above the hardware and softare, but the bottom line is that it doesnt.
.NET is no more a virtual machine than COM.
and if it is, please let me know how.
Re: Difference between C# and C++ or Java
From the book "Programming Microsoft .Net":
"The CLR sits atop the operating system and provides a virtual environment for hosting managed applications."
The CLR is Microsoft's implementation of CLI (Common Language Infrastructure), an Ecma Standard (http://www.ecma-international.org/pu.../Ecma-335.htm), that defines "...the Common Language Infrastructure (CLI) in which applications written in multiple high-level languages can be executed in different system environments without the need to rewrite those applications to take into consideration the unique characteristics of those environments."
The Mono project implements CLI so that .Net applications can run on different unix flavors.
To me this looks like a virtual machine.
Back to the original question: C# and Java are similar in syntax, at a conceptual level, and in performance (from figures I have seen doing comparisons between C#, Java, and C++). C# and Java compiles to IL/Bytecode, which later gets interpreted to machine code by JIT-compilation. With C# (and Java too I think) you can also compile directly to machine code, but then the code generated is for a specific platform and version of the .Net framework.
C++ is a superset of C, and it contains both OO and non-OO constructs. In C++, you have much more control of memory etc so you can write faster code - but this comes at a price, it is difficult, takes time, and is error-prone to code in C++.
Even though C# in theory is portable to Unix platforms, I would mainly consider C# for applications targeted for the windows platform. I have not used Java for a long time, but I know that it is being used for many different platforms including mobile phones. I would only use C++ for really performance critical applications. In most cases, performance is an issue of overall design, and much time spent by an application is in code outside of the application anyway (e.g. db, network, file system etc.).
Re: Difference between C# and C++ or Java
a "virtual environment" is not a "virtual machine"
I can't see how this is so confusing. you can host managed applications inside the clr, but this is not a "hardware" or "software" "Machine" abstraction. it is a system, or environment for managing allocation of system resources. Its the very same concept that you use in every other programming language (except .net does not strictly rely on the OS to provide everything it needs like other languages do). It is executed as a machine specific set of instructions.
in contrast, Java, while providing similar functionality, runs on top of the operating system, but intreprets the bl into machine code.
run a .net app. then run a java app.
here's an example:
zend studio (written in Java):
http://sanity-free.org/misc/zend.gif
bindmaker (an app I wrote in .net)
http://sanity-free.org/misc/czbm_proc.gif
notice that zend needs javaw to run. bindmaker does not. it is not "launched" with another application, "intrepreted" by another application, nor under the framework that it is written under "watched" by another application. It is not "run" by anything.
explain how you run something in a virtual machine when there is no virtual machine running when you run your .net application.
Re: Difference between C# and C++ or Java
Well, if I look at the nice picture in this article I see the runtime on top of the operating system and hardware. So it is a virtual machine for me. Maybe you want it call abstraction layer but it means the same. You do not have direct access to the hardware and you do not have direct access to the operation system. So where is the difference?
Your screenshots of the running processes do not proving if it is a virtual machine or not. It is only prove that the .NET runtime is maybe started in the same thread like the .exe file. So starting the runtime and the application in one thread or many threads is not the question and not the answer. If I build an empty Windows application with one form and a label which says "Hello world!" the application takes 15 MB of memory. If I build the same in C++ it only needs a few kBytes. So to all appearances the runtime is included and will be executed too.
No difference to Java in my mind.
Re: Difference between C# and C++ or Java
you could draw a similar picture for every programming technology in existance today (they all interface the OS directly, unless you're writing firmware).
it seems folks get "runtime" and "virtual machine" confused.
a virtual machine (like vmware, virtual pc, and jvm) allows code for one machine to be run on another. It uses hardare abstraction to interpolate one machine instruction to another.
windowing:
java: has to be able to window in X, windows, (whatever mobile devices use), and so forth
.net: directly wraps Win32 and GDI.
file system:
java: again, has to be able to use ntfs, nfs, fat, and all the million other file system types used throughout the unix / linux / windows worlds.
.net: the framework directly calls Windows Platform api's.
break out reflector and dig through the source code (or download it from microsoft).
anything that ran in a virtual world should not directly call **any** machine specific api's. that's the responsibility of a virtual machine. If you're running Vista on window 98 via vmware, what is happening when the operating system is reading or writing to a "physical device?" vmware emulates the hardware and passes those commands on through to the physical hardware.
do you honestly think java's source code implementation of stream would import kernel32.dll to read a file?
.net does. so how then does that work? does .net's vm intercept all these calls and re/write them into whatever OS they happen to be on?
.net does not use a "virtual machine". dont get this confused with a "runtime"
Re: Difference between C# and C++ or Java
According to this page: http://en.wikipedia.org/wiki/Virtual_machine the CLR is an application virtual machine.
According to Portable .Net and Mono project respectively, they both implement CLI virtual machines to allow .Net programs written in C# or other .Net languages to run on a number of different OS's.
According to Peter Golde the CLR can be considered to be a virtual machine (see http://msdn.microsoft.com/library/de...netconvers.asp, 6th Q and A)
According to Microsoft, Ecma 335 defines CLI - virtual machine (see: http://msdn.microsoft.com/netframework/ecma/), in the actual spec the term virtual execution system is used though.
So I would say that both Java and C# can run in virtual machines. The main difference is that the CLR VM can run different languages. (But I think I have heard about Ada compilers that generated Java byte code so that ada programs can run in a Java VM).
Re: Difference between C# and C++ or Java
If I were creating a programming language, I could not call it a "virtual machine" if I directly invoked platform specific api.
the fact that parts of the framework only work on windows, not all of it works in the compact framework is a testiment to that.
IMO if it walks like a duck, and talks like a duck, then it must be a duck.
you can quote people all day long, but a language that can emit direct, specific assembly code, a language that directly invokes platform specific code from kernel libraries, cannot be considered a virtual machine.
so quote away, but the fact remains 1. that it does not emulate hardware, and 2. it directly executes platform api's (and not in imulation).
Re: Difference between C# and C++ or Java
Ok, so i took this rather silly java application:
Code:
public static void main(String[] args)
{
int i = 0;
int j = 0;
while (true)
{
i += j;
i ^= 1;
i ^= j;
i |= 1;
}
}
and attach my debugger to it. This is what i saw:
Code:
00AE3F9C mov edi,edx //
00AE3F9E add ebx,edi // ebx += edx (i += j)
00AE3FA0 xor ebx,1 // ebx ^= 1 (i ^= 1)
00AE3FA3 xor ebx,edx // ebx ^= edx (i ^= j)
00AE3FA5 or ebx,1 // ebx |= 1 (i |= 1)
00AE3FA8 test dword ptr ds:[820100h],eax
00AE3FAE jmp 00AE3F9C // loop back...
And these instructions looks very similar to what a compiled C++ (and probably C#) application would look like. It is not interpreted by some virtual machine, it's compiled to platform dependent machine language... but still I would say that the JVM is a Virtual Machine, and the same goes for the CLR.
- petter
Re: Difference between C# and C++ or Java
obviously.
if it didnt get intrepreted into machine lanugage then it couldnt run.
I can do pretty much the same thing with php (and you know thats intrepreted). never underestimate the power of a debugger.
lets look at the terms:
virtual: (right off the top) Having the power of acting or of invisible efficacy without the agency of the material or sensible part.
machine (I dont think we need a definition here): the computer (hardware, software, & peripherals).
the power of java's vm is to not require the physical / logical machine to run the java application. platform specific bindings are implemented in the intrepreter.
1. .net does not have an intrepreter (it has a compiler that generates a native machine language image. the fact that java may be able to produce native images is irrelevant)
2. .net does contain OS specific bindings (as the end all of how things are implemented)
when you're bound to physical / logical OS specific libraries IN your code base, you no longer have the abliity to bind to anything else (and if you're emulating, simulating, or pretending you're working on something that may or may not exist, or whatever those def.'s of virtual are, how do you get around this?). am I wrong?
here's are the references of a few .net libraries. I can did deeper and show how they are referenced in the inner implementation (as anyone w/ reflector can).
CLR
http://sanity-free.org/misc/clr1.gif
Windows
http://sanity-free.org/misc/clr2.gif
Web
http://sanity-free.org/misc/clr3.gif
Drawing
http://sanity-free.org/misc/clr4.gif
System
http://sanity-free.org/misc/clr5.gif
Services
http://sanity-free.org/misc/clr6.gif
if there is a similar tool for java, do the same thing and see if they have platform specific bindings. Though I havent done it I very seriously doubt that they do (because it would limit the hardware that java can run on), and you'd have to download a new version of java for ever piece of hardware that you want to install your app (which you do, but you're getting the virtual machine, not the library implementation).
now. please somebody show me how .net could be remotely considered a "virtual" machine. I dont need quotes from microsoft propaganda, unless it gives specifics on how the .net implementation is abstracted out, intrepreted / emulated / simulated, or virtualized on the machine.
Re: Difference between C# and C++ or Java
Actually .NET is a JITted language i.e. it is compiled at runtime into the machine language for whichever platform it is designed for.
By my understanding the original versions of Java were interpreted - and probably still are for web browser back ends but I wouldn't know about that.
When you compile a .NET application it compiles to an intermediate language (IL) which is very quick to be compiled at run time.
Once a piece of code is compiled it is held in memory in its compiled (i.e. machine language) form so that if it's used again then it doesn't need to be compiled twice.
C++ is ALWAYS compiled to machine language for whichever machine it is targetting (e.g. PC, Mac, Sun, Silicon Graphics etc).
However a major difference between Java and .NET between C++ (native C++ of course - there is C++/CLI which is a .NET managed language) is in memory handling.
In C++ you have to allocate and deallocate the memory you need yourself. If you don't do this it leads to memory leaks which (certainly in server applications) will build up over time and cause your application (or even your entire OS) to crash.
With managed languages such as Java and .NET memory is managed by a seperate process - in .NET it's the Garbage Collector.
There are other differences (reflection in .NET is an example) but I think that's enough from me for now. My apologies if I've covered topics which have already been dealt with.
As to whether these things run on a 'virtual machine' or not - who cares ? It's the philosophy of each of these languages which makes up their advantages and disadvantages and ultimately effects which language you use to write a particular piece of software.
But to be accurate I would say that .NET doesn't work on top of a 'virtual machine' but more of an 'invisible compiler'.
I'm not making any statements about Java - I know very little about it.
Oh and WildFrog - I'm interested in the 'test' statement in that machine language because I would have thought the Java JIT compiler would be clever enough to recognise that it wasn't necessary. :D
Darwen.
Re: Difference between C# and C++ or Java
Quote:
Originally Posted by darwen
Oh and WildFrog - I'm interested in the 'test' statement in that machine language because I would have thought the Java JIT compiler would be clever enough to recognise that it wasn't necessary.
I found that abit odd myself. I did a test with C# and there was a couple of bad optimizations there aswell. Like this one:
Code:
static void Main(string[] args)
{
for (int i = 0; i < int.MaxValue; i++)
for (int j = 0; j < int.MaxValue; j++)
{
// do nothing
}
}
Coming from a VC++ environment I would assume that everything was optimized away, but the JIT actually produced this:
Code:
00C90070 33 D2 xor edx,edx
00C90072 33 C0 xor eax,eax
00C90074 83 C0 03 add eax,1
00C90077 3D FF FF FF 7F cmp eax,7FFFFFFFh
00C9007C 7C F6 jl 00C90074
00C9007E 83 C2 02 add edx,1
00C90081 81 FA FF FF FF 7F cmp edx,7FFFFFFFh
00C90087 7C E9 jl 00C90072
00C90089 C3 ret
One thing I found as a positive surprise (atleast to me) was that it inlines and optimizes functions residing in different assemblies.
- petter
Re: Difference between C# and C++ or Java
optimizations are not typically done in debug build so that you can step through along w/ the disassembled code. release mode builds do.
Re: Difference between C# and C++ or Java
Yes, it was release mode. I started it from command line and then attached my debugger to it (with no knowledge of any IL or source code).
- petter
Re: Difference between C# and C++ or Java
Well of course the CLR has platform specific bindings, any virtual machine would, wouldn't it?