我正在使用简单的应用程序,它在偶数位置显示字符,但是当我输入输入时抛出运行时错误“应用程序收到SIGSEGV信号。分段失败”。我不知道哪里出错了。我多次分析我的代码,但我找不到任何有意义的解决方案。
我的代码:
#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s;
scanf("%s",&s);
int length = s.length();
for(int i = 0; i < length; i++)
{
if(i%2==0)printf("%s",s[i]);
}
return 0;
}
GDB调试器信息:
**
->->pre-prompt
(gdb)
->->prompt
->->post-prompt
Reading symbols from C:\Users\user\Desktop\par.exe...done.
->->breakpoints-invalid
->->breakpoints-invalid
->->pre-prompt
(gdb)
->->prompt
->->post-prompt
Source directories searched: C:/Program Files/Dev-Cpp/MinGW32/lib;$cdir;$cwd
->->pre-prompt
(gdb)
->->prompt
->->post-prompt
Source directories searched: C:/Program Files/Dev-Cpp/MinGW32/include;C:/Program Files/Dev-Cpp/MinGW32/lib;$cdir;$cwd
->->pre-prompt
(gdb)
->->prompt
->->post-prompt
Source directories searched: C:/Program Files/Dev-Cpp/MinGW32/include;C:/Program Files/Dev-Cpp/MinGW32/include;C:/Program Files/Dev-Cpp/MinGW32/lib;$cdir;$cwd
->->pre-prompt
(gdb)
->->prompt
->->post-prompt
->->pre-prompt
(gdb)
->->prompt
->->post-prompt
->->pre-prompt
(gdb)
->->prompt
->->post-prompt
->->pre-prompt
(gdb)
->->prompt
->->post-prompt
Working directory C:\Users\user\Desktop.
->->pre-prompt
(gdb)
->->prompt
->->post-prompt
Starting program: C:\Users\user\Desktop\par.exe
[New Thread 5328.0x13c0]
->->new-thread
->->breakpoints-invalid
->->breakpoints-invalid
->->breakpoints-invalid
->->breakpoints-invalid
->->breakpoints-invalid
->->frames-invalid
->->breakpoints-invalid
->->breakpoints-invalid
->->breakpoints-invalid
->->breakpoints-invalid
->->breakpoints-invalid
->->breakpoints-invalid
->->starting
->->signal
Program received signal
->->signal-name
SIGSEGV
->->signal-name-end
,
->->signal-string
Segmentation fault
->->signal-string-end
.
->->frame-begin 0 0x41f036
->->frame-address
0x0041f036
->->frame-address-end
in
->->frame-function-name
std::string::length() const
->->frame-args
()
->->frame-end
->->stopped
->->pre-prompt
(gdb)
->->prompt
->->post-prompt
->->error
->->pre-prompt
(gdb)
->->prompt
->->error-begin
No symbol "include" in current context.
->->post-prompt
eax 0x64616441 1684104257
ecx 0x22ff04 2293508
edx 0x77a60958 2007370072
ebx 0x7ffdf000 2147348480
esp 0x22feec 0x22feec
ebp 0x22ff18 0x22ff18
esi 0x0 0
edi 0x0 0
eip 0x41f036 0x41f036 <std::string::length() const+2>
eflags 0x10206 [ PF IF RF ]
cs 0x1b 27
ss 0x23 35
ds 0x23 35
es 0x23 35
fs 0x3b 59
gs 0x0 0
->->pre-prompt
(gdb)
->->prompt
->->post-prompt
->->pre-prompt
(gdb)
->->prompt
->->post-prompt
Dump of assembler code for function _ZNKSs6lengthEv:
0x0041f034 <+0>: mov (%ecx),%eax
=> 0x0041f036 <+2>: mov -0xc(%eax),%eax
0x0041f039 <+5>: ret
0x0041f03a <+6>: nop
0x0041f03b <+7>: nop
End of assembler dump.
->->pre-prompt
(gdb)
->->prompt
->->post-prompt
->->frame-begin 0 0x41f036
#0
->->frame-address
0x0041f036
->->frame-address-end
in
->->frame-function-name
std::string::length() const
->->frame-args
()
->->frame-end
->->frame-begin 1 0x4013ee
#1
->->frame-address
0x004013ee
->->frame-address-end
in
->->frame-function-name
main
->->frame-args
()
->->frame-source-begin
at
->->frame-source-file
C:\Users\user\Desktop\par.cpp
->->frame-source-file-end
:
->->frame-source-line
11
->->frame-source-end
->->frame-end
->->pre-prompt
(gdb)
->->prompt
->->post-prompt
->->error
->->pre-prompt
(gdb)
->->prompt
->->error-begin
No symbol "length" in current context.
**
CPU信息:
0x0041f034 <+0>: mov (%ecx),%eax
=> 0x0041f036 <+2>: mov -0xc(%eax),%eax
0x0041f039 <+5>: ret
0x0041f03a <+6>: nop
0x0041f03b <+7>: nop
答案 0 :(得分:5)
scanf
旨在使用char
数组而不是std::string
,并且要打印字符,您必须使用%c
代替%s
:
string s;
scanf("%s", &s);
^^^^
//Undefined behavior invokes!
...
printf("%s",s[i]);
^^^^
//Undefined behavior again!
如果您真的想使用scanf
,则应使用中间char
数组:
char str[BIG_ENOUGHT];
scanf("%s", &str);
std::string s(str);
最好使用cin
和cout
代替:
cin >> s;
...
cout << s[i];