读书人

为啥函数返回的 string 对象为空

发布时间: 2013-11-29 00:21:24 作者: rapoo

为什么函数返回的 string 对象为空?

string getDate(){
int t_iYear = 2013;
int t_iMonth = 11;
int t_iDay = 23;
int t_iHour = 16;
int t_iMin = 45;

string t_oStrTarget;
char* pBuf = (char*)malloc(kMaxStrLen);
if (pBuf) {
sprintf(pBuf, "%d-%d-%d %d:%d", t_iYear, t_iMonth, t_iDay, t_iHour, t_iMin);
t_oStrTarget = string(pBuf);
free(pBuf);
}
return t_oStrTarget;
}


string getDate(){
int t_iYear = 2013;
int t_iMonth = 11;
int t_iDay = 23;
int t_iHour = 16;
int t_iMin = 45;

string t_oStrTarget;
char* pBuf = (char*)malloc(kMaxStrLen);
if (pBuf) {
sprintf(pBuf, "%d-%d-%d %d:%d", t_iYear, t_iMonth, t_iDay, t_iHour, t_iMin);
t_oStrTarget = pBuf;
free(pBuf);
}
return t_oStrTarget;
}


string getDate(){
int t_iYear = 2013;
int t_iMonth = 11;
int t_iDay = 23;
int t_iHour = 16;
int t_iMin = 45;

string t_oStrTarget;
char* pBuf = (char*)malloc(kMaxStrLen);
if (pBuf) {
sprintf(pBuf, "%d-%d-%d %d:%d", t_iYear, t_iMonth, t_iDay, t_iHour, t_iMin);
t_oStrTarget.append(string(pBuf));
free(pBuf);
}
return t_oStrTarget;
}


为什么返回的 string 对象都是空的? c++
[解决办法]
没发现什么问题?楼主可以单步么?单步看看,什么时候置空的?
[解决办法]
引用:
Quote: 引用:

Quote: 引用:

Quote: 引用:

Quote: 引用:

Quote: 引用:

没发现什么问题?楼主可以单步么?单步看看,什么时候置空的?

 [Info_] [OptionLayer.cpp::addTextFields:77] addTextFields
1970-1-1 8:30 [Info_] [OptionLayer.cpp::persistServerIpAndPort:135] text_field_server_ip = 192.168.1.123
 [Info_] [OptionLayer.cpp::persistServerIpAndPort:140] text_field_server_port = 8888
 [Info_] [DataModel.cpp::parseJson:76] 加载已存在的数据模型文件~
 [Info_] [DataModel.cpp::parseJson:92] 文件的长度为:8087
 [Info_] [FakeEntryLayer.cpp::setUpDataModel:44] DataModel::sharedInstance
 [Info_] [HZNetMgr.cpp::getIpAndPort:136] 使用 UserDefault 中存储的 ip 以及 port
 [Info_] [HZNetMgr.cpp::getIpAndPort:140] ip = "192.168.1.123", port = 8888
单步跟了一下,发现有时候行,有时候不行

不行的时候,是偶然还是必然的阿?是在那里给置空的呢?

我上面三种写法那种比较好呢?

建议用第一个吧!其实适合实际代码才是最好的!

能解释一下么?

第一个直观,简单明了!第二个类型不匹配,可能因编译环境不同,有影响!第三个追加可能有为清空的脏数据!所以建议使用第一种,
[解决办法]
Horrible code, mix C/c++ together is the worst thing to do.
Just use stringstram


string getDate(){
int t_iYear = 2013;
int t_iMonth = 11;
int t_iDay = 23;
int t_iHour = 16;
int t_iMin = 45;

std::stringstream ss;
ss << t_iYear << "-" << t_iMonth << "-" << t_iDay<< " " << t_iHour <<":" t_iMin


return ss.str();
}


[解决办法]
gdb命令:
设置观察点(WatchPoint)
观察点一般用来观察某个表达式(变量也是一种表达式)的值是否变化了。如果有变化,马上停住程序。有下面的几种方法来设置观察点:
watch <expr>
为表达式(变量)expr设置一个观察点。一旦表达式值有变化时,马上停住程序。
rwatch <expr>
当表达式(变量)expr被读时,停住程序。
awatch <expr>
当表达式(变量)的值被读或被写时,停住程序。
info watchpoints
列出当前设置的所有观察点。
[解决办法]
写这种代码的,应该被打屁股!
[解决办法]
引用:
Quote: 引用:

Horrible code, mix C/c++ together is the worst thing to do.
Just use stringstram


string getDate(){
int t_iYear = 2013;
int t_iMonth = 11;
int t_iDay = 23;
int t_iHour = 16;
int t_iMin = 45;

std::stringstream ss;
ss << t_iYear << "-" << t_iMonth << "-" << t_iDay<< " " << t_iHour <<":" t_iMin
return ss.str();
}

why horrible?c++ is original support c, so why cannot i write code in this way?


没人强制你不可以这样写代码,但18楼的代码的确比你的好得多。

读书人网 >C++

热点推荐