gpt4 book ai didi

c++ - 返回值后删除指针

转载 作者:行者123 更新时间:2023-11-30 01:21:58 25 4
gpt4 key购买 nike

你好我有以下功能:

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),这样更安全,因为您不能传递 NULL

如果您出于性能原因确实需要避免复制,并且您的编译器不够智能,无法自动执行此操作,您可以使用 smart pointers ,它们是引用计数的,因此您无需担心删除它们。

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.

关于c++ - 返回值后删除指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17200490/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com