Welcome back! This is the second and final part in a series of articles about why unmanaged programming languages are the best. In this article, I will be covering the ways in which unmanaged programming languages, such as C++, are far superior to managed languages when it comes to data management and manipulation.
Pointers
Pointers are literally the best thing since sliced bread... They seem
like such a simple concept, but oh man are they powerful!
Basically, a pointer is a variable that holds a memory address. It's actually a little more simple than that. A pointer, at it's lowest level, just holds an integer. This means you can do integer arithmetic with it. It might not be immediately obvious why this is useful, but I'll get to that in a minute.
The first thing pointers allow you to do that is pretty neat is to change the
data type of a variable very easily. Now, this isn't strictly speaking
converting the data to another type, it's actually just reinterpreting the
data as whatever you want, but it's still extremely powerful. One example
of where this is very useful is when storing data to a file. There are
generally two ways to accomplish this: the first is to convert all your data
to text and then write it to a text file. The second is to just write the raw
bytes to disk. Both approaches have their pros and cons, but generally, it's
very useful to be able to just write raw data to disk. Now, with managed
languages, it's usually not the easiest thing in the world to convert
arbitrary data to a byte stream, but in C++ it's literally one line of code!
Let me give you an example. In both examples, I will create an Entity class
with two variables: name and age. (For all intents and purposes, we'll accept
that name is an array of characters of length 16). The first example is C++,
the second is Java.
// C++
struct Entity {
private:
char name[16];
unsigned int age;
};
int main() {
Entity e = { "Hello!", 19 }; // Declare the entity
char* byteStream = (char*)&e; // Convert it to a byte stream
return 0;
}
// Java
import java.nio.ByteBuffer;
class Main {
public static void main(String[] args) {
Entity e = new Entity("Hello!", 19); // Declare the entity
// Allocate the required space
ByteBuffer bytes = new ByteBuffer(7 + Integer.BYTES);
// Convert name to bytes
for (char c : e.getName())
bytes.putChar(c);
bytes.putChar('\0');
// Convert age to bytes
bytes.putInt(e.getAge());
bytes.rewind();
}
}
So, in this example, you can see it takes only one line to convert, well, pretty much anything to a byte stream in C++. Conversely, in Java, it takes, well, many more lines. I'm actually too lazy to count, but it's more!
The other very useful thing pointers allow you to do is to do pointer arithmetic. This isn't really something I can explain in a small blog post, but without pointers, it is pretty hard to keep track of memory addresses sometimes.
Unions
Unions are another very useful concept in C++. They allow you to access the same part of memory as if it were two different variables with, crucially, different types. As long as those types take up the same amount of space. This allows you to do things like access a 3D vector as x, y and z, but also as r, g and b. This is generally very useful. The problem now is that I can't really do the concept of unions justice in this article. It's such a neat concept, but not the easiest thing to explain.
The most useful thing about unions are probably the memory management capabilities they offer. This combined with the way they allow you to manipulate memory is pretty amazing.
Exceptions
As with any common argument, there are always some exceptions to the rule. In my previous article, I said in the introduction that Python was one of the exceptions to the "unmanaged languages are more useful" rule. The reason for this is not really because of any of the reasons I've listed so far. Python does some very useful things, but one thing it does really well is simplicity. Many programming languages are complicated and hard to learn. Python is not. That said, it's still (technically) a managed language. Technically, it's not really even in the same category as Java or C++, since it's a scripting language, but it suffers from most of the same drawbacks as something like Java or, more accurately, JavaScript.
Conclusion
This concludes my series on why unmanaged languages are far superior to managed languages. As with most of my articles, I tried to keep it short. This might detract from my argument somewhat, but I think, in this case, listing only the core points is a useful strategy. Let me know what you think. Also, be sure to have a look at my previous article if you haven't read it. It's all about data allocation and how unmanaged languages do that better than their managed counterparts

Comments
Post a Comment