B
brad.power
Hi All,
Often in C++ code I find myself having to allocate character arrays to
pass back to callers for error codes, or as topic/data in message
passing/observer type scenarios (When having them on the heap is
necessary to avoid them going out of scope before something else, like
another thread, has had a chance to process them). What I wanted was a
neat way to keep track of these allocations within the class, and
clean them all up nicely, without too much overhead.
What I hit upon is based on the following ideas:
1. std::string is just 'better' than char*, so use it!
2. std::vector, in its destructor, will attempt to destroy each object
within it.
So what I did was to declare a vector of std::strings inside
'SomeClass' - a class which needs to keep track of a lot of strings it
creates during its adventures:
class SomeClass
{
....
private:
std::vector<std::string> m_stringvector;
....
}
And then, in the body of some method in SomeClass:
std::string* s1 = new std::string();
std::string* s2 = new std::string();
//do something with s1 and s2 - i.e. set them and pass them back to a
function
//now add them to the class vector to keep track of them:
m_stringvector.push_back(*s1);
m_stringvector.push_back(*s2);
So the idea is that m_stringvector is just a container to keep track
of all of my allocated strings, and when SomeClass destructs,
m_stringvector will automatically free all the string objects.
To me, this seems like a good way to keep track, as long as:
1. strings created this way are not freed anywhere else
2. The program design is happy to have all the strings destruct when
the class destructs
3. The life of SomeClass is such that it will never end up holding
zillions of strings, hogging memory
4. The programmer remembers to push the strings onto the vector
Since I stumbled across this in my own programming, and havent seen it
in a book or as a documented technique, I wanted to run it past the
group for any comments you may have. Is it as good an idea as I think?
Are there any obvious (or not so obvious) gotchas or 'you shouldn't do
that' aspects to this? Is this an actual documented technique that I
am just ignorant of?
Thanks
Brad
Often in C++ code I find myself having to allocate character arrays to
pass back to callers for error codes, or as topic/data in message
passing/observer type scenarios (When having them on the heap is
necessary to avoid them going out of scope before something else, like
another thread, has had a chance to process them). What I wanted was a
neat way to keep track of these allocations within the class, and
clean them all up nicely, without too much overhead.
What I hit upon is based on the following ideas:
1. std::string is just 'better' than char*, so use it!
2. std::vector, in its destructor, will attempt to destroy each object
within it.
So what I did was to declare a vector of std::strings inside
'SomeClass' - a class which needs to keep track of a lot of strings it
creates during its adventures:
class SomeClass
{
....
private:
std::vector<std::string> m_stringvector;
....
}
And then, in the body of some method in SomeClass:
std::string* s1 = new std::string();
std::string* s2 = new std::string();
//do something with s1 and s2 - i.e. set them and pass them back to a
function
//now add them to the class vector to keep track of them:
m_stringvector.push_back(*s1);
m_stringvector.push_back(*s2);
So the idea is that m_stringvector is just a container to keep track
of all of my allocated strings, and when SomeClass destructs,
m_stringvector will automatically free all the string objects.
To me, this seems like a good way to keep track, as long as:
1. strings created this way are not freed anywhere else
2. The program design is happy to have all the strings destruct when
the class destructs
3. The life of SomeClass is such that it will never end up holding
zillions of strings, hogging memory
4. The programmer remembers to push the strings onto the vector
Since I stumbled across this in my own programming, and havent seen it
in a book or as a documented technique, I wanted to run it past the
group for any comments you may have. Is it as good an idea as I think?
Are there any obvious (or not so obvious) gotchas or 'you shouldn't do
that' aspects to this? Is this an actual documented technique that I
am just ignorant of?
Thanks
Brad