Help, I'm stuck at an embarassing error. This is the offending code:
string itos(int arg) {
ostringstream s;
s << arg; //<----- This is the offending line.
return s.str();
}
And here is the stack trace:
#0 0x4041f716 in _int_malloc () from /lib/libc.so.6
#1 0x4041e388 in malloc () from /lib/libc.so.6
#2 0x40355b6e in operator new(unsigned) () from /usr/lib/libstdc++.so.5
#3 0x40341845 in std::__default_alloc_template ::xsputn(char const*, int) () from /usr/lib/libstdc++.so.5
#9 0x40321c42 in std::ostreambuf_iterator --
-- Verdi March --
Strange... If I create a dynamic array of pointers (ClassA **c),
after initializing the array, unexpected things (all related to memory
allocation) would happen.
When I replace it with vector
On Wed, 25 Jun 2003 17:49:46 +0800
Verdi March
Help, I'm stuck at an embarassing error. This is the offending code:
string itos(int arg) { ostringstream s; s << arg; //<----- This is the offending line. return s.str(); } I was curious so I tested it on my system and it worked fine with a few minor mods (I used std:: rather than using namespace std). #include <iostream> #include <sstream>
std::string itos(int arg) {
std::ostringstream s;
s << arg; //<----- This is the offending line.
return s.str();
}
int main()
{
std::string s = itos(5);
std::cout << "itos(5) returns " << s << std::endl;
return 0;
}
While it would apprear that the offending line is the problem, it might
also be that your app possibly ran out of heap space (eg. straw that
broke the camel's back) or that you corrupted the freelist somewhere
else in the program and when the string is created withing the
ostringstream class, you crash. This is not an easy error to find. While
I'm familiar with Purify, you might try using Valgrind or step through
with gdb or use a tool like electric fence.
Memory allocation errors are one of the more difficult errors to find
because they tend to cause errors in unrelated sections of code.
--
Jerry Feldman
participants (2)
-
Jerry Feldman
-
Verdi March