English 中文(简体)
图形结构——点击器坏吗?
原标题:Graph structure - are pointers bad?
  • 时间:2015-10-15 20:05:26
  •  标签:
  • c++
  • pointers

我正在执行一个图表结构,以便在C++中找到道路算法。

当新edge子或 no子出现时,它储存在一个单独的病媒中,供图表级的脱轨器随后删除。 我之所以使用点,是因为它们提供了方便的图表导航;我可以简单地比较其地址,或者我可以通过创建点人媒介,然后直接利用这些点人通过图表浏览,从而形成一条路。

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

struct Node
{
    Data data;

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

我读过一些文章,介绍点人是坏的,应当避免或替换为智能点人(但甚至应当避免)。 或者说,良好的方案者根本不使用(除例外情况外)。 我的理解是,它们是传闻的来源、安全风险以及整体上难以正确管理(特别是在多面应用中)。

我的问题: 本案中的点名办法?

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

在答复的第二部分:

  • "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)."
问题回答

我将重复。 POINTERS ARE NOT BAD。 以友好的黄色信(C)印刷,并贴在墙上。 它们非常有用。 我从未看到过一个专业的C++方案,该方案设法完全避免点人。

Managing your own pointers is usually bad, unless you are working on pointers manager.

未经训练的标准记忆分配可能是高性能应用的一个瓶颈,但点击器是具有标准记忆分配的NOT恶名。

点击器“ARE”不是记忆泄露的来源,也不是安全风险。 点击器难以管理(一般说来不比书写好的节目更困难)。

If you do not want to use pointer, you ve choosen wrong language.

Why are pointers to be avoid if possible for performance reasons?
Because you have to follow them around in memory, they suffer from very bad cache locality.

How many owners can there be of an object?
There can be only one!
unless its a shared_ptr (which then is the real single owner). Hence you can use none-owning pointers as long as you only follow them and don t delete or transfer owner ship with them.

Will pointers magically update if what I point to move?
No! if you use a vector for storage and you exceed its capacity it will relocate rendering all pointers to it invalid. If your sure that you have enough capacity you can use pointers.

因此,在这种情况下,我会考虑以下结构,一个记忆点只是一个记忆指数,为什么不使用指数本身?

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

or more radically if you don t need to traverse all edges

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

What you can t do with this is:
a) delete any Node/Edge and move the rest up. b) move any of them including c) sort them

如果你去除,就记住在诺德两处都抹去了边缘。

If you got a lot of edges in each vector it might be relevant to sort them so you can use a binary search to find a specific one.

如果你想要使用点人,就应当确保每个点和边缘都有一个所有人。 将它们置于病媒之中,可确保这一点,但应认识到转基因,因为它使所有病媒都失效。

在需要点人之前,应避免点人。

此处,斜线要么包含其他节点或提及点。 在控制方面,Nto-the-N复制诺德之间保持共性将是一个 night梦。 某种提法不太复杂,不太可能造成问题。

But now managing ownership becomes a problem. Who owns the Node and is responsible for deletion when it s no longer used?

有了一张未经指示的图表,诺德可以自行拥有和自毁(<>条码>。 如果没有进一步的联系,或将其从诺德库中删除。

In a directed graph, a Node can t self-determine because it has no knowledge of who may still be pointing to it. Its fellow Nodes must be collectively responsible and free the Node when no Nodes touch the node. Tracking this is a fun management task, but also kind-of the point of a graph.

采用<代码>的便捷灯 边缘类别即为一切权利,只要添加的 no子不再被移走。 您仍然可以删除整个图表。 如果您的图表经常变动,我建议使用<代码>。

However the class Edge is completely obsolete.

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

This is sufficient for directed and undirected graphs. In both cases you only need to record outgoing edges. In an undirected graph you have to record the edge in both nodes (with a pointer in the other direction).

每个高层次的视频游戏室都使用像Cchar***矩阵这样的大量点子,它还为找工作提供了标准开始器测试。

So there is no probleme to use pointer for graphe, it s used principaly to link node between to create chained list in C, like you have do, also everywhere like binarie/quaternarie tree.

智能点是另一种解决办法,因为你说不暴露于记忆泄露,即有意使用,因为灵敏度高,而且它比简单的软件更容易在核心视频游戏发动机中听到。

But concerning your vector<*Edge> it s better to calculate max size of vector, and replace it by a pointer array, it s speeder for memory speed acces.

why they say it s bad to use pointer, because theire is a memory leak risk, and also because pointer are not align in memory ram, so you will up the number of cache miss and slow down your program speed ... du to cache miss segment repatriation.

So if you make something like that char Matrix2D, the problem is that pointer will be instanciate in memory everywhere in memory, so must force to align pointer instanciation like: char myPointer attribute((aligned(alignof(char*))));

or easyer alignas(alignof(char*)) char*** myPointer;

关于点人不好的说法完全是假的。 点击器是语言的一个重要部分,常常是相互参照一个数据结构的最佳(或仅仅是)方式。 甚至美国航天局(美国航天局)的《航天器性能和安全关键代码》也自由使用点人。 见它们的方案拟订标准:

毫无顾虑地利用点人,你会陷入困境,但这是生活中一切的。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签