Reputation: 4655
I make such function to get required time info in string form.
string gettimeinfo()
{
tm * timeinfo;
time_t rawtime;
char timebuff[120] = {0};
time(&rawtime);
timeinfo = localtime(&rawtime);
strftime(timebuff, 120, "%d.%m.%Y. %H:%M:%S %x %W %I:%M %p %a %b %A %B %Z\n", timeinfo);
return string(timebuff);
}
This compiles without error and works as expected.
But when debugging on code:blocks/GCC program stops at return statement and debug warning appear:
In std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&) () ()
What happens and how to get rid of this?
EDIT: Whole error is:
In std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, unsigned int, std::allocator<char> const&) () ()
#1 0x004013fe in gettimeinfo () at C:\programi\[connectcpp\general.cpp:36
C:\programi\[connectcpp\general.cpp:36:785:beg:0x4013fe
At C:\programi\[connectcpp\general.cpp:36
In char* std::string::_S_construct<char const*>(char const*, char const*, std::allocator<char> const&, std::forward_iterator_tag) () ()
In std::string::_Rep::_S_create(unsigned int, unsigned int, std::allocator<char> const&) () ()
In operator new(unsigned int) () ()
Upvotes: 2
Views: 6900
Reputation: 67743
Firstly, you should at least check the return value from strftime
. If it returns zero, your buffer is invalid and you shouldn't try constructing a string from it.
Secondly, an error in new
often means your heap was damaged before this operation. If you stick a
std::string tmp("heap test");
before your return statement for example, and that fails in the same way, you definitely have some prior damage (sadly the converse is not true, since these things often aren't deterministic). Using valgrind or some other heap checker is a better bet.
Upvotes: 5