A Programmer's Pain: C++ Scares the Brain Cells Out of Me
Why C++ is scary and some tips to make it less scary :)
The first horror movie I ever watched was IT
Well, the first horror movie I partially watched, because I ran away from my friends home and hid in my room for hours after Pennywise first popped up
It really scared me, because I had nightmares for the next couple weeks about walking next to a sewer and being dragged away by a clown.
For some reason, this memory popped up when I tried to learn C++ for the first time. Funny, because both horror movies and C++ made me want to run away… at first
Pennywise and C++ give me similar feelings of dread
C++ is painful, but we can make it less painful
I decided to learn C++ for a stupid reason, my friend Dan
Every time I would write code, he would be over my shoulder pointing out mistakes I made, talking about memory management and compilers and assembly and bytecode and a bunch of other stuff that made me feel stupid
So I went on a journey to become less stupid
Sadly it didn’t work, reading through C++ tutorials and trying to create my first C++ project (a tic tac toe game) seemed impossible
A lot of people go through this, especially for people like me where my background comes from learning basic programming languages like Python first (a lot of my friends learned C++ as their first language 😲). But learning C++ is the key to unlocking your computer
Sounds weird but I’m serious: minus object-oriented programming and libraries, C++ is basically a one-by-one match with assembly, and you can do and build literally anything with it
Seems cool, but it also comes with weird stuff like pointers, references, value types, linkage, and a whole stack of other things
So let’s go through some of these one by one, so you don’t have to go through the same pain I did :)
cppreference.com, where this list on the homepage shows maybe 20% of all you need to know about C++, in other words this proves C++ is pure pain
MEMORY MANAGEMENT AAAAAHHHH 🤬
We all know it, we all hate it, but most of us don’t understand it, so I’m gonna simplify it
Modern programming languages pamper us. We don’t have to deal with memory management, but C++ treats you like an ADULT, and lets you manage the internals of memory by yourself
Almost every variable you create gets put on a stack, which is allocated at the start of a program. Here, you don’t have to worry about things like memory leaks and scope, because the program handles it for you (static memory allocation)
- In C++, this is done by default with primitives, and is also done with objects as long as you don’t use the new keyword (like Object object = Object(…args) allocates statically, while Object object = new Object(…args) allocates dynamically)
If you do use a new keyword, or you create a pointer (we’ll get to that), that variable is put on the heap. The big thing about the heap is that whatever you put on it, you have to take it off at the end of the program, otherwise you have a memory leak
C++ is pretty famous for the asterisk, but what the hell does it mean? Good question! compared to something like int representing an integer, an int* represents a pointer to an integer. Basically, you are creating a variable that holds a memory address of another variable, and you can travel to that address to access the actual value (dereferencing)
And why would you want to do this? Well mainly because it makes you seem like a professional programmer, but it also allows you to allocate things on the heap (information pointed to by a pointer is stored on the heap automatically), allows you to mess with subclasses as base classes, and much more
Libraries 📚
If you’re used to pip or npm/pnpm with Python and JS, you might be wondering if there’s a cip or cpm in c++
Well you’re ✨in luck✨ because there isn’t 😔
instead, C++ developers use this trashy tool called cmake. Instead of installing using npm install, you have to install a C++ library directly on your system and link it using a bunch of annoying and buggy commands :_(
There’s two ways to do this:
Installing it using sudo apt-get install or any other command to get the binaries and libraries directly
(The more painful way) cloning the repo, running all the build commands, and creating the libraries and binaries yourself
You might see in your /usr/bin or build folder that after building/installing, there’s a bunch of .a and .so files. These are C++ equivalents of libraries. (like python packages or stuff in your node_modules directory)
To use them, there’s a couple commands in CMake: first you have to use find_library to let CMake locate it (you might have to configure a CMAKE_PREFIX_PATH, which tells CMake which directory to look for libs). Then, you have to create your executable (using add_executable), where you can add all the sources in YOUR project.
After that, you can reference that executable name and call target_link_library to add the library to your executable, and target_include_directories to add header files (these are hpp files, kind of where classes and methods are declared, while being defined in cpp files)
I don’t want to go on for too long, but these are the two main things I struggled with a WHOLE LOT when learning C++. It made no sense to me, but I learned over time through practice. It may make you feel stupid and annoyed, but learning C++ is the ultimate way to flex on all of your programmer friends. If you want to be a ✨sigma✨ developer, C++ is the path you must take
Also if you're still reading, here’s a list of important but weird standard library functions in C++
std::move - normally assigning one object to another create a copy, but this deletes the old version and directly moves everything to a new value (read about lvalues and rvalues for this, btw this one concept took me a week to understand)
std::variant - kind of like union types from TypeScript, it also comes with std::visit to do different actions based on the current type of the object
std::optional - this is a pretty cool one, its kind of like having your type be the type you specify or null. So basically if there’s an error you can return std::nullopt without causing a ton of compile errors
std::thread - a thread object, but don’t go into this yet because its a whole other beast
and here’s some quick mappings from c++ library functions to other languages
dictionaries/hashmaps - std::map
ArrayList/Python lists - std::vector
stacks and queues: std::stack and std::queue
Sorry for this post being pretty long btw 😅, C++ is a pretty big topic so I tried to condense it as much as I could