Y
yinglcs
Hi,
I have a vector<char*>
vector<char *> arguments;
and I have code to free memory (from example in effective stl):
struct DeleteObject {
template<typename T>
void operator()(const T* ptr) const {
delete ptr;
}
};
for_each(arguments.begin(), arguments.end() , DeleteObject());
arguments.clear();
My question is I currently is doing this:
// allocate memory on the heap
char* arg2 = new char[20];
memset (arg2, '\0', 20);
memcpy( arg2, "dummy", strlen("dummy") );
arguments.push_back(arg2);
I wonder if I can simplify by doing this:
arguments.push_back("dummy");
But if i do this, where does the memory allocated? And If I can still
use this:
for_each(arguments.begin(), arguments.end() , DeleteObject());
to free all memory used by arguments?
Thank you.
I have a vector<char*>
vector<char *> arguments;
and I have code to free memory (from example in effective stl):
struct DeleteObject {
template<typename T>
void operator()(const T* ptr) const {
delete ptr;
}
};
for_each(arguments.begin(), arguments.end() , DeleteObject());
arguments.clear();
My question is I currently is doing this:
// allocate memory on the heap
char* arg2 = new char[20];
memset (arg2, '\0', 20);
memcpy( arg2, "dummy", strlen("dummy") );
arguments.push_back(arg2);
I wonder if I can simplify by doing this:
arguments.push_back("dummy");
But if i do this, where does the memory allocated? And If I can still
use this:
for_each(arguments.begin(), arguments.end() , DeleteObject());
to free all memory used by arguments?
Thank you.