Skip to main content

The case for unmanaged programming languages: data management

 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

Popular posts from this blog

Why you (yes, you!) should be using Telegram

 If you've not been living under a rock these past few months, you will have heard about the (now postponed) changes to WhatsApp's privacy policy. There are some problematic things about their privacy policy already, but I'll not be spending your precious time discussing legal and bureaucratic nonsense. Instead, I want to talk about and highlight some things that may or may not allow you to come to the (obviously correct) conclusion that you should be using an open-source messaging app like Telegram or Signal. Disclaimer: I am not sponsored by, nor was this article commissioned by, Telegram, LLC or any other company. I just happen to really like open-source software and think that Telegram is a great app. Now, as for why you shouldn't be using WhatsApp. Proprietary software is hazardous at best. In general, there are two types of software: proprietary and open-source. Proprietary being software where the underlying source code is not available to the end-user. Open-sour...

The genius of public-key cryptography

Most people have come across cryptography in some way, shape or form. To most people, it's just a way of encrypting a message in such a way that only the intended receiver can decrypt it. In this article, I will be exploring the benefits of public-key cryptography, most notably RSA, as well as explaining the basic concept behind computer cryptography for the uninitiated. Cryptography (intro) So, if this is the first time you've heard of cryptography, this section is meant for you! The basic concept of cryptography is relatively simple: You take a message and, combining it with some type of secret shared between sender and recipient, turn the message into something that cannot be understood without said secret. If you've ever come up with some sort of secret code in your childhood (or early adult life, no judgement here), you've probably done cryptography, even if you didn't know it. The simplest form of symmetric-key cryptography is the Cesar Cipher. The way it work...
Creative Commons Licence
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License.