图形结构 - 指针是否坏? [英] Graph structure - are pointers bad?

查看:106
本文介绍了图形结构 - 指针是否坏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C ++中实现了一个路径查找算法的图形结构。



当创建一个新的边或节点时,它被存储在一个单独的向量中,以供图形类的析构函数删除。我使用指针,因为它们提供简单的图形导航;我可以通过简单地比较它们的地址来轻松地测试节点的身份,或者我可以通过创建指针向量来创建路径,然后直接使用这些指针来浏览图形。

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

struct Node
{
数据数据;

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

我已经阅读了有关指针是如何坏的应该避免或替换的文章智能指针(但是应该避免它们)。或者好的程序员根本不使用它们(有例外)。我知道他们是内存泄漏,安全风险的来源,整体很难正确管理(特别是在多线程应用程序)。



我的问题:是指针法在这种情况下是坏的?



编辑1:
我有关于指针应该避免。
http:// programmers.stackexchange.com/questions/56935/why-are-pointers-not-recommended-when-coding-with-c/163279#163279



在他的答案的第二部分:




  • C ++中大多数指针的使用都是不必要的。

  • ...现代C ++成语通常根本不需要指针...

  • 对于那些熟悉现代C ++的人来说,很明显你很少需要任何指针


解决方案

我要去重复我自己。指针不是坏。用友好的黄色字母(C)打印,并钉在墙上。它们非常有用。我从来没有见过一个专业的C ++程序,设法避免指针完全。



管理自己的指针通常很糟糕,除非你在指针管理器上工作。



无限制的标准内存分配可能是高性能应用程序的瓶颈,但指针与标准内存分配不是同义词。



指针不是内存泄漏的来源或安全风险。指针不难管理(一般不比编写好程序更难)。



如果你不想使用指针,你选择了错误的语言。 p>

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<Edge*> outEdges;
    std::vector<Edge*> 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. http://programmers.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)."

解决方案

I am going to repeat myself. POINTERS ARE NOT BAD. Print it in friendly yellow letters (C) and pin on the wall. They are extremely useful. I have never seen a professional C++ program which managed to avoid pointers completely.

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

Unconstrained standard memory allocation can be a bottleneck in high performance applications - but pointers are NOT sinonyms with standard memory allocations.

Pointers ARE not source of memory leaks, or security risks. Pointers are NOT hard to manage (no harder than write good programms in general).

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

这篇关于图形结构 - 指针是否坏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆