删除返回值后的指针
delete pointer after return value
hello我有以下函数:
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);
}
我认为我有一个内存泄漏,我怎么能删除指针返回后?我如何使用析构函数来释放内存,我得到一个运行时错误坏:
解决这个问题最简单的方法,就像@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);
}
我把参数作为指针的唯一原因是这是你的TiXmlElement
的FirstChildElement()
和NextSiblingElement()
函数返回的。通常,我会让它们引用(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.
一般不"返回"指针。您可以将指针作为参数传递给函数,并在函数中为其赋值。由于指针是一个内存位置,当函数返回时,这个值将保留在指针中。然后,您可以在使用指针后进行任何内存管理。
相关文章:
- 将返回值存储在函数指针数组的指针中是如何工作的?
- 当我使用 void 函数的返回值(通过强制转换函数指针)时,究竟会发生什么?
- 如何读取指针的值,因为这个指针是函数的返回?
- 如何在不使用临时变量的情况下取消引用返回指针的函数的返回值?
- 如何返回值为NULL的变量指针
- 将指向数组的指针作为函数参数传递,这本身就是另一个函数的返回值?
- 存储指向函数返回值的指针
- 指针未正确返回值
- 动态指针引用数组由三元运算符返回值,但有异常
- 指针返回值的地址
- 此指针 - 按值返回 vs 按引用返回
- 将指针设置为函数的返回值
- (C )当在同一类中调用时,污点指针返回正确的值,而从MAIN调用时为0
- 即使在返回值之前释放后,数据仍在指针中仍然存在
- 返回指针、返回值或传入引用,这在C++中很优雅
- 为什么指针返回值,而不是地址
- 字符指针返回值垃圾
- 将引用强制转换为函数指针返回值中的指针
- 为什么指针返回值而不是地址
- 函数指针返回值时出现奇怪的seg错误