M
matth.schmitt
Hi,
I have a problem with using vector to transfer new data into a class.
Running valgrind on the following program gives
lost memory, but I'm not sure why.
Help appreciated,
Matthias
#include <vector>
#include <iostream>
typedef std::vector<int> TInt;
class mem
{
TInt demo;
public:
void set_mem(TInt &line)
{
std::cout << demo.capacity();
// I believe the problem is here: what was contained in
demo is now lost
// but could someone enlighten me pls?
demo = line;
}
mem()
{
for (int j = 0; j < 5; j++)
{
demo.push_back(j);
}
}
~mem()
{
// I already read that clear doesn't force vector to
free its space, how can I achieve
// this? resize() doesn't seem to have any effect
either...
demo.clear();
}
};
int main()
{
mem* test;
TInt d;
for (int i = 0; i < 10; i++)
d.push_back(i);
test = new mem();
test->set_mem(d);
}
I have a problem with using vector to transfer new data into a class.
Running valgrind on the following program gives
lost memory, but I'm not sure why.
Help appreciated,
Matthias
#include <vector>
#include <iostream>
typedef std::vector<int> TInt;
class mem
{
TInt demo;
public:
void set_mem(TInt &line)
{
std::cout << demo.capacity();
// I believe the problem is here: what was contained in
demo is now lost
// but could someone enlighten me pls?
demo = line;
}
mem()
{
for (int j = 0; j < 5; j++)
{
demo.push_back(j);
}
}
~mem()
{
// I already read that clear doesn't force vector to
free its space, how can I achieve
// this? resize() doesn't seem to have any effect
either...
demo.clear();
}
};
int main()
{
mem* test;
TInt d;
for (int i = 0; i < 10; i++)
d.push_back(i);
test = new mem();
test->set_mem(d);
}