Why Do We Need size_t?

1 mainIn statically typed programming languages, datatypes play a very important role. Some of common datatypes are int, float, char, double, etc. Different datatypes have different functionalities and we use them depending on our requirements. In modern C++ code, the type “size_t” is used a lot of instead of int or unsigned int. It appears in many different scenarios like parameters to string functions, standard template library (STL), etc. Ever wondered why we need to use it in the first place? Does it have any real advantage?   Continue reading “Why Do We Need size_t?”

The Cost Of Abstraction In Python

1 mainPeople who have been coding for years will tell you that abstraction is always a good thing. It makes the system more robust and tolerant to future changes. Recently, I’ve been working on some Python libraries that wrap C extensions. While working on those libraries, a couple of interesting things came up.This blog post is about one of those things, and how it affects the speed. When we talk about abstraction, we tend to think about hierarchies, encapsulations, and robustness in general. But what about the ways in which they are implemented underneath? Different languages implement this in their own way, and they are not always optimal. This is what we are going to talk about. Let’s dive in, shall we?   Continue reading “The Cost Of Abstraction In Python”

Why Is Python Slow?

1 mainWhen people talk about speed in the world of programming languages, they usually center the discussion around compiled vs interpreted languages. In this post, we will discuss two of the most famous languages on this planet, Python and C. I was recently playing around with this and I made a few pleasant discoveries. So I thought I should share them here. One of the biggest reasons as to why Python is slower than C is because of the dynamic typing feature in Python. While it may be true that dynamically typed programming languages are slower than statically typed languages, it may not be the major factor slowing down your Python code. The dynamic typing feature of programming languages like Python makes the interpreters harder to optimize. I guess this is the cost of having an extremely beginner-friendly language! But one thing to note is that there is a big difference between interpreter being harder to optimize and your code being slow. We’ve had years of research focusing on the best way to perform type checking at runtime in these languages, thus making this overhead negligible. So how do we understand why Python code is slower than C code? How do we write Python code that’s not slow?   Continue reading “Why Is Python Slow?”

Functors In C++

1 mainWhen you look at the word “functor”, the first thing that comes to mind is that it looks very similar to “function”. The word actually comes from a field called category theory, which is an abstract branch of mathematics. A category basically consists of objects that are linked to each other in certain ways. This is an extremely simplistic view of a very complex field of mathematics. But basically, that’s what it’s about! As it turns out, this mathematical concept is really useful in the world of programming. If used in the right way, it can be a very powerful tool in your coding arsenal. So what exactly is a functor in the context of programming? How do we use it?   Continue reading “Functors In C++”

Exploring The Hidden C++ Features

mainPeople have been using C++ for a long time now and most of us think that we are pretty well versed with it. Interestingly enough, as we spend more time with something, we keep discovering more things about it. I love it when the thing you have been using for so long turns out to have powerful hidden features. I encounter a lot of C++ in my day-to-day life and so I end up spending a lot of time with it. Over time, as I delved deeper into C++, I came across some abstruse features, some of which really surprised me! So I thought I should write about them. Let’s see what they are.   Continue reading “Exploring The Hidden C++ Features”

What Is Tail Recursion?

mainProgrammers deal with recursion all the time. It’s actually quite a nice concept, but not a lot of people use it because of the mystery associated with it. To be honest, there’s no mystery as such! Sometimes, a problem is too complex to solve because it is too big. If we can break the problem down into smaller versions of itself, we may be able to find a way to solve one of these smaller versions and then be able to build up to a solution to the entire problem. This is the entire idea behind recursion. Recursive algorithms break down a problem into smaller pieces which you already know the answer to. We can solve these smaller problems by applying the same algorithm to each piece, and then combine the results. I have discussed all this in complete detail here. Now that we have talked about recursion, what exactly is the title of this blog post referring to? What exactly is tail recursion and why do we need it?   Continue reading “What Is Tail Recursion?”

What Is Pointer Casting?

1 mainWe all know how important pointers are in C++. A pointer is basically an object that contains an address to a variable. We can use this address to access the value stored in that variable, and that variable is located somewhere in the memory. It’s like how we use an address to locate someone in real life! An interesting thing to note about pointers is that although they contain addresses, they don’t really care about what they are actually pointing to. It’s like looking at an address in real life and not knowing if it’s a house or a clothing store. Now you might ask, why is such a thing relevant in programming? Can we take actually advantage of this?   Continue reading “What Is Pointer Casting?”

What’s The Use Of Function Pointers In C++?

1 mainWhen we talk about C++ programming, functions and pointers are very important. A function is basically a series of steps packaged into a nice unit. A pointer is an object that refers to a value stored in memory. So why not just combine them and create something really useful! Function pointer is exactly what you think it is. A function pointer is a variable that stores the address of a function. We can call this function using the function pointer. But why would we want to do that? This looks like a roundabout way of doing things, right? What’s the purpose of having it in the first place?   Continue reading “What’s The Use Of Function Pointers In C++?”

Using Hash Function In C++ For User-Defined Classes

mainIf you are a programmer, you must have heard the term “hash function”. In its most general form, a hash function projects a value from a set with many members to a value from a set with a fixed number of members. We basically convert the input into a different form by applying a transformation function. Hash functions map data of arbitrary length to data of a fixed length. The values returned by a hash function are called hash values. An interesting thing to note is that hash functions are not reversible. This means that you cannot recover the original data from hashed values. This property makes it one of the most useful data structures known to mankind. Hash functions are used extensively in internet security. In this post, we will talk about C++ STL and how to use hash functions with user defined classes.   Continue reading “Using Hash Function In C++ For User-Defined Classes”

ifdef and ifndef

mainIf you have worked on a large code base before, you would have seen ifdef and ifndef statements a lot of times. In fact, it is a good practice to have them wherever necessary. It just adds more robustness to your code and if you are working in a team, it helps you avoid weird compiler issues. The preprocessor directive #ifdef is used to define conditional groups of code at the preprocessor level. Based on a condition, a piece of code may or may not be included in the program. The body of this directive is usually termed controlled text.   Continue reading “ifdef and ifndef”