C++字符串::查找崩溃应用程序
C++ string::find crashes application
所以我想检查我的const std::string &foo = "hello world";
是否包含另一个字符串const std::string &bar = "world";
.
所以我尝试使用这样的 if 语句:if (foo.find(bar) != std::string::npos)
.没有错误,但是当我启动应用程序时,它会崩溃。当我删除该代码应用程序时,该应用程序再次正常工作。
有人可以告诉我如何使用此功能吗?
我试图找到原因,另一个教程,但我的代码似乎很好。是的,我确定我在某处犯了一个错误。
编辑:
法典:
#include "modelLoader.h"
#include <iostream>
#include <fstream>
#include <algorithm>
#include <strstream>
#include <string>
ModelAsset *ModelLoader::loadOBJModel(FilePath *objFile)
{
ModelAsset *model = new ModelAsset();
std::ifstream file(objFile->getPath());
std::vector<std::string*> lines;
std::string tempLine;
while(std::getline(file, tempLine))
{
lines.push_back(&tempLine);
}
for(U32 i = 0; i < lines.size(); i++)
{
if((*lines[i])[0] == '#') continue;
else if((*lines[i])[0] == 'v' && (*lines[i])[1] == ' ')
{
F32 tempX, tempY, tempZ;
sscanf(lines[i]->c_str(), "v %f %f %f %f", tempX, tempY, tempZ);
model->verticles.push_back(new Vector3(tempX, tempY, tempZ));
}
else if((*lines[i])[0] == 'v' && (*lines[i])[1] == 'n')
{
F32 tempX, tempY, tempZ;
sscanf(lines[i]->c_str(), "vn %f %f %f %f", tempX, tempY, tempZ);
model->normalVectors.push_back(new Vector3(tempX, tempY, tempZ));
}
else if((*lines[i])[0] == 'v' && (*lines[i])[1] == 't')
{
F32 tempX, tempY, tempZ;
sscanf(lines[i]->c_str(), "vt %f %f %f %f", tempX, tempY, tempZ);
model->textureVectors.push_back(new Vector3(tempX, tempY, tempZ));
}
else if((*lines[i])[0] == 'f')
{
U16 counter = std::count(lines[i]->begin(), lines[i]->end(), '/');
if(counter == 0)
{
S32 v1, v2, v3;
sscanf(lines[i]->c_str(), "f %d %d %d", v1, v2, v3);
model->faces.push_back(new Face(v1, v2, v3));
}
else if(counter == 3)
{
S32 v1, v2, v3;
S32 vt1, vt2, vt3;
sscanf(lines[i]->c_str(), "f %d/%d %d/%d %d/%d", v1, vt1, v2, vt2, v3, vt3);
model->faces.push_back(new Face(v1, v2, v3, vt1, vt2, vt3));
}
else if(counter == 6)
{
/* Just testing if find works fine */
const std::string &main = "hello world";
const std::string &part = "world";
if(main.find(part) != std::string::npos)
{
S32 v1, v2, v3;
S32 vn;
sscanf(lines[i]->c_str(), "f %d//%d %d//%d %d//%d", v1, vn, v2, vn, v3, vn);
model->faces.push_back(new Face(v1, v2, v3, vn));
}
else
{
S32 v1, v2, v3;
S32 vn;
S32 vt1, vt2, vt3;
sscanf(lines[i]->c_str(), "f %d/%d/%d %d/%d/%d %d/%d/%d", v1, vt1, vn, v2, vt2, vn, v3, vt3, vn);
model->faces.push_back(new Face(v1, v2, v3, vt1, vt2, vt3, vn));
}
}
}
}
return model;
}
当我使用 if(main.find...) 删除所有内容时,我的程序可以工作。
问题出在 sscanf 行上:
S32 v1, v2, v3;
S32 vn;
sscanf(lines[i]->c_str(), "f %d//%d %d//%d %d//%d", v1, vn, v2, vn, v3, vn);
sscanf 希望获得地址来存储它从字符串中解析的值。 这里的这些调用将 v1、vn 等未初始化的值作为地址传递,导致 sscanf 将结果写入内存中的随机位置。
这应该会让你更接近你的目标:
sscanf(lines[i]->c_str(), "f %d//%d %d//%d %d//%d", &v1, &vn, &v2, &vn, &v3, &vn);
(我注意到"&vn"在这里被多次传递,这可能不是想要的)
相关文章:
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- 如何找出应用程序崩溃的原因 - Win 10 LTSB
- 操纵安卓相机的深度图导致应用程序崩溃
- 调用 java 的回调() 时应用程序崩溃.由于 detatchThread 而获得运行时错误
- QCompleter set模型使应用程序崩溃
- Qt 5 应用程序崩溃并出现"qLineEdit::setText"
- 切换NvAPI_Stereo_Deactivate/NvAPI_Stereo_activate会使unity应用程序崩溃
- eglSwapBuffers上的应用程序崩溃
- VLD 使应用程序崩溃
- 由于调用 std::condition_variable 后参数无效而导致应用程序崩溃
- 执行递归函数时 C++ 应用程序崩溃
- 在 C++/CLI/C# 项目中启用"Native Code Debugging"导致应用程序崩溃
- LOCAL_SHARED_LIBRARIES使我的应用程序崩溃
- 为什么使用 nullPtr 调用函数不会使我的应用程序崩溃
- 当删除上下文属性中的QLIST对象时,QT QML应用程序崩溃
- React 本机 0.59 应用程序崩溃并出现 libc 错误
- 当使用“制造”编译的程序中使用库时,应用程序崩溃
- 如何在导致应用程序崩溃的代码中修复指令
- C++多线程应用程序崩溃
- 当使用 /ENTRY:main 和 /MT 运行时库编译为 /SUBSYSTEM:WINDOWS 时,应用程序崩溃