Chart structure - bad pointers?

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

When creating a new edge or node, it is saved in a separate vector for subsequent deletion using the graph class destructor. I use pointers because they provide convenient graph navigation; I can easily check the identifiers of nodes by simply comparing their addresses, or I can create paths by creating a vector of pointers, and then directly use these pointers to navigate the graph.

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

struct Node
{
    Data data;

    std::vector<Edge*> outEdges;
    std::vector<Edge*> inEdges;
}

, , ( ). ( ). , , ( ).

: ?

1: , (), . https://softwareengineering.stackexchange.com/questions/56935/why-are-pointers-not-recommended-when-coding-with-c/163279#163279

:

  • " ++ ".
  • "... ++ ..."
  • " , ++, , - ( , ).
0
5

. . (C) . . ++-, .

, .

, .

. ( , ).

, .

+4

, .

. N-to-N . - .

. Node , ?

a Node (delete Node), .

a Node , , . - Node, Nodes node. , .

+1

Edge , node . . , std::weak_ptr .

Edge .

struct Node
{
    Data data;
    std::vector<Node*> edges;
}

. . ( ).

0

, ?
, .

?
!
a shared_ptr ( ). , , .

, , ?
! , , . , , .

, : - , ?

struct Edge {
    // index into allNodes
    uint32_t from;
    uint32_t to;
}

std::vector<Edge> allEdges;

struct Node {
    Data data;

    // index into allEdges
    std::vector<uint32_t> outEdges;  // sort on to
    std::vector<uint32_t> inEdges;   // sort on from
}

std::vector<Node> allNodes; // contains all nodes

,

struct Node {
    Data data;

    // index into allNodes
    std::vector<uint32_t> outEdges; // sort
    std::vector<uint32_t> inEdges;  // sort
}

std::vector<Node> allNodes; // contains all nodes

:
a) Node/Edge . ) , c)

Edge , .

, , , .

, , node edge. , , .

0

The wrong pointer meme is simply incorrect. Pointers are an important part of the language and are often the best (or only) way to reference one data structure from another. Even NASA JPL uses pointers freely in its space-critical code on spacecraft. See Their Programming Standard: http://lars-lab.jpl.nasa.gov/JPL_Coding_Standard_C.pdf

Using pointers carelessly can cause you problems, but this is true for everything in life.

-1
source

Source: https://habr.com/ru/post/1611802/


All Articles