Graph structure - are pointers bad?
15:58 15 Oct 2015

I am implementing a graph structure for a path finding algorithm in C++.

When a new edge or node is created, it is stored in a separate vector for later deletion by the graph class' destructor. I use pointers because they provide easy graph navigation; I can easily test nodes' identity by simply comparing their addresses or I can create paths by creating vector of pointers and later directly use these pointers to navigate through the graph.

struct Edge
{
    Node* from;
    Node* to;
}

struct Node
{
    Data data;

    std::vector outEdges;
    std::vector inEdges;
}

I have read articles about how pointers are bad and should be avoided or replaced with smart pointers (but even they should be avoided). Or that good programmers don't use them at all (with exceptions). I understand that they are sources of memory leaks, security risks and overall are hard to manage correctly (especially in multithreaded applications).

My question: is pointer approach in this case bad?

Edit 1: There are questions where did i read about pointers (smart) should be avoided. https://softwareengineering.stackexchange.com/questions/56935/why-are-pointers-not-recommended-when-coding-with-c/163279#163279

In a second part of his answer:

  • "Most uses of pointers in C++ are unnecessary."
  • "...modern C++ idioms often don’t need pointers at all..."
  • "For somebody who knows modern C++, it’s clear that you very rarely need any pointers (either smart or raw; except when using them as iterators)."
c++ pointers