简体   繁体   English

使用指针删除未分配的新内容

[英]Deleting what wasn't allocated with new using pointers

Update: Please Note, I am forced to return a pointer to the result since I need to support multiple operations in one line (will be used in python)更新:请注意,我被迫返回一个指向结果的指针,因为我需要在一行中支持多个操作(将在 python 中使用)

Today, I have faced the hardest problem in my programming career Ever, So I hope someone can help.今天,我遇到了我编程生涯中最困难的问题,所以我希望有人能提供帮助。

In graph_p.h I have:graph_p.h我有:

typedef struct Graph* Graph_P;

(Graph_P is a pointer for a Graph) (Graph_P 是一个 Graph 的指针)

While Graph Is another class which I defined虽然Graph是我定义的另一个 class

In graph_p.cpp I have the following functions:graph_p.cpp我有以下功能:

Graph_P create()
{
    try {
        Graph_P graph=new Graph;
        return graph;
    }
    catch (std::bad_alloc &) {
        std::cout << "Error: Allocation Failed" << std::endl;
        return nullptr;
    }
}

void destroy(Graph_P graph_p)
{
    delete graph_p;
}

And the following graphUnion function (which is our topic ):以及下面的graphUnion function (这是我们的主题):

Graph_P graphUnion(Graph_P graph_in1, Graph_P graph_in2, Graph_P graph_out) {
    try {
        *graph_out=(*graph_in1)+(*graph_in2);
//I have defined operator + between two graphs which returns a new graph built using default c'tor **not using new**

        destroy(graph_out);
        return graph_out;
    }
    catch (CException::exception &e) {
        std::cout << e.what() << std::endl;
        return nullptr;
    }
}

What's the problem?有什么问题?

In case operator + failed I'm deleting the content of graph_out which shouldn't be done.万一operator + failed 我删除了不应该做的graph_out的内容。

Suggested Solution:建议的解决方案:

Saving graph_out content in a temporary Graph_P object like this: graph_out内容保存在临时Graph_P object 中,如下所示:

Graph_P graphUnion(Graph_P graph_in1, Graph_P graph_in2, Graph_P graph_out) {
    try {
        Graph tmp=*graph_out;
        tmp=(*graph_in1)+(*graph_in2);
        destroy(graph_out);
        graph_out=&tmp;
        return graph_out;
    }
    catch (CException::exception &e) {
        std::cout << e.what() << std::endl;
        return nullptr;
    }
} 

What's the problem?有什么问题?

I am putting a value inside graph_out that wasn't allocated via new so if the user types destroy(graph_out) outside the function, that would be an undefined action since I read:我在graph_out中放置了一个不是通过new分配的值,所以如果用户在function之外键入destroy(graph_out) ,那将是一个未定义的动作,因为我读到:

You need to delete only what you new ed您只需要delete new编辑的内容

How may I fix this?我该如何解决这个问题?

typedef struct Graph* Graph_P;

This is a bad idea.这是一个坏主意。 Obfuscating pointer like this should be avoided.应该避免像这样混淆指针。


Problem is here:问题在这里:

Graph tmp=*graph_out;
// ...
graph_out=&tmp;
return graph_out;

You create a graph that local to the function.您创建一个 function 本地的图形。 It is automatically destroyed when the function returns.当 function 返回时自动销毁。 You return a pointer to that graph.您返回指向该图的指针。 The returned pointer will be invalid outside of the function.返回的指针在 function 之外将无效。 Attempting to delete or access the non-existing value through that dangling pointer results in undefined behaviour.尝试通过该悬空指针删除或访问不存在的值会导致未定义的行为。

I think the simplest solution would be to add if statement in destroy() function to make sure that graph_p isn't a nullptr:我认为最简单的解决方案是在destroy() function 中添加 if 语句,以确保 graph_p 不是 nullptr:

void destroy(Graph_P graph_p)
{
    if (graph_p != nullptr) {
        delete graph_p;
    }
}

I think it would be a good idea to make Graph_P a smart pointer (if you're using at least C++11) - memory allocation would be easier.我认为让 Graph_P 成为智能指针是个好主意(如果您至少使用 C++11)- memory 分配会更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 删除分配给指针的内存(动态数组) - Deleting memory allocated with pointers (dynamic array) 使用dynamic_cast转换它们后删除指针:分配的内存发生了什么 - Deleting pointers after casting them with dynamic_cast: what happenes to the allocated memory 在删除动态分配对象的指针向量中的元素之前,我需要做什么? - What do I need to do before deleting elements in a vector of pointers to dynamically allocated objects? 递归删除链表,使用new分配内存 - Recursively deleting a linked list, memory was allocated using new 指针和带有已分配内存的指针之间有什么区别? - What is the difference between pointers and pointers with allocated memory? 从堆栈上分配的对象指针向量中删除元素 - Deleting an element from a vector of object pointers allocated on the stack 删除用 new[] 分配的数组元素的问题 - Problem in Deleting the elements of an array allocated with new[] c ++删除用new分配的char指针 - c++ deleting char pointer allocated with new 如何计算所有使用 malloc 和 new 动态分配内存的指针和变量? - how to count all pointers and variables that using an dynamically allocated memory with malloc and new? 通过指针删除分配的内存不起作用 - Deleting allocated memory by pointer doesn't work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM