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...

Public Key Cryptography: The Mathematics of RSA

 Initially, when I created this blog, I stated that only opinions would be contained here. However, it has become very clear to me that I am obviously better at explaining things than I am at expressing complex opinions. And beyond that, the articles I enjoyed writing the most were the explanations about topics in computer science. That brings us to now. Moving forward, I'd like to shift my focus toward explaining topics I am interested in as well as continuing to express my concerns and opinions. I am, however, going to try to move more toward opinion articles that require some explanation as to the principles at work. On to the Maths!  Note: If, at this point, you haven't read my article on public-key cryptography, I would highly suggest that you read it before continuing. You can find it here . In this article, I will be explaining the mathematics of RSA. RSA is but one example of public-key cryptography and there are many out there, but today we'll only be looking a...
Creative Commons Licence
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License.