6.3w+
社区成员
#include <iostream>
#include <string>
using namespace std;
struct TimeStamp
{
long year; ///< A year of a time stamp.
long month; ///< A month of a time stamp.
long day; ///< A day of a time stamp.
long hour; ///< A hour of a time stamp.
long minute; ///< A minute of a time stamp.
long second; ///< A second of a time stamp.
};
class TypeConv
{
public:
static TimeStamp toTimeStamp(const char* aString);
static TimeStamp toTimeStamp(const std::string& aString);
};
inline
TimeStamp
TypeConv::
toTimeStamp(const std::string& aString)
{
std::string str = ( aString.c_str() == 0 ) ? std::string() : aString;
if ( str.size() <= 14 )
str.append(" ", 14 - str.size());
else
str.append(" ", 14);
std::string strYear (str, 0, 4);
std::string strMonth (str, 4, 2);
std::string strDay (str, 6, 2);
std::string strHour (str, 8, 2);
std::string strMinute(str, 10, 2);
std::string strSecond(str, 12, 2);
TimeStamp timeStamp;
timeStamp.year = ::atol(strYear.c_str());
timeStamp.month = ::atol(strMonth.c_str());
timeStamp.day = ::atol(strDay.c_str());
timeStamp.hour = ::atol(strHour.c_str());
timeStamp.minute = ::atol(strMinute.c_str());
timeStamp.second = ::atol(strSecond.c_str());
return timeStamp;
}
inline
TimeStamp
TypeConv::
toTimeStamp(const char* aString)
{
return ( ::strcmp(aString, "") == 0 ) // 在此处会崩溃,用gdb调试时显示aString=0x0,为什么会这样,如何避免?
? TypeConv::toTimeStamp(std::string())
: TypeConv::toTimeStamp(std::string(aString));
}
int main()
{
cout << "begin" << endl;
string sTimeStr = "";
//const char* str = NULL;
TimeStamp time = TypeConv::toTimeStamp(sTimeStr.c_str());
cout << "end" << endl;
return 0;
}
inline
TimeStamp
TypeConv::
toTimeStamp(const char* aString)
{
if( aString == NULL ) // 判断了空指针的情况,但是发现实际是根本不会出现空指针,每次依然是以""的形式进入
{
return TypeConv::toTimeStamp(std::string());
}
else // 下面实际只是将原来的代码拆分开来写
{
if(::strcmp(aString, "") == 0)
{
//std::string tmpStr = "20080416";
//TimeStamp t = TypeConv::toTimeStamp(tmpStr);
TimeStamp t = TypeConv::toTimeStamp(std::string()); // 用gdb单步调试在这里会crash,堆栈信息如后附。
// 更为奇怪的是即使我使用上面注释掉的两行代码(使用常量)依然会出问题,
// 而且同样的参数传入的时候并不是每次都会crash,网上有资料称某些版本gcc
// 的basic_string构造函数存在sharded memory管理bug,可能会导致
// segfault,不确定是否是此原因。
return t;
}
else
{
TimeStamp t = TypeConv::toTimeStamp(std::string(aString));
return t;
}
/*
return ( ::strcmp(aString, "") == 0 )
? TypeConv::toTimeStamp(std::string())
: TypeConv::toTimeStamp(std::string(aString));
*/
}
}
basic_string(const _E *_S, const _A& _Al = _A())
: allocator(_Al) {_Tidy(), assign(_S); } // _Tidy使_Ptr=0
_Myt& assign(const _E *_S)
{return (assign(_S, _Tr::length(_S))); }
_Myt& assign(const _E *_S, size_type _N) // _N是0
{if (_Grow(_N, true)) // 返回false
{....}
return (*this); }
bool _Grow(size_type _N, bool _Trim = false)
{if (max_size() < _N)....
if (_N == 0)
{if (_Trim)
_Tidy(true); // 所以这次_Ptr还是0
else if (_Ptr != 0)
_Eos(0);
return (false); }
else....}
void _Tidy(bool _Built = false)
{if (!_Built || _Ptr == 0)
;
else if ...
else...
_Ptr = 0, _Len = 0, _Res = 0; }
int main()
{
cout << "begin" << endl;
string sTimeStr = "ABC";
//const char* str = NULL;
TimeStamp time = TypeConv::toTimeStamp(sTimeStr.c_str());
cout << "end" << endl;
return 0;
}
inline TimeStamp TypeConv::toTimeStamp(const char* aString)//aString是个指针 指向的空。
{
return ( ::strcmp(aString, "") == 0 ) ? TypeConv::toTimeStamp(std::string()): TypeConv::toTimeStamp(std::string(aString));
}
inline
TimeStamp
TypeConv::
toTimeStamp(const std::string& aString)// 该成string类型看看
{
return ( aString==0 ) ? TypeConv::toTimeStamp(std::string()): TypeConv::toTimeStamp(std::string(aString));
}
TimeStamp time = TypeConv::toTimeStamp(sTimeStr);//试试看看