strlen(string.c_str()) != string.size() 的原因是什么
时间: 2024-05-22 20:15:32 浏览: 12
因为 `c_str()` 方法返回的是一个以 null 结尾的 C 字符串,而 `size()` 方法返回的是字符串中字符的数量,不包括结尾的 null 字符。所以,如果字符串中存在 null 字符,那么 `strlen(string.c_str())` 返回的长度会比 `string.size()` 小 1。此外,如果字符串中不存在 null 字符,则两者相等。
相关问题
String String::operator + (const char* s)const { String ret; long long len = m_length + strlen(s ? s : ""); char* new_str = reinterpret_cast<char*>(malloc(len+1)); if(new_str) { strcpy(new_str,m_str); strcat(new_str,s ? s : ""); free(ret.m_str); ret.m_str = new_str; ret.m_length = len;} else {//THROW_EXCEPTION(NoEnoughMemoryException,"No memory to creat new element ..."); cout<<"没有足够的内存异常,“没有内存来创建新元素"<<endl; }return ret;}
这是一个 C++ 中的字符串类 String 中的重载运算符 +,其作用是将两个字符串进行连接。具体实现如下:
1. 首先创建一个新的 String 对象 ret,用于存储连接后的字符串;
2. 然后计算出连接后的字符串的长度,为原有字符串 m_str 的长度加上第二个字符串 s 的长度(如果 s 为空,则将长度置为 0);
3. 分配空间,将原有字符串 m_str 和第二个字符串 s 拼接起来,并存储到新的内存空间 new_str 中;
4. 如果内存分配成功,将 ret 对象中原有的字符串 m_str 释放,并将新的内存空间 new_str 赋值给 ret 对象的 m_str 成员变量,并更新 ret 对象的 m_length 成员变量;
5. 如果内存分配失败,则输出错误信息并抛出异常;
6. 最后返回新的 String 对象 ret。
需要注意的是,在第 3 步中,如果第二个字符串 s 为空,则需要将长度置为 0,否则使用 strlen() 函数计算字符串长度时会出现错误。同时,在第 4 步中,需要释放 ret 对象中原有的字符串 m_str,否则会造成内存泄漏。
void ImageSearchMng::LoadCurFeaFile() { if (m_pImageSearch == nullptr) return; int nBufLen = 100 * 1024 * 1024; char *pReaFeaBuf = CSVNEW char[nBufLen]; if (pReaFeaBuf == nullptr) return; char cReadSection[64] = { '\0' }; for (int i = 0; i < SURPPORTSEARCHTYPE; ++i) { switch (i) { case 0: strcpy(cReadSection, FACEFILEPATH); break; case 1: strcpy(cReadSection, VEHICLEFILEPATH); break; case 2: strcpy(cReadSection, PERSONFILEPATH); break; case 3: strcpy(cReadSection, NONMOTERFILEPATH); break; default: memset(cReadSection, 0, sizeof cReadSection); break; } if (strlen(cReadSection) == 0) continue; FeaFileInfo l_FileInfo; std::string strConfigDir = ((CEvtImgStorage *)m_pUserData)->GetWorkPath() + MYSAVEFILEINI; GetPrivateProfileString(cReadSection, "LASTFEAFILE", "", l_FileInfo.cPath, MAX_PATH, strConfigDir.c_str()); if (strlen(l_FileInfo.cPath) > 0) { l_FileInfo.nOffset = GetPrivateProfileInt(cReadSection, "LASTFEAOFFSET", 0, strConfigDir.c_str()); l_FileInfo.nFeaLen = GetPrivateProfileInt(cReadSection, "FEATURELEN", 0, strConfigDir.c_str()); int nAlienLen = ALIGN_LENGTH(l_FileInfo.nFeaLen + 128, FILESETTIONLEN); sprintf(l_FileInfo.cPath, "%s\\Feature.idx", l_FileInfo.cPath); if (m_ImgSvrInfo.m_iSvrInitAddDone == 3) { if(l_FileInfo.nOffset > 0 ) ReadFileAndAddFeaData(l_FileInfo.cPath, l_FileInfo.nOffset, nAlienLen, pReaFeaBuf, nBufLen); m_ImgSvrInfo.m_iSvrInitAddDone = 2; } SetCurFeaFile(i, l_FileInfo); } } delete[]pReaFeaBuf; } 转为linux下可运行代码
以下是转换后的代码,具体改动如下:
1. 将 char 类型的字符串转为 std::string 类型;
2. 将 sprintf 函数替换为 snprintf 函数;
3. 将 GetPrivateProfileString 函数替换为读取文件内容并解析的方式;
4. 将 ReadFileAndAddFeaData 函数替换为读取文件内容并拷贝的方式;
5. 将 CSVNEW 和 delete[] 替换为 new 和 delete。
```
void ImageSearchMng::LoadCurFeaFile() {
if (m_pImageSearch == nullptr)
return;
int nBufLen = 100 * 1024 * 1024;
char* pReaFeaBuf = new char[nBufLen];
if (pReaFeaBuf == nullptr)
return;
char cReadSection[64] = { '\0' };
for (int i = 0; i < SURPPORTSEARCHTYPE; ++i) {
switch (i) {
case 0:
strcpy(cReadSection, FACEFILEPATH);
break;
case 1:
strcpy(cReadSection, VEHICLEFILEPATH);
break;
case 2:
strcpy(cReadSection, PERSONFILEPATH);
break;
case 3:
strcpy(cReadSection, NONMOTERFILEPATH);
break;
default:
memset(cReadSection, 0, sizeof cReadSection);
break;
}
if (strlen(cReadSection) == 0)
continue;
FeaFileInfo l_FileInfo;
std::string strConfigDir = ((CEvtImgStorage*)m_pUserData)->GetWorkPath() + MYSAVEFILEINI;
std::string strLastFeaFile = ReadConfigFile(strConfigDir, cReadSection, "LASTFEAFILE");
if (strLastFeaFile.length() > 0) {
strcpy(l_FileInfo.cPath, strLastFeaFile.c_str());
l_FileInfo.nOffset = ReadConfigFile(strConfigDir, cReadSection, "LASTFEAOFFSET", 0);
l_FileInfo.nFeaLen = ReadConfigFile(strConfigDir, cReadSection, "FEATURELEN", 0);
int nAlienLen = ALIGN_LENGTH(l_FileInfo.nFeaLen + 128, FILESETTIONLEN);
snprintf(l_FileInfo.cPath, MAX_PATH, "%s/Feature.idx", l_FileInfo.cPath);
if (m_ImgSvrInfo.m_iSvrInitAddDone == 3) {
if (l_FileInfo.nOffset > 0)
ReadFileContent(l_FileInfo.cPath, l_FileInfo.nOffset, nAlienLen, pReaFeaBuf, nBufLen);
m_ImgSvrInfo.m_iSvrInitAddDone = 2;
}
SetCurFeaFile(i, l_FileInfo);
}
}
delete[] pReaFeaBuf;
}
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)