请帮助:无法释放堆对象的向量 [英] Please Help: can't deallocate a vector of heap objects

查看:54
本文介绍了请帮助:无法释放堆对象的向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,

我不明白为什么删除在破坏对象时效果很好,但是
无法破坏它的向量。您的任何评论都非常高兴

赞赏!

以下是代表此问题的程序:

========= ================================

int main(int argc,char * argv [ ])

{

FILE * input_file = fopen64(argv [1]," r"); //输入文件是216Mb,

// 10万行

[...在这里初始化一些变量..]

vector< m_op * GT; MVEC; //指向m_op对象的指针向量

//此循环将每个输入行解析为m_op对象,

//然后将它们插入到向量中/>
while(fgets(intput_line,30,input_file)!= NULL)//在一行中读取

{

[...赋值a ,b,c来自input_line ...]

m_op * ptr = new m_op(a,b,c);

mVec.push_back(ptr);

}

[断点#1]< - 进程内存大小:801Mb

// 1000万m_op对象的大小


//这个循环将删除向量中的每个对象

for(vector< m_op *> :: iterator it = mVec.begin();

it!= mVec.end();它++)

{

删除(* it);

}

mVec.clear();

[断点#2]< - 进程内存大小:763Mb //不应该是0Mb?

}

========================================== ======== ====

输入文件是216Mb,有1000万行。第一个循环将

为每个输入行创建一个m_op对象。在[断点#1],我使用

" top"检查过程,发现它需要801Mb的内存空间。所以我希望在运行第二个循环之后,它将是接近零的
。但是,在第二个循环之后,在[断点#2],

进程仍然保持763Mb。显然,向量已经被清除,但是1000万个对象仍然留在内存中。我无法弄清楚

为什么析构函数没有释放内存空间。


但是,如果我把

声明delete ptr在它的'new'之后,进入一个循环,

内存将始终保持808Kb,这意味着析构函数

已经正常工作。 />
============================================ ====== ====

while(fgets(intput_line,30,input_file)!= NULL)

{

[ ...从input_line分配值a,b,c ...]

m_op * ptr = new m_op(a,b,c);

delete(ptr) ;

}

================================ ================== ====

构造函数和析构函数很简单:

m_op :: m_op( int cyc,string& rw,string& addr)

:_cycle_no(cyc),

_rw(rw),

_m_addr( addr),

{

}

m_op :: ~m_op()

{

}

====================================== ============ ===

我不知道为什么析构函数在单个对象上运行良好,但是失败了

来释放一个向量对象。

环境是RedHat Linux,x86,gcc 3.2.2,编译:

g ++ -o prog1 main.cc -g

您的任何评论或帮助都非常感谢!

非常感谢,

查理

Dear all,
I don''t understand why "delete" works well on destructing a object, but
fails to destruct a vector of it. Any of your comment is highly
appreciated!
Following is the program that represent this problem:
=========================================
int main (int argc, char *argv[])
{
FILE *input_file = fopen64 (argv[1], "r"); //input file is 216Mb,
//10 millon lines
[... initialize some variables here ..]
vector<m_op*> mVec; // a vector of pointer to m_op objects
//this loop will parse each input line into a m_op object,
//and then insert them into a vector
while (fgets (intput_line, 30, input_file) != NULL) //read in a line
{
[... assign values a,b,c from input_line ...]
m_op* ptr = new m_op (a, b, c);
mVec.push_back(ptr);
}
[break point #1] <- process memory size: 801Mb
//size of 10 million m_op objects

//this loop will delete on each object in the vector
for (vector<m_op*>::iterator it = mVec.begin();
it != mVec.end(); it++)
{
delete (*it);
}
mVec.clear();
[break point #2] <- process memory size: 763Mb //shouldn''t be 0Mb?
}
================================================== ====
The input file is 216Mb, with 10 million lines. The first loop will
create a m_op object for each input line. At [break point #1], I use
"top" to inspect the process, and find that it takes 801Mb memory
space. So I expect after running the second loop, it will be something
close to zero. However, after the second loop, at [break point #2], the
process still holds 763Mb. Obviously, the vector has been cleared, but
the 10 million objects still remain in the memory. I can not figure out
why the destructor didn''t free up the memory space.

However, if I put
the statement "delete ptr" right after it''s "new", into a single loop,
the memory will always remain 808Kb, which means that the destructor
has worked properply.
================================================== ====
while (fgets (intput_line, 30, input_file) != NULL)
{
[... assign values a,b,c from input_line ...]
m_op* ptr = new m_op (a, b, c);
delete (ptr);
}
================================================== ====
The constructor and destructor is simply:
m_op::m_op(int cyc, string &rw, string &addr)
:_cycle_no(cyc),
_rw(rw),
_m_addr(addr),
{
}
m_op::~m_op()
{
}
================================================== ===
I got no clue why destructor works well on a single object, but failed
to deallocate a vector of object.
The environment is RedHat Linux, x86, gcc 3.2.2, compiled with:
g++ -o prog1 main.cc -g
Any of your comment or help is highly appreciated!
Many thanks,
Charlie

推荐答案

go ********* @ yahoo.com 写道:
我不喜欢理解为什么删除在破坏对象方面效果很好,但是没有破坏它的向量。您的任何评论都非常感谢!

以下是代表此问题的程序:

============= ============================
int main(int argc,char * argv [])
{
FILE * input_file = fopen64(argv [1]," r"); //输入文件是216Mb,
// 10万行
[...在这里初始化一些变量..]

[...]
[断点#1]< - 进程内存大小:801Mb
// 1000万m_op对象的大小
[...]

[断点#2]< - 进程内存大小:763Mb //不应该是0Mb?
[..]
I don''t understand why "delete" works well on destructing a object,
but fails to destruct a vector of it. Any of your comment is highly
appreciated!
Following is the program that represent this problem:
=========================================
int main (int argc, char *argv[])
{
FILE *input_file = fopen64 (argv[1], "r"); //input file is 216Mb,
//10 millon lines
[... initialize some variables here ..]

[..]
[break point #1] <- process memory size: 801Mb
//size of 10 million m_op objects
[..]
[break point #2] <- process memory size: 763Mb //shouldn''t be 0Mb?
[..]




无论你试图从进程内存大小中得出什么结论,它有'/ b $ b'与'删除'是否有效无关。这两个完全不相关的东西。内存可能在您的进程中可用

但不能用于其他进程。或者操作系统可能会决定让每个人都可以获得
。它不在语言中,而是在平台上。请

在comp.os.linux.development.apps寻求帮助。


V



Whatever conclusions you try to draw from "process memory size", it has
no bearing on whether ''delete'' works or not. Those are two totally
unrelated things. The memory may become available inside your process
but not to other processes. Or the OS may decide to make it available
to everybody. It''s not in the language, it''s in the platform. Please
ask for assistance in comp.os.linux.development.apps.

V




< go ********* @ yahoo.com>在消息中写道

news:11 ********************** @ z14g2000cwz.googlegr oups.com ...

<go*********@yahoo.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
亲爱的,

我不明白为什么删除在破坏对象时效果很好,但
无法破坏它的向量。您的任何评论都非常感谢!

以下是代表此问题的程序:

============= ============================
int main(int argc,char * argv [])
{
FILE * input_file = fopen64(argv [1]," r"); //输入文件是216Mb,
// 10万行
[...在这里初始化一些变量..]

vector< m_op *> MVEC; //指向m_op对象的指针向量

//此循环将每个输入行解析为m_op对象,
//然后将它们插入到向量中
while( fgets(intput_line,30,input_file)!= NULL)//读一行
{
[...从input_line分配值a,b,c ...]

m_op * ptr = new m_op(a,b,c);
mVec.push_back(ptr);
}

[断点#1]< - 进程内存大小:801Mb
// 1000万m_op对象的大小

//这个循环将删除向量中的每个对象
for(vector< m_op *> :: iterator it = mVec.begin();
it!= mVec.end(); it ++)
{
删除(* it);
}
mVec。 clear();

[断点#2]< - 进程内存大小:763Mb //不应该是0Mb?
Dear all,
I don''t understand why "delete" works well on destructing a object, but
fails to destruct a vector of it. Any of your comment is highly
appreciated!
Following is the program that represent this problem:
=========================================
int main (int argc, char *argv[])
{
FILE *input_file = fopen64 (argv[1], "r"); //input file is 216Mb,
//10 millon lines
[... initialize some variables here ..]
vector<m_op*> mVec; // a vector of pointer to m_op objects
//this loop will parse each input line into a m_op object,
//and then insert them into a vector
while (fgets (intput_line, 30, input_file) != NULL) //read in a line
{
[... assign values a,b,c from input_line ...]
m_op* ptr = new m_op (a, b, c);
mVec.push_back(ptr);
}
[break point #1] <- process memory size: 801Mb
//size of 10 million m_op objects

//this loop will delete on each object in the vector
for (vector<m_op*>::iterator it = mVec.begin();
it != mVec.end(); it++)
{
delete (*it);
}
mVec.clear();
[break point #2] <- process memory size: 763Mb //shouldn''t be 0Mb?




查找主题范围和物体的寿命然后是堆栈之间的

差异和堆以及何时以及如何恢复这些分配(这不是删除的内容)。


更好的是,编译一个什么都不做的程序,不分配任何东西并设置返回时的
断点。你有什么发现?


然后向我们解释为什么一个正在运行的程序的过程应该不会占用给定的一块内存。
占用一块特定的内存。或者是你的beleif断点#2

在正在运行的程序中不会发生?



Look up the subjects "scope" and "lifetime of objects" and then the
difference between "stack" and "heap" and when and how these allocations are
recovered (thats not what delete does).

Better yet, compile a program that does nothing, allocates nothing and set a
breakpoint on the return. What are your findings?

Then explain to us why a running program''s process should somehow not be
occupying a given chunk of memory. Or is it your beleif that breakpoint #2
does not occur in a running program?


gogogo_1 ... @ yahoo.com写道:
gogogo_1...@yahoo.com wrote:

我不明白为什么要删除在破坏
对象时效果很好,但是无法破坏它的向量。


对象被正确销毁。但你错误解释了你的调试器给你看的是什么。

int main(int argc,char * argv [])
{/ / while( fgets(intput_line,30,input_file)!= NULL)
{
m_op * ptr = new m_op(a,b,c);
mVec.push_back(ptr);
}

[断点#1]< - 进程内存大小:801Mb
// 1000万个m_op对象的大小

//这个循环将删除向量中的每个对象
for(vector< m_op *> :: iterator it = mVec.begin();
it!= mVec.end(); it ++)
delete(* it);
mVec.clear();

[断点#2]< - 进程内存大小:763Mb //不应该是0Mb?


这是一个操作系统问题。当你释放内存时,

并不要求进程将它返回到

操作系统。

但是,如果我把声明放删除ptr在它的新之后,进入一个循环,内存将始终保持808Kb


因为该过程只有一个对象存在于

一次,它只需要从操作系统的
请求一个对象的内存。当你分配第二个对象时,

它会重新使用第一个对象的内存。


如果你根本不分配任何对象,你可能需要

找到使用的内存略低于808Kb。

你的任何评论或帮助都非常感谢!

I don''t understand why "delete" works well on destructing a
object, but fails to destruct a vector of it.
The objects are destructed correctly. But you are misinterpreting
what your debugger is showing you.
int main (int argc, char *argv[])
{
while (fgets (intput_line, 30, input_file) != NULL)
{
m_op* ptr = new m_op (a, b, c);
mVec.push_back(ptr);
}

[break point #1] <- process memory size: 801Mb
//size of 10 million m_op objects

//this loop will delete on each object in the vector
for (vector<m_op*>::iterator it = mVec.begin();
it != mVec.end(); it++)
delete (*it);
mVec.clear();

[break point #2] <- process memory size: 763Mb //shouldn''t be 0Mb?
This is an operating system issue. When you deallocate memory,
there is no requirement that the process return it to the
operating system.
However, if I put the statement "delete ptr" right after it''s
"new", into a single loop, the memory will always remain 808Kb
Because the process only ever has one object in existence at
once, it only needs to request memory for one object from
the operating system. When you allocate the second object,
it re-uses the memory from the first object.

If you don''t allocate any objects at all, you will probably
find the memory used is slightly less than 808Kb.
Any of your comment or help is highly appreciated!



您是否考虑过将这些对象存储在向量中,而不是指针中的
?这将为每个对象节省四个字节

(即40Mb),使您的编码工作变得更加容易。


另外,从技术上讲,你应该在删除之前从

向量中删除指针。如果在

中删除指针它位于向量中,则向量包含一个不确定的

值,这可能会导致未定义的行为(即使

你对向量做的唯一事情是明确的)。


例如:

for(vector< m_op *> :: iterator it = mVec.begin();

it!= mVec.end(); it ++)

{

m_op * ptr = 0;

std :: swap(* it,ptr);

删除ptr;

}

mVec.clear ();


另请注意,向量上的clear()不需要释放向量使用的内存
。要做到这一点,你必须摧毁向量的
;一种方便的方法是:


mVec.swap(std :: vector< m_op *>());



Have you considered storing the objects in the vector, rather
than pointers? This will save you four bytes per object
(ie. 40Mb), and makes your job as a coder a lot easier.

Also, technically you should remove a pointer from the
vector before you delete it. If you delete a pointer while
it is in a vector, then the vector contains an indeterminate
value, and this could cause undefined behaviour (even if
the only thing you do to the vector is clear it).

For example:
for (vector<m_op*>::iterator it = mVec.begin();
it != mVec.end(); it++)
{
m_op *ptr = 0;
std::swap(*it, ptr);
delete ptr;
}
mVec.clear();

Also note that clear() on a vector isn''t required to free
the memory used by the vector. To do that, you have to destroy
the vector; one convenient way to do that is:

mVec.swap( std::vector<m_op*>() );


这篇关于请帮助:无法释放堆对象的向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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