[Re]Learning C++

Hello everyone! My first L1T Blog. Super stoked :sunglasses:

I read an interesting thread with some community members discussing graphics and the sorts of shenanigans that spring up around that subject. I messaged an awesome community member and we spoke briefly about his experience with C++. I had been on the fence anyway, but he got me super jacked about the language. So, Iā€™m going to learn it.

A little background, this isnā€™t my first venture into programming. Not by a long shot. Hell, itā€™s not even my first go at C++ (hence the [Re]Learning in the title). I started with C++ in Computer Science. Spent a year working with it in an academic level, and then used it to create a file system (nothing to brag about, everyone in the class did it). Since then, Iā€™ve not used it. I spent a lot of time in college saying ā€œIā€™m never going to use thisā€ and either skimming or learning enough just to pass a test or project, and forgetting it within the semester.

Sadly, Iā€™m learning that all the things I said that about are what fascinate me at this point in my life. But, Iā€™m young and driven. This will be my attempt to learn C++ in a way that is practical and applicable to a modern environment.

My goal is to have three things.

  1. A functional system.
  2. A modern UI/UX.
  3. A helluva lotta fun!

A Functional System

Those are three words that donā€™t really mean anything. So, what do I mean? My primary goal is to have a clean design and robust system that functions well. Designing clean architecture is very, very challenging (ask any JavaScript dev LOL). There are a lot of programs, scripts, and code out there that are terribly put together, messy, and bloated. I am going to prevent that from happening. How?

  • Memory Management
  • Multithreading
  • Immutable Values

That last one might make a few of you quirk an eyebrow, but I think having certain values set in stone upon execution will definitely keep bugs and anomalies or side effects down to a minimum. Of course, with memory management comes knowledge of pointers, dynamic arrays, and other fun things in the toolbox. Itā€™s been a while, I have my work cut out for me.

Multithreading will be interesting. Iā€™ve tested a few things out from some juicy test code, and itā€™s not similar to Java at all :rofl: Iā€™ll probably have more control but also have high risk of errors and catastrophe. ARE WE EXCITED OR WHAT?

A Modern UI

The code sexy, hallelujah. Ahem, anyway. Nothing is a bigger turn off than bombing the UI, especially these days. Iā€™m thinking Qt is the obvious choice, hands down. But I am going to take a strong look at wxWidgets, too. Weā€™ll see. This is going to be a ways away, so Iā€™m not too focused on this right now. But, I do want to be prepared to keep rocking and rolling.

A Helluva Lotta Fun!

Thatā€™s why weā€™re here, yeah? So, letā€™s see what I have in store for meā€¦

Curriculum
Week 1:
Functions, Loops, Decisions, and Arrays

Iā€™m familiar with most of the above in several languages, and Iā€™m not a total newb to C++, but I want to start from the ground up. I have Stroustrupā€™s book to use, as well as Effective Modern C++ by Scott Meyers to bleed into. Iā€™ll also be referencing Algorithms by Sedgewick and Modern Operating Systems by Tanenbaum, two fantastic books that Iā€™ve used in the past for C, C++, and Java.

Week 2: Files, Strings, and Classes

Pretty much go hand and hand. If youā€™re going to do some serious programming, you need to know how to read from and write to files, manipulate strings, and create objects.

Week 3: Advanced Classes

Looks pretty good, but this is going to be using header files, the reasoning behind it, and priming your classes.

Week 4: Abstract Data Types and Linked Lists

Phew, tired yet? A month in Iā€™ll get into Structs, Linked Lists, and advanced file handling.

Week 5: TBD (Probably getting into C++ 11)

Week 6: TBD (More C++ 11, might start getting into the GUI at this point)

Week 7 - TBD: Dealing with the GUI, Databases, deployments, and system architecture. Iā€™ll flesh this out as I go, maybe make some videos if any of you are interested.

Environments

Windows 10 Professional with Visual Studio Enterprise 2017 ā€“ This was at the recommendation of a friend. Going to see how well it handles the modern stuff. Supposedly, despite all rumors, modern C++ is possible and incredible in Windows.

OS X with CLion ā€“ My portable workstation. If Visual Studio is enticing enough Iā€™ll see how it plays with OS X. Probably going to do the majority of my work in this environment.

Debian Stretch with CLion ā€“ This is my second workstation. I have a lot of C stuff setup on here, but I donā€™t see how that canā€™t seamlessly integrate into a C++ environment.

Ubuntu 18.04 with CLion ā€“ Went with the latest release after an incident with Fedora. So far itā€™s been working really well. I have CLion, VIm and some plugins, IntelliJ, and PHPStorm. Not much else, but I donā€™t need much else :sunglasses:

Anyway, thatā€™s it for tonight. I might start up tomorrow or I might start up after the weekend.

Lastly, I want to give a big shoutout to @pFtpr for going over some stuff with me. You probably had no idea what fire you were starting, but here it goes. Appreciate your insight and the discussion we had.

Until next time, happy coding everyone.

5 Likes

Might want to go close to the code in a LinuxVM and compile your stuff manually with clang (produces almost readable assembly code).

I use VisualStudioCode for my C++ adventures, in windows I use GCC, in Linux I use Clang++ to compile stuff.

ā€œHappyā€

1 Like

Whats are the big differences between GCC and CLang? Iā€™ve always used GCC and makefiles.

2 Likes

As said, Clang produces really clean and mostly readable Assembly code. For learners, I would recommend Clang as the errors and warnings are very readable. When compiling with clang, you may have to put -std=c++14 to ensure it uses the most recent c++ standard.

To my knowledge, GCC has the performance lead in the compiled programm and in compilation time (yay, multithreading!)

1 Like

Ahh, that wouldā€™ve been nice. Too bad our professors made us use GCC.

2 Likes

You recommend a VM so I donā€™t FUBAR my main rig?

happy :grin:

Wow, you werenā€™t kidding. I intentionally broke a little practice program.

Compiled with g++:

error: expected ',' or ';' before 'return'
return 0;
^

Compiled with clang++:

error: expected ';' at the end of declaration 
    int count = 0
                 ^

:+1: My man.

3 Likes

Holy shit that is better.

I remember hunting for tens of minutes in Vim before I found the culprit.

-____-

1 Like

Same. I have a plug-in now that points out somewhat obvious errors lol.

Of course, even proficient with Vim, Iā€™m the opposite of most. I still love RAM eating, flashy, bells and whistles IDEs lol :frowning:

JetBrains is my jam

1 Like

Now VSCode just shows that squiggly red line under errors because I have a linter. I drastically reduced the times I would compile to test and was able to focus on being productive.

1 Like

I go off and on with VS Code. Iā€™ll give it another go, you are the second to recommend it in here.

It makes me rage with Java projects, says components are missing every time I launch it.

Yeah, its better at C/C++, PS, Sh, or Web frameworks.

Pretty much anything except Java.

I hate java.

2 Likes

Some resources Iā€™ll be using for this venture.

1 Like

This is relevant to my interests.

1 Like

ā€œThe C++ Programming Languageā€ is quite the book. I donĀ“t think it is of any help for learning C++ but is more of an ā€œlevel up your skillsā€

1 Like

If you care about performance all of this stuff is worthwhile. https://github.com/dbartolini/data-oriented-design

Iā€™m not sure how important compile time performance (mostly sticking to C features) is anymore, but avoiding templates definitely helps speed up compilation, and std might still have enough issues where itā€™s worth just rolling your own libraries, (heavily templated) or using the EA std.

Slight correction: If you care about runtime performance just focus on optimizing code for hitting L1 and L2 caches. If you ever have to go to ram thatā€™s like traveling to the moon. Thatā€™s why some recommend custom data structures so you can avoid certain performance issues, in real time, from using stl libraries.

1 Like

Whats are the big differences between GCC and CLang?

Itā€™s true they are both open source cpp compilers, but clang was developed to be modular and extensible as a response to the largely non-extensible gcc. As a small example: clang as the front end to llvm has a flag

-code-completion-at

that spits out suggestions that things like company-mode for emacs use for intellisense.

And beyond that, libclang exposes a c api the front end that makes it a lot easier to write c++ tools.

1 Like

Clangā€™s also focused on compiling a whole hell of a lot faster. Iā€™ve had personal projects go from 30 second compile times in GCC to less than 10 with clang. (Was able to push it even lower by other optimizations) Another big advantage of Clang is it should be fully supported in Windows, thanks to some efforts from Google.

1 Like

Sadly, as I started to review everything, I became insanely bored. Iā€™m revising my original plan. I know how data types, functions, and arrays work lol. Kind of have to at this point.

As I was reading, I did remember growing an appreciation for C++ while in school. It has a way of engineering things in a smooth and easy way, and their exception handling is phenomenal.

An example I hacked and played around with was essentially a ā€œnot enough memoryā€ handling.

If you have a system that is allocating memory, you could wait for a crash or spin down the application. An example that I started with:

#include <iostream>
using namespace std;

void causeError();

int main()
{
    try
    {
        causeError();
    }
    catch(string &err)
    {
        cout << "Error caught: " << err.what() << endl << endl;
    }
    cout << "Press <Enter> to shutdown..." << endl;
    getchar();


    return 0;
}

void causeError()
{
    char *pHandler = new char[12345678998765432123456]
    delete[] pHandler;
}

In the above example, weā€™re going to try to call a function to allocate memory. With 4096 * 8 or something small, itā€™s not going to be an issue. However, with the number above, itā€™s going to yield a bad_alloc error, depending on your compiler. Rather than crap out the system, we can catch the error (by reference) and call the what() function that produces the specified error message. From there, we can gracefully shutdown the computer.

Decisions can be implemented to spin down automatically or offer the user to retry or something else. Another example I worked with even used a class for the allocation.

class CauseError
{
public:
    CauseError()
    {
        char *pMem = new char[9876543211122334455];
        delete[] pMem;
    }
};

You could try instantiating the object and then catch the same error. There are tons of other examples, and even custom exceptions you can write.

Fun stuff, I never handled exceptions or errors when working with C++, I enjoy the way C++ does it. Very efficient, and if thereā€™s not something there, just write your own :wink:

Good links:

http://www.cplusplus.com/doc/tutorial/exceptions/

See you guys next week! Had a few distractions (cough Python cough Golang) but I will have more time next week to focus. Going through a .NET bootcamp at the moment so that is a bit of a side track but it was free and I get paid to do it so :wink:

3 Likes