std::unique pointer in C++

Smart pointers are useful in the new C++ standard. Here’s a short post detailing why do we use std::unique pointer and some caveats.

  1. unique_ptr vs raw pointer
    1. Programmer is responsible for explicitly freeing raw pointers and keep tracking of the ownership. This poses a big problem when the C++ program gets big and it’s hard to figure out the life time of a heap allocated object, leading to memory leaks.
    2. with unique_ptr, the heap-allocated object is deallocated when the pointer goes out of scope. There can only be one owner for the unique pointer.

For example

#include <memory>
#include <iostream>
using namespace std;

struct Car{
  Car(string make): make_(make){}
  ~Car(){cout << "Sold";}
  string make_;
};

int main(){
  auto p = make_unique<Car>("toyota");
  return 0;
}

If you run the above code, it will show “Sold” even though there’s no explicit free / delete on p. The pointer p goes out of scope after main has finished and the destructor is automatically invoked.

There can only be one unique pointer that owns the object on heap. As a result, you cannot make a copy of a unique pointer. However, you can transfer the ownership of the object with std::move

auto q = make_unique<int>();
auto p = q; // Error the object belongs to q and cannot be shared.

p = move(q); // OK, p now owns the object

Other options to consider

  1. if you can allocate on heap, that is the best. In the example above, just do Car p (“toyota”);
  2. you can also use std::optional.

Reference posts

https://iamsorush.com/posts/unique-pointers-cpp/

Advertisement
Posted in Uncategorized | Leave a comment

Casting shared pointers in C++

Here’s a note on my experience with casting a base class shared pointer to a derived class pointer.

Continue reading
Posted in Uncategorized | 1 Comment

Notes on Reservoir Sampling

Reservoir sampling is a useful technique when sample k items for a large data sets.

In this post I outline some ideas on how it works.

Continue reading
Posted in Uncategorized | Leave a comment

How to trim text in List of Figures / Tables (Latex)

Sometimes we have really long texts in the list of figures and list of tables part (\listoffigures, \listoftables). The text correspond to the captions of the figures and tables.

We can trim the text using an optional argument to the \caption command.

\caption[short text to show in list of figures]{longer text to show next to the figure.}

I learned this trick from the following post

https://tex.stackexchange.com/questions/152239/custom-text-in-list-of-figures

Posted in Uncategorized | Leave a comment

Get Pylint to work with Python 3 programs

Here’s my note on getting Pylint to work with Python3. The issue is that I also have Python2.7 installed on the machine. As a result, when I just run “pylint python3_program_name.py”, the script gives “invalid syntax” errors.

One trick I did was to use the following command instead

python3 -m pylint python3_program_name.py

This seems to triggers the pylint for Python3 as it executes the pylint with python3. The “-m” option runs library modules as a script.

I got the hint from this post here (https://www.reddit.com/r/learnpython/comments/822qsc/running_pylint_for_python3/).

I also just realized that pylint is also doing static code analysis and can detect many errors statically like a compiler, which is very cool!

Posted in Uncategorized | Leave a comment

Reducing space in latex

Some useful tricks I learned from past collaborators.

Continue reading
Posted in Uncategorized | Leave a comment

Spell Checking for Latex

This is a post summarizing my experience performing spell check for my latex files.

Continue reading
Posted in Uncategorized | Leave a comment

Learning Tensorflow 2.0

Some notes on learning how to use Tensorflow 2.0

Continue reading

Posted in Uncategorized | Leave a comment

Resources on Polyhedral Compilers

Here’s are some useful resources for learning about polyhedral compilation.

Continue reading

Posted in Uncategorized | Leave a comment

Reading C / C++ Types

Some notes on reading C and C++ types (go right, and then left, then right … )

Continue reading

Posted in Uncategorized | 1 Comment