DEV Community

Karan Prajapati
Karan Prajapati

Posted on

"5 Things I Wish I Knew Before I Started Learning C++"

Learning C++ was one of the most challenging and rewarding parts of my journey as a student. But like many others, I jumped into it without a few key things. That led to countless hours lost to confusing compilation errors (segmentation faults became my old enemy!!) and frustrating debugging sessions.

**Here are the five lessons I wish I had learned earlier.


1. Understanding Pointers Takes Time (And That's okay)

When you're first learning pointers, you're told this classic line:

"A pointer is a variable that stores memory address of another variable".

Sure, sounds simple until the * and & comes into play and makes you rethink you choices.
Try to reference a memory address that doesn't exists.

Here's the deal:

  • & is the address-of operator, it gives the memory address of a variable.
  • * is the dereference operator, it lets you access the value stored at a memory address.

Now the tricky part is knowing when to reference and when to dereference. Most pointer-related errors happens because you:

  • Try to dereference a pointer that points nowhere (nullptr).
  • Try to reference a memory address that doesn't exists.

The Warehouse Analogy That Helped Me

Think of your computer's memory as a big warehouse.

  • Variables, objects and data are items on shelves, each with a unique address.
  • A pointer is like a warehouse worker who doesn't carry the item, they just know where it is.

When you use &, you're asking:

"Hey, what shelf is this item on?"

And when you use *, you're asking:

"What's sitting on this shelf?"

Now imagine asking your warehouse worker to grab something from the shelf that's either empty or doesn't exist anymore. That's exactly what happens when you dereference an invalid pointer, you get undefined behavior, or more commonly, a segmentation fault.


2. The Standard Library Is Your Best Friend

One of the biggest mistakes I made was not understanding and learning the tools that the C++ Standard Library provided. Now we understand std::vector, std::map and maybe even std::set, but there is much more the STL provides, some of them are:

  • std::multimap, std::multiset, std::unordered_map, std::unordered_set
  • std::pair, std::tuple
  • The <algorithm> header which contains various useful functions like, std::sort, std::transform, std::remove etc...

The main reasons to use STL is because the methods it provides are,

  • Highly optimized.
  • Very Flexible.
  • Provides a much cleaner interface.
  • Saves a lot of time.

Investing time in understanding the different features of the STL, truly will save you a lot of time and energy while working with different problems.


3. Compilation Errors Are a Learning Tool

C++ error messages are known to be notoriously complicated and daunting to read through, especially template errors. But there are actually a lot of things you can learn from those error messages and actually use them to debug the error. It all matters to looking at the right place.

You might be wondering, how can errors even be useful? And that is a valid point, how can something that didn't go or run as expected be good or useful. But the thing is that when our program runs into an error there are couple of benefits to it especially when talking about C++,

  • They help spot the bug (obviously)
  • For template errors, they actually allow us to detect the template related errors which are not detected by our code editors given the generic nature of templates.
  • For segmentation faults, they allow to spot potential leaks which we are luckily able to spot, because segmentation faults are not some fixed patterns, they can happen anywhere if memory is not managed properly somewhere.

In-order to use errors messages, go all the way to the top (at the beginning) of the error message there you will see a clear stack of different files from where error is being evoked and that is really helpful to spot from which file and line did the error actually emerge.

And also some instructions are given for the reasons for why the error is happening which can be really helpful while debugging. If you will also try to read closely you will find some methods where being called internally and they failed, for example when using std::unique_ptr if we try to copy such a std::unique_ptr, then we would get some logs like calling an explicitly deleted constructor, which means we are calling a copy constructor which is not allowed in std::unique_ptr.

So the point is, if your fundamentals are clear about the features you use in C++, the errors will start to make some sense. But still C++ errors are not the most intuitive things to look at, so its okay to feel confused when looking at them.


4. Don't Ignore Memory Management

Coming from languages like Python or JavaScript, one doesn't have to worry much about memory much. But in C++, you have to.

One of the great features of C++ and what makes it so powerful is the immense level control it gives to user over the memory, using which very memory efficient application can created (and are created!), but with that being said we all know that famous line,

With Great Power Comes Great Responsibility.

If you don't manage you memory properly, not only will you waste resources but will introduce memory leaks and tracing and fixing memory leaks is no trivial task.

Here are few ways and good practices to manage memory properly in C++,

  • Understand RAII (super important!)
  • Understand stack vs heap memory
  • Avoid raw pointers wherever possible and use smart pointers.
  • Use STL containers and methods.
  • If you using raw pointers make sure to delete that memory when it goes out of scope using the delete keyword.
  • Don't mix new/delete with malloc/free, they work very differently.

5. You Don't Need to Master Everything At Once.

Trying to learn every feature, template, lambdas, multi-threading, all at once will burn you out. Focus on writing small programs. Use what is required by those programs and slowly build the muscle memory first.

When I was learning C++, I was also tempted to write that advanced level of code, where you use lambdas, templates etc, all the cool stuff that you see in existing C++ code bases for applications. As a result I tried to force myself to use this advanced features in relatively simpler application where a simple solution would be optimal, resulting in over-complicating the relatively simple program.

So here's the thumb rule that I stick to,

"The deeper and complex stuff will make more sense when you need it".

For example, I had this temptation to write such template based function which can be generic and can pass in callbacks (lambdas) and take n number of parameters of different types. And at the beginning I didn't find a place where such an implementation was really required but, when I was working on a multi-threaded application ones, I had to implement my own thread-pool, so there I had to create a function which would take in a callback and that callback can have n number of parameters of different type, and that callback had to be registered to the thread pool.

So the point is that every single features exists for a specific use case and you will know when you need it and you will be required to use it, so rush on using the advanced features where its not needed, in complex projects you will have plenty of opportunities to use them.


Final Thoughts

C++ is hard, but that's also why it's beautiful. It forces you to understand what's happening under the hood. And once things start to click, its incredibly empowering.

If you're just starting out: take it slow, embrace the STL and don't fear error messages, they're teachers in disguise. You're not alone, we've all been confused too.

Now if you read till this point, rest assured because you are well on your way to becoming an C++ addict (like me!!).
Let me know if you're also learning C++, I'd love to hear what you're struggling with or what helped you!

Top comments (3)

Collapse
 
shruti_hiramankotgire_9 profile image
Shruti Hiraman Kotgire

Most of the people said to me that learn Java instead of C++. But still I stick to C++. Now I am middle of my learning and there is still confusion in my mind whether I continue in C++ or switch to another language?

Collapse
 
karan_prajapati_2004 profile image
Karan Prajapati • Edited

That's really good question, and honestly, a lot of people (including myself) face this same confusion when learning C++.
Here's the thing C++ and Java both have their own strengths, and choosing between them depends on you use case.

  • C++ is more low-level, so you get to understand how memory works, how system manages resources, and what happens under the hood. This deep understanding pays off in the long run, especially if you ever go into systems programming, game dev, embedded systems. No other language pushes you to learn those things as C++ does.

  • On the other hand, Java is more commonly used in enterprise applications, backend systems and Android development, so it's often the go-to for general programming roles.

But here's the cool part: if you've learned C++, picking Java feels much more easier, its like switching to a simpler syntax with automatic memory management.

Personally, I've learned multiple languages, and I can say that starting with C++ gave me a solid foundation. So if you're already halfway through, i'd encourage you to keep going, the confusion means you're actually learning. Things will click with time.

In the end, it's not really about "C++ or Java", you can (and probably will) learn both. But finishing what you've started with C++ will give you skills that translate far beyond just one language.

Hope this answers your question!!

Collapse
 
nathan_tarbert profile image
Nathan Tarbert

Pretty cool read. I always forget how much those early C++ headaches taught me. Feels good knowing other people hit the same walls.