G
gogogo_1001
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
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