我正在尝试创建一个向量输入strings (white space included)
的向量,直到用户输入'!'
。
以下是我的代码:
#include <iostream>
#include <vector>
#include <string>
#include <iterator>
using namespace std;
int main()
{
char buffer[100];
string temp;
vector <string> vec;
while(1)//Shows Segmentation fault error while running,
//compiles succesfully
{
cout << "Enter String : ";
cin.get(buffer,100,'\n');//To ensure that whitespaces are also input
cout << "@@@@ " << buffer << endl ;//Shows correct input
cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
//Clearing cin stream for any residual characters
temp.assign(buffer);//Assigning string the input "phrase"
cout << "###" << temp << endl;//Shows correct output
if (temp == "!") //if input is '!' character do not push on the vector a
//come out of the loop
break;
vec.push_back(temp);//push on the vector
temp.assign(NULL); //flush temp string
}
vector <string>::iterator p = vec.begin();
//display thre vector
while( p != vec.end())
{
cout << *p << endl;
p++;
}
return 0;
}
它成功编译但在运行时抛出Segmentation fault
错误。
无法弄清楚原因?任何人都可以指出吗?
同样更智能的解决方案,同时指出我的代码有什么问题。
由于
答案 0 :(得分:3)
这将是原因:
temp.assign(NULL);
因为std::string::assign()
将尝试读取,直到找到空终止符并且取消引用NULL
指针是未定义的行为:在这种情况下是分段错误。使用temp.clear()
或只在每次迭代时创建一个新对象。
使用std::getline()
读取包含空格的行,避免必须对固定大小的数组进行硬编码(即buffer[100]
):
std::string line;
while (std::getline(std::cin, line) && line != "!")
{
vec.push_back(line);
}
答案 1 :(得分:3)
尽管存在其他问题,但在gdb
(或可能是任何其他调试器)中运行程序会显示原因:
tikal@seven ~$ g++ -o temp temp.cpp
tikal@seven ~$ gdb temp
GNU gdb 6.3.50-20050815 (Apple version gdb-1822) (Sun Aug 5 03:00:42 UTC 2012)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin"...Reading symbols for shared libraries ... done
(gdb) run
Starting program: /Users/tikal/temp
Reading symbols for shared libraries ++............................. done
Enter String : asd
@@@@ asd
###asd
Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0x0000000000000000
0x00007fff9081e6b0 in strlen ()
(gdb) backtrace
#0 0x00007fff9081e6b0 in strlen ()
#1 0x00007fff9857ab95 in std::string::assign ()
#2 0x0000000100001642 in main ()
(gdb)
基本上,temp.assign( NULL )
是一个坏主意。您可以使用temp.clear()
代替,或者不要打扰它(稍后您只需重新分配)。
答案 2 :(得分:1)
while (getline(cin, temp) && temp != "!")
{
vec.push_back(temp);
}
使用像这样的缓冲区比C ++更容易做C事。在C ++中,通常可以使用类来避免这种显式内存管理 - 这是一个很好的例子。