C/C++ Pointers & Applications
C & C++ are very powerful languages when it comes to performance & flexibility. But there are some features that are complex and take time to master. One of such features is pointers. Pointers is what separates C/C++ from other languages. These are incredibly powerful as they allow programs to access memory directly and manipulate it.
This course focuses on pointers and their applications.It leans more towards implementation in C++, rather C. You’ll learn the basics of pointers and then move on to understanding and implementing arrays, pointers to arrays & heap based arrays. You’ll also learn advanced memory management by creating a custom dynamic array (just like std::vector<T> in standard C++ library). You’ll use placement new & delete to directly place objects in a memory pool allocated through operator new function. As you’ll see later in the course, this is a powerful mechanism to optimize usage of heap memory with user-defined objects.
After arrays, you’ll learn how to use pointers to create node-based data structures. We’ll focus on two types of linked lists – singly & doubly linked lists. You’ll understand the difference between arrays and lists and also learn how to access the elements of both the data structures without having to know their internal structure. This is possible by creating context variables that allows access in a container-agnostic manner.
Pointers are invaluable while working with strings. You’ll learn how to create dynamic strings using pointers. This will be shown with an implementation of a string class.
The next important topic you’ll learn and implement will be function pointers. You’ll understand how function pointers work and how we can simplify their syntax. You’ll also master the complexity of creating arrays of function pointers and functions that return function pointers. On top of that, you’ll be comfortable with functions returning pointer to functions that themselves return pointer to other functions. Confused? See the section on function pointers.
That’s not all. You’ll also learn how to create pointer to members (which have even a more complex syntax than function pointers).
Furthermore, you’ll learn how to create callbacks through functions pointers. This course will show you how to optimize the callbacks through function objects. Function objects are more powerful than functions pointers as callbacks.We’ll use this knowledge and apply it in many examples to reinforce the concept of pointers to functions.
This course also introduces some commonly used containers of the C++ standard template library (STL), such as std::array, std::vector & std::list. By the time you hit these topics, you’ll already know how these are implemented internally. How about that!
This course relies on some modern C++ (C++11) features to simplify things, such as auto, std::initializer_lists, type aliases. Even if you don’t know about these features, the course has videos on these topics to get you started. Additionally, there are four full length videos dedicated to discussion on move semantics.
I hope you enjoy this course!
Introduction of the course.
Source Code for modern C++ lecture & introduction to pointers.
This lecture covers some basic features of Modern C++ (C++11/14). These features are used extensively in the subsequent lectures and make it easier to write code and make it more expressive.
Learn about the basics of pointers, stack, heap and data section.
This lecture will show an example of using pointers. We'll examine them using the memory window of Visual Studio.
Source code for arrays section
Introduces static arrays (arrays that are created on stack, at compile time)
This lecture demonstrates usage of arrays and their representation in the memory.
Demonstrates how arrays can be passed as arguments into functions.
Demonstrates how arrays can be passed by reference into functions through templates.
A gentle introduction to standard library std::array.
Learn how to create dynamic arrays on heap through new operator. You'll also see how dynamic arrays are represented in the memory.
This video discussed multi-dimensional static arrays and their syntax along with pointers to such arrays. You'll also learn how to pass multi-dimensional arrays to functions.
Here, you'll learn how to create multi-dimensional arrays on heap using new and how they are represented in heap memory.
Provides the code for the dynamic array and the Noisy class. This class is used for debugging in dynamic arrays.
Revisits dynamic arrays on heap and discusses their advantages in comparison with static arrays. This lecture also explains the Dynamic Array project along with the important functions.
In this part, we'll implement different kinds of constructors in the dynamic array class.
Here, we'll add accessor functions (back, front, getat, etc).
This lecture explains implementation of important functions such as Add & Insert.
Here, we'll discuss how to implement the erase functionality in the dynamic array.
This is the start of four-part discussion on move semantics. Move semantics is a very important part of C++11 as it greatly improves the performance of the code.
This part prepares the groundwork by explaining the basics of rvalue and lvalue references and also demonstrates different function overloads based on value types.
The second part discusses lvalues & rvalues with class objects.
Here, we'll discuss how move and copy semantics work.
In this part, you'll learn how to implement move constructor and assignment in the Integer class. You'll also understand when move semantics are used by the compiler for objects of the class
In this lecture, we'll implement move and copy semantics in the dynamic array class and understand how this improves the performance of our code.
The dynamic array class has several issues due to usage of new operator. This video explains all these issues in depth.
Placement new is a very powerful operator for creating an object without allocating memory. Using this operator, this lecture demonstrates how it can help us overcome the issues in the dynamic array class.
This lecture builds explains and demonstrates how the placement new operator can be used to dynamically & efficiently grow a heap-based array.
We'll create a new dynamic array in this video and implement smart reallocation technique in it. We'll start by implementing constructors and a destructor.
Here, we'll implement the Add function in the new dynamic array class.
Discusses implementation of the Insert function.
We'll add some more functions in the array class and also fix some bugs.
This lecture revisits the pros and cons of arrays - both static and dynamic. It also proposes an alternative data structure called list, which we shall discuss in the next section.
Singly Linked Lists
Source code for singly-linked list.
This lecture explains the basics of linked lists. You'll learn how pointers are used to create nodes which ultimately form a chain, called linked list.
We'll add the common functions in the list class, but implement them in subsequent videos.
We'll implement the Add function in this lecture.
In this video, we'll implement various constructors in the list.
In this video, we'll implement move and copy semantics in the list.
You'll learn how to insert a new element in the list, by creating a node and inserting at the correct position in the list.
You'll learn how to remove elements by erasing nodes from the list without breaking the chain of nodes.
To access the elements of the list, we expose the head pointer to the users. This is a violation of encapsulation and you'll learn how to prevent this.
We'll further encapsulate the List class. We'll also add support for element access without exposing the node pointers.
Doubly Linked Lists
Source code for doubly-linked list
You'll understand the basic structure of a doubly linked list. We'll start adding common functions in a list class.
In this video, we'll implement a few accessor functions (Front, Back).
You'll learn how nodes are added to doubly linked list at the front and back.
In this video, we'll implement support for element access through the Position context class.
We'll complete the implementation of the constructors.
In this video, we'll implement the functions to remove elements at the front and back of the list.
Here, you'll learn how new nodes can be inserted in the list by implementing the Insert function.
In this video, we'll implement the erase function and learn how to remove nodes from the list at any position.
Source code for Strings section.
Introduces the basic concepts of strings and how they're represented as arrays.
Shows the implementation of calculating the length of the string.
Shows the implementation of copying and joining/concatenating strings.
Shows how to create a duplicate of a string through dynamic memory allocation.
We'll put together the concepts learnt in the earlier videos of string and create a string class. This class will manage the memory allocation/deallocation automatically.
Source code for function pointers.
This lecture introduces the concept of function pointers.
Demonstrates function pointers with more examples.
Here, you'l learn how to pass function pointers as arguments to functions
This lecture explains how function pointers can be returned from functions as values.
More examples of function pointers as return values.
You'll learn how to create array of function pointers.
Shows how to return an array of function pointers from a function. You'll also learn how to create a dynamic array of function pointers on heap.
Pointer To Member
Source code for Pointer to member section.
Introduces the syntax of creating pointer to a members of a class.
Explains how to create a pointer to a constant members of a class.
Here, you'll learn how to simplify the syntax of using pointer to members.
This lecture explains how to create pointers to static members of a class.
Source code for Callbacks section.
In this lecture, we'll discuss an example of a Find algorithm that will serve as the foundation for learning callback concept later.
Here, you'll learn how function pointers can be used as callbacks.
We'll see another example of function pointers as callbacks in this video.
This lecture demonstrates usage of member functions as callbacks through templates.
This video explains the basics of function objects.
In this lecture, you'll learn and understand how function objects are used as callbacks instead of function pointers.
We'll go deep into the internals of how the compiler invokes a function pointer and a function objects. Consequently, you'll come to appreciate the advantages of using function objects as callbacks.
You'll learn the key differences between function pointers and function objects as callbacks. You'll also learn how to choose between the two when you want to use a callback.