Free memory in a vector<char*>

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.
 
B

Barry

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");

you can
but better change vector said:
But if i do this, where does the memory allocated?
>

"dummy" is a string constant, it's a static const data
And If I can still
use this:

for_each(arguments.begin(), arguments.end() , DeleteObject());

to free all memory used by arguments?

No, since the memory is not on the heap
 
A

Alf P. Steinbach

* (e-mail address removed):
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?

In order not to mince words, all of the above is Wrong (TM).

Use vector<string>, and be done with it.

Don't use raw pointers, don't use memset, don't use memcpy, and above
all, don't think about efficiency (the usual reason for newbies doing
this) because first of all it doesn't count when the program is
incorrect, as above, and secondly, newbie ideas of efficiency are all
Wrong, i.e., the end result isn't efficiency anyway.
 
B

Barry

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");

you can
but better change vector said:
But if i do this, where does the memory allocated?

"dummy" is a string constant, it's a static const data
And If I can still
use this:

for_each(arguments.begin(), arguments.end() , DeleteObject());

to free all memory used by arguments?

No, since the memory is not on the heap
 
T

tragomaskhalos

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;
}
};

In addition to the other responses, if you're allocating using
char* arg2 = new char[20];
Then should really use a differentDeleteObject, called e.g.
DeleteArray, that uses delete[] ptr; rather than plain delete ptr;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top