2013-09-29 1 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 : 그것은 cin 대신 cout를 사용하는 것이 좋습니다

char str[BIG_ENOUGHT]; scanf("%s", &str); std::string s(str); 

을 문자열과 함께 작동하지 않습니다. – billz

답변

5

scanfchar 배열하지 std::string 작동하도록하고, 문자를 인쇄 할 수을 사용해야합니다.대신 %s :

string s; 
scanf("%s", &s); 
      ^^^^ 
     //Undefined behavior invokes! 

... 

printf("%s",s[i]); 
     ^^^^ 
     //Undefined behavior again! 

당신이 정말로 당신이 중간 char 배열을 사용해야합니다 scanf을 사용하려면 :

cin >> s; 

... 

cout << s[i]; 
관련 문제