C++ string::find 使应用程序崩溃
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' 在这里被多次传递,这可能不是预期的)
所以我想检查我的 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' 在这里被多次传递,这可能不是预期的)