2013-09-29 71 views
1

我正在簡單的應用程序,它顯示在甚至位置的字符,但是當我在給輸入是拋出運行時錯誤「應用程序收到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

scanf函數不適用於字符串 – billz

回答

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); 

這是更好地使用cincout代替:

cin >> s; 

... 

cout << s[i]; 
相關問題