从C 中的功能返回时分割故障
Segmentation fault while returning from function in c++
i使用stringstream用于解析字符串,但是从函数退出时,它出乎意料地给出了分割错误。
bool check_if_condition(int a)
{
string polygonString1="19.922379 51.666267 19.922381 51.665595 19.921547 51.665705 19.921218 51.665753 19.920787 51.665815 19.919753 51.665960 19.919952 51.666897 19.920395 51.666826 19.920532 51.667150 19.920830 51.667748 19.920989 51.667905 19.921690 51.667906 19.922141 51.662866 19.922855 51.668696 19.922664 51.668237 19.922610 51.668025 19.922464 51.667451 19.922355 51.666732 19.922379 51.666267";
double buf1; // Have a buffer string
stringstream ssPolygonString1(polygonString1); // Insert the string into a stream
double polygon1[2]; // Create vector to hold our words
int iterPoly1=0;
while (ssPolygonString1 >> buf1)
{
polygon1[iterPoly1]=(buf1);
cout<<"buf1="<<buf1<<"n";
iterPoly1++;
}
ssPolygonString1.str("");
cout<<"Return true";
return true;
}
main()
{
check_if_condition(1);
}
有人可以帮助我了解函数电话怎么了?
我正在使用C 11
我在https://www.tutorialspoint.com/compile_cpp11_online.php上运行它
我假设当您说double polygon1[2]
时,您想通过评论创建一个向量。
如果您使用矢量,您的代码将起作用。
当您使用向量时,请确保使用vector.push_back();
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
using namespace std;
bool check_if_condition(int a)
{
string polygonString1 = "19.922379 51.666267 19.922381 51.665595 19.921547 51.665705 19.921218 51.665753 19.920787 51.665815 19.919753 51.665960 19.919952 51.666897 19.920395 51.666826 19.920532 51.667150 19.920830 51.667748 19.920989 51.667905 19.921690 51.667906 19.922141 51.662866 19.922855 51.668696 19.922664 51.668237 19.922610 51.668025 19.922464 51.667451 19.922355 51.666732 19.922379 51.666267";
double buf1; // Have a buffer string
stringstream ssPolygonString1(polygonString1); // Insert the string into a stream
vector<double> polygon1; // Create vector to hold our words
int iterPoly1 = 0;
while (ssPolygonString1 >> buf1)
{
//polygon1[iterPoly1] = (buf1);
polygon1.push_back(buf1); // Add buf1 to vector.
cout << "buf1=" << buf1 << "n";
iterPoly1++;
}
ssPolygonString1.str("");
//Print vector to show it works!
for (int i = 0; i < polygon1.size(); i++)
{
cout << polygon1.at(i) << endl;
}
cout << "Return true";
return true;
}
int main()
{
check_if_condition(1);
system("pause");
return 0;
}
错误在行中:polygon1 [iterpoly1] =(buf1);
因为向量的长度为2,并且迭代率高于2。因此行为是出乎意料的。
相关文章:
- 分割故障C++矩阵
- C ,二进制树的高度,而不是检查我的子树是否为空,而是在检查我的子树是否是叶子节点.抛出分割故障
- 我正在为此代码分割故障
- 在执行strcpy时获取分割故障错误
- 空隙指针分割故障
- Xcode会产生分割故障,而不是例外
- OPENCV C 中的分割故障误差
- 分割故障:核心倾倒C 矢量对字符串:
- C 2D数组导致分割故障(核心倾倒)
- 分割故障/错误读数变量-C 列表
- DLSYM导致分割故障
- 使用C 中的操作员删除的分割故障
- 使用Linux X64下的Libmozjs-52(SpidermonKey)进行分割故障
- 使用长变量时分割故障(11)错误
- C - 返回字符串时的分割故障
- 创建VertexArray时的分割故障
- 向量分割故障的向量
- 从std :: fileSystem ::路径对象的类中退出时的分割故障
- C 矢量分割故障
- 基于向量的数据格式的分割故障误差