Introduction - Error Handling
Error handling is necessary for any computer program. (Literally any.) The way C++ programs typically handle C++ is through exceptions.
The way to think about it is that a program has several layers of code, each doing something. For example, the top layer is main, which is responsible for running the entire programming, and maybe main calls some other functions to do stuff, and those functions call other functions. What happens if a function several layers deep encounters an unexpected problem, or has some problem and needs advice on what to do? The answer, with exceptions, is that it throws the problem back up to it's previous layer, and that previous layer has to decide what to do. C++ Try-Catch Blocks
Try-Catch blocks are the basic mechanism used to handle exception handling, so let's look at a very basic C++ try-catch example.
There are a few things to note:
This is the general idea of try-catch blocks: they're really just control statements that help our program flow as we want it too. But we need to know a few exception types that we actually need to watch out for (otherwise, we could just handle everything with if statements). Let's look at a few.
Exceptions
We threw int and string before, but there are more useful types to throw to really be descriptive about what the error is. There are quite a few C++ exception types, and cppreference.com has created a very nice list.
Let's go over one specific example to see how this generally works.
Output:
Program started. Inside the function. Stop trying to access an invalid index! Program ended.
What we do it use normal code to check whether an error will occur or not, and if so, we throw that type of exception, which gets handled by the calling function.
Conclusion
Exceptions are an important part of any program. If we don't use exceptions, then our programs will crash, or we'll get undefined behavior.
Using exceptions is pretty simple, but exceptions should be thought about at the design level, and not just in the implementation level. (Meaning, you need to think about errors and exceptions when you first make your codebase.) To see some more advanced C++ exception tips, watch John Kalb's exception presentation on youtube.
Like this content and want more? Feel free to look around and find another blog post that interests you. You can also contact me through one of the various social media channels.
Twitter: @srcmake Discord: srcmake#3644 Youtube: srcmake Twitch: www.twitch.tv/srcmake Github: srcmake Introduction - RAII
RAII (Resource Acquisition is Initialization) is a programming concept in C++. We're going to describe it very simply:
Problem: Memory leaks occur when heap allocated (new keyword) variables/objects don't get deleted because of unexpected problems. For example...
The above code has a memory leak, because a resource is initialized, but is never deleted. This is where RAII comes in...
Solution: Encapsulate variables in classes, and design the destructors to deallocate the resource from memory when the object itself is deleted.
The reason this works is because the object will have it's destructor called when it's scope ends (for example, when the throw happens). The best way to make this happen is with smart pointers. There are a few types, but we'll look at two in this blog post. Unique Pointers
Unique pointers are a good RAII technique for objects you only want to have one pointer too. (Meaning, only one pointer is allowed access to the variable/object.)
The output of the above code (compiled with c++14) is:
Program started. Shared Pointers
Shared Pointer is the same as unique pointers, except there can be multiple pointers to one variable/object.
The code will output the following:
Program started. Conclusion
RAII is an important C++ technique, where the code is designed so that even unexpected code breaking will assure that objects are cleaned up and memory leaks don't occur. We've seen how Smart Pointers, such as unique pointer and shared pointer, allow us to automatically handle RAII, but RAII is a design principle that can be applied to even our custom classes where any type of resource that we're responsible for needs to be designed carefully.
To explain it simply: Put a resource in the constructor of a class. Clean up that resource in the destructor of that class. And use a smart pointer to handle using that class. EZ.
Like this content and want more? Feel free to look around and find another blog post that interests you. You can also contact me through one of the various social media channels.
Twitter: @srcmake Discord: srcmake#3644 Youtube: srcmake Twitch: www.twitch.tv/srcmake Github: srcmake
References
1. www.fluentcpp.com/2017/08/22/smart-developers-use-smart-pointers-smart-pointers-basics/ 2. www.fluentcpp.com/2018/02/13/to-raii-or-not-to-raii/ 3. stackoverflow.com/questions/8480640/how-to-throw-a-c-exception 4. www.youtube.com/watch?v=ENj37HvptgU 5. stackoverflow.com/questions/22571202/differences-between-stdmake-unique-and-stdunique-ptr-with-new Introduction - GPU Programming
One of the main advantages of using C++ is that you have very finely tuned performance - you can control basically everything about your program, and don't use unnecessary resources of have the overhead that languages like Java and C# have. As such, C++ programmers should be very familiar with how CPUs and RAM work.
However, accessing the GPU is very beneficial: GPUs are specialized for performing mathemetical calculations, and so being able to do work (or offload work) onto a GPU, in addition to a CPU, makes for strong programming. In this blog post, we'll look at GPU programming by using CUDA. Prerequisite: You Need A GPU On Your Computer
Obviously, to program with a GPU, you need to actually have a GPU. Some laptops use CPUs with integrated graphics cards, which probably aren't CUDA enabled. For a list of CUDA-enabled GPUs, click here.
And if you're using Windows, you need Visual Studio installed. I recommend having Visual Studio 2017. (You should honestly have it anyway.) Here's a link to download it. Installation
To be able to compile C++ code that runs on the GPU, you'll need the CUDA toolkit. Click here to download it. Choose your operating system, architecture, and version. For installer type, choose local. (Either is fine, but I like local.) Click the download button, and follow the instructions that are written.
After the download finishes, launch the installer. Follow the on-screen instructions. (I recommend doing a Custom install, and making sure everything is checked.)
If you're on Windows, you need to make sure the PATH environment variable points to your visual studio bin, otherwise you'll get a "can't find cl.exe" problem when trying to compile. See the video to see how to set this. Hello World Code
So we need some actual C++ code to actually utilize our GPU. (Note, this code is taken (but modified) from this tutorial.) Put the following code in a file named main.cu
The code is simple: we have a function that adds to arrays together (into the second array). We have some code in main that allocates memory for two arrays, calls the add functions, checks if the computation worked, and then frees the memory. Basic programming.
Compiling and Running The Code
So we have our code file, now we need to compile it. Open a terminal in your current directory (for Windows, see the video if you don't know how that works), and to compile the code, in the terminal run:
nvcc main.cu -o run
This will generate our executable file. To run it, next in the terminal, run:
./run
The output will show "Max error: 0".
Next Steps
So we installed some CUDA tools and successfully ran some GPU enabled code on our computer. Now we need to do some more advanced programming to learn how to optimize our code and do even more. I recommend reading through this article and following the links there. It's possible that in the future I'll do more on this topic, so make sure to follow me using the links below.
Like this content and want more? Feel free to look around and find another blog post that interests you. You can also contact me through one of the various social media channels.
Twitter: @srcmake Discord: srcmake#3644 Youtube: srcmake Twitch: www.twitch.tv/srcmake Github: srcmake |
AuthorHi, I'm srcmake. I play video games and develop software. Pro-tip: Click the "DIRECTORY" button in the menu to find a list of blog posts.
License: All code and instructions are provided under the MIT License.
|