繁体   English   中英

返回值后删除指针

[英]delete pointer after return value

你好,我有以下功能:

Block* Keywords::parseBlock(TiXmlElement* element)
{
    double x1 = atoi(element->Attribute("left"));
    double y1 = atoi(element->Attribute("top"));
    double x2 = atoi(element->Attribute("right"));
    double y2 = atoi(element->Attribute("bottom"));
    double width  = abs(x2 - x1);
    int bid = atoi(element->Attribute("id"));

    vector<LineElement*> lines;
    for (TiXmlElement* sub = element->FirstChildElement("line"); sub; sub = sub->NextSiblingElement("line"))
        lines.push_back(parseLine(sub));

    return new Block(y2,x2,y1,x1,bid,width, lines);
}///End function parse Block

LineElement* Keywords::parseLine(TiXmlElement* element)
{
    double x1 = atoi(element->Attribute("left"));
    double y1 = atof(element->Attribute("top"));
    double x2 = atoi(element->Attribute("right"));
    double y2 =  atoi(element->Attribute("bottom"));
    int bid = atoi(element->Attribute("id"));

    vector<Element*> words;
    for (TiXmlElement* sub = element->FirstChildElement("word"); sub; sub = sub->NextSiblingElement("word"))
        words.push_back(parseWord(sub));

    return new LineElement(y2,x2,y1,x1,bid,words);
}///End function parse Line

Element * Keywords::parseWord(TiXmlElement* element)
{
    string w =element->Attribute("value");
    double x1 = atoi(element->Attribute("left"));
    double y1 = atoi(element->Attribute("top"));
    double x2 = atoi(element->Attribute("right"));
    double y2 = atoi(element->Attribute("bottom"));
    int bid = atoi(element->Attribute("id"));

    vector<Letter*> chars;

    for (TiXmlElement* sub = element->FirstChildElement("char"); sub; sub = sub->NextSiblingElement("char"))
        chars.push_back(parseChar(sub));

    return new  Element(w,y1, x1, y2,x2,-1,bid,chars);
}///End function parse word

Letter * Keywords::parseChar(TiXmlElement* element)
{
    string w = element->Attribute("value");
    double x1 = atoi(element->Attribute("left"));
    double y1 = atoi(element->Attribute("top"));
    double x2 = atoi(element->Attribute("right"));
    double y2 =  atoi(element->Attribute("bottom"));
    int bid = atoi(element->Attribute("id"));
    return new Letter(w,y1,x1,y2,x2,bid);
}

我认为我有内存泄漏,如何在返回指针后删除指针? 我如何使用析构函数释放内存,但遇到运行时错误bad:alloc

像@BalogPal所说的那样,最简单的解决方法是停止像Java那样对待C ++。 没有理由从任何这些函数中返回指针。 尝试这样的事情:

Block Keywords::parseBlock(TiXmlElement* element)
{
    double x1 = atoi(element->Attribute("left"));
    double y1 = atoi(element->Attribute("top"));
    double x2 = atoi(element->Attribute("right"));
    double y2 = atoi(element->Attribute("bottom"));
    double width = abs(x2 - x1);
    int bid = atoi(element->Attribute("id"));

    vector<LineElement> lines;
    for (TiXmlElement* sub = element->FirstChildElement("line"); sub; sub = sub->NextSiblingElement("line"))
        lines.push_back(parseLine(sub));

    return Block(y2, x2, y1, x1, bid, width, lines);
}

LineElement Keywords::parseLine(TiXmlElement* element)
{
    double x1 = atoi(element->Attribute("left"));
    double y1 = atof(element->Attribute("top"));
    double x2 = atoi(element->Attribute("right"));
    double y2 = atoi(element->Attribute("bottom"));
    int bid = atoi(element->Attribute("id"));

    vector<Element> words;
    for (TiXmlElement* sub = element->FirstChildElement("word"); sub; sub = sub->NextSiblingElement("word"))
        words.push_back(parseWord(sub));

    return LineElement(y2, x2, y1, x1, bid, words);
}

Element Keywords::parseWord(TiXmlElement* element)
{
    string w = element->Attribute("value");
    double x1 = atoi(element->Attribute("left"));
    double y1 = atoi(element->Attribute("top"));
    double x2 = atoi(element->Attribute("right"));
    double y2 = atoi(element->Attribute("bottom"));
    int bid = atoi(element->Attribute("id"));

    vector<Letter> chars;

    for (TiXmlElement* sub = element->FirstChildElement("char"); sub; sub = sub->NextSiblingElement("char"))
        chars.push_back(parseChar(sub));

    return Element(w, y1, x1, y2, x2, -1, bid, chars);
}

Letter Keywords::parseChar(TiXmlElement* element)
{
    string w = element->Attribute("value");
    double x1 = atoi(element->Attribute("left"));
    double y1 = atoi(element->Attribute("top"));
    double x2 = atoi(element->Attribute("right"));
    double y2 = atoi(element->Attribute("bottom"));
    int bid = atoi(element->Attribute("id"));
    return Letter(w, y1, x1, y2, x2, bid);
}

我将参数保留为指针的唯一原因是TiXmlElementFirstChildElement()NextSiblingElement()函数返回的结果。 通常,我会改为TiXmlElement &element引用( TiXmlElement &element ),这更加安全,因为您不能传递NULL

如果出于性能原因确实需要避免复制,并且编译器不够智能,无法自动执行该操作,则可以使用智能指针 ,这些指针已被引用计数,因此您不必担心delete它们。

std::shared_pointer<Block> Keywords::parseBlock(TiXmlElement* element)
{
    double x1 = atoi(element->Attribute("left"));
    double y1 = atoi(element->Attribute("top"));
    double x2 = atoi(element->Attribute("right"));
    double y2 = atoi(element->Attribute("bottom"));
    double width = abs(x2 - x1);
    int bid = atoi(element->Attribute("id"));

    vector<std::shared_pointer<LineElement> > lines;
    for (TiXmlElement* sub = element->FirstChildElement("line"); sub; sub = sub->NextSiblingElement("line"))
        lines.push_back(parseLine(sub));

    return std::shared_pointer<Block>(new Block(y2, x2, y1, x1, bid, width, lines));
}

// etc.

通常,您不会“返回”指针。 您可以将指针作为参数传递给函数,并在函数中为其分配一个值。 由于指针是内存位置,因此函数返回时,此值将保留在指针中。 将指针用于所需的对象之后,即可进行任何内存管理。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM