E
Ellarco
Im sorry for asking a question that is surely in the archives somewhere, but
I have been unable to locate it.
Its about string memory management. I need to dynamically construct a
C-style string and was planning to go about it this way;
char* foo()
{
std::stringstream ss;
ss << ...
std::string s = ss.str();
char* c = s.c_str();
return c;
}
void main()
{
char* c = foo();
// work with c
delete(c);
}
What am I doing wrong? Should I be deleting stuff somewhere? Will the foo
return a pointer to some out of scope memory - should it be;
char* foo()
{
std::stringstream* ss = new std::stringstream();
*ss << ...
std::string s = ss->str();
delete(ss);
char* c = s.c_str();
return c;
}
Am I deleting too much? ...
Thanks for any guidance you can offer.
El.
I have been unable to locate it.
Its about string memory management. I need to dynamically construct a
C-style string and was planning to go about it this way;
char* foo()
{
std::stringstream ss;
ss << ...
std::string s = ss.str();
char* c = s.c_str();
return c;
}
void main()
{
char* c = foo();
// work with c
delete(c);
}
What am I doing wrong? Should I be deleting stuff somewhere? Will the foo
return a pointer to some out of scope memory - should it be;
char* foo()
{
std::stringstream* ss = new std::stringstream();
*ss << ...
std::string s = ss->str();
delete(ss);
char* c = s.c_str();
return c;
}
Am I deleting too much? ...
Thanks for any guidance you can offer.
El.