I've heard a little about how with C++ you can inadvertantly make "unsafe code" especially with pointers. How are all the ways that C++ is "unsafe," how are some other languages safer, and what is the worst that can happen, more than that your program/computer freezes? Can long-term damage be done in ways other than losing data when your program and/or computer crashes?
What is the worst that can happen with "unsafe C++"code?
Well you are right about pointers being the real unsafe portion of unsafe c++ but the worst thing you can do is overwrite memory, even reserved system memory which would corrupt the OS and can cause a complete system crash. Most of the time a reboot is necessary and all is fine, but in rare situations it could corrupt system files and cause the computer to be unbootable.
Some other languages, particularly standard .NET languages, are safer because they no longer use pointers and have garbage collection to prevent leaks. They also make it harder for a programmer to write bad style of code which could lead to the data corruption.
Yes, long-term damage could be done using unsafe code and the reason it is being phased out. After all, virus'/worms usually work this way, corrupt or alter memory and propagate itself throughout the computer and its system files. Again making it unbootable or corrupting important files.
Vista is going to supposedly fight this by running all code in reserved memory or "sandbox" which is isolated from the system memory. We shall see how well that works.
Hope this explains the dangers of unsafe C++ code.
Reply:Comment about other answerer. I didn't say C++ is being phased out, I was talking about the pointers. Second you should always free memory yourself, never rely on garbage collection because it could collect at anytime, not just at the time it is not used. Lastly, he wasn't talking about Java at all. Report It
Reply:I'd make a couple of comments on the previous answer. First, any quality operating system will prevent programs from accessing memory outside their "sandbox". If Windows is only now getting this feature with Vista, it is a weakness of Windows.
Also, I'm not sure that C/C++ are really being phased out. This may eventually happen, but nearly all the software you currently use is written in C++.
Having said that, it's true that Java is a safer language than C++. I can think of at least three reasons why.
First, it is "strongly typed". This means that programmers have to specifically "cast" one data type to another. This reduces the chances that the compiler will do things with a variable that weren't intended.
Second, garbage collection removes the need for "freeing" memory. This eliminates the possibility of accessing memory that has already been freed, and no longer belongs to the program.
Third, all array accesses are checked against the bounds of the array. This may sound like a minor point, but a very large number of the security holes that allow viruses to propagate rely upon writing past the end of an array. Java disallows this, which makes for safer programs.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment