我正在使用在偶数位置显示字符的简单应用程序,但是当我输入时抛出运行时错误“应用程序收到 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.
**
中央处理器信息:
0x0041f034 <+0>: mov (%ecx),%eax
=> 0x0041f036 <+2>: mov -0xc(%eax),%eax
0x0041f039 <+5>: ret
0x0041f03a <+6>: nop
0x0041f03b <+7>: nop