2013-03-13 4 views
2

음, 메시지 암호화 및 암호 해독 프로그램을 만들려고했습니다. 그럼 왜 세분화 오류가 발생합니까? 누구든지 나를 도와 주시면 매우 감사하게 생각합니다! 암호화 기능 만 실행했습니다. 그것은 적절한 결과를 낳았습니다. 무슨 일이 있었는지 알아?문자열 입력을 할 때 분할 오류가 발생하는 이유는 무엇입니까?

gdb를 나에게이 메시지를 제공
 #include <iostream> 
     using namespace std; 
     #define max 1000 
     #include <string.h> 
     #include <cstdlib> 
     #include <stdlib.h> 
     #include <math.h> 
     #include <stdio.h> 


     char * encrypt(char *s) 
     { 
      int x = (rand()/((RAND_MAX+1u)/5)); 
      char *res; 
      int ascii; 
      switch(x) 
      { 
       case 0: 
       for(int i=0;i<strlen(s);i++) 
       { 
        ascii = (int)s[i]; 
        ascii = ascii-19; 
        res[i+2] = (char)ascii; 

       } 
       res[0]='a'; 
       res[1]='$'; 
       break; 
       case 1: 
       for(int i=0;i<strlen(s);i++) 
       { 
        ascii = (int)s[i]; 
        ascii = ascii+sqrt(strlen(s)); 
        res[i+2] = (char)ascii; 
       } 
       res[0]='x'; 
       res[1]='&'; 
       break; 
       case 2: 
       case 3: 
       for(int i=0;i<strlen(s);i++) 
       { 
        ascii = (int)s[i]; 
        ascii = ascii-sqrt(strlen(s)); 
        res[i+2] = (char)ascii; 
       } 
       res[0]='z'; 
       res[1]='^'; 
       break; 
       case 4: 
       for(int i=0;i<strlen(s);i++) 
       { 
        ascii = (int)s[i]; 
        ascii = ascii+13; 
        res[i+2] = (char)ascii; 
       } 
       res[0]='a'; 
       res[1]='j'; 
       break; 
      } 
      return res; 


     } 

     char * decrypt(char *s) 
     { 
      int ascii; 
      char *res; 
      if(s[0]=='a' &&s[1]=='$') 
      { 
       for(int i=0;i<strlen(s);i++) 
       { 
        ascii = (int)s[i+2]; 
        ascii += 19; 
        res[i] = ascii; 
       } 
      } 
      else if(s[0]=='b' &&s[1]=='&') 
      { 
       for(int i=0;i<strlen(s);i++) 
       { 
        ascii = (int)s[i+2]; 
        ascii -= (strlen(s)*strlen(s)); 
        res[i] = ascii; 
       } 
      } 
      else if(s[0]=='z' &&s[1]=='^') 
      { 
       for(int i=0;i<strlen(s);i++) 
       { 
        ascii = (int)s[i+2]; 
        ascii +=(strlen(s)*strlen(s)); 
        res[i] = ascii; 
       } 
      } 
      else if(s[0]=='a' &&s[1]=='j') 
      { 
       for(int i=0;i<strlen(s);i++) 
       { 
        ascii = (int)s[i+2]; 
        ascii -= 13; 
        res[i] = ascii; 
       } 
      } 

      return res; 

     } 


     int main() 
     { 
      int ch; 
      int i=0; 
      char *s; 
      char *res; 
      while(1) { 

      cout<<endl<<"1.Encrypt\n2.Decrypt"; 
      cout<<endl<<"Choice: "; 

      cin>>ch; 
      switch(ch) 
      { 
       case 1: 
       cout<<"\nEnter a message: "; 
       cin>>s; 

       res=encrypt(s); 
       cout<<"\nEncrypted message is: "<<res<<endl; 
       break; 

       case 2: 
       cout<<"\nEnter an Encrypted message: "; 
       cin>>s; 

       res=decrypt(s); 
       cout<<"\nDecrypted message is: "<<res<<endl; 
       break; 
       default: exit(0); 
      } 
     } 
      return 0; 
     } 

:

char *res; 

Starting program: /home/prasanna/encdec 

1.Encrypt 
2.Decrypt 
Choice: 1 

Enter a message: Test Line 

Program received signal SIGSEGV, Segmentation fault. 
0xb7f41aab in std::basic_istream<char, std::char_traits<char> >& std::operator>><char,  std::char_traits<char> >(std::basic_istream<char, std::char_traits<char> >&, char*)() 
    from /usr/lib/i386-linux-gnu/libstdc++.so.6 
+0

을 변경할 수 없기은'res'가 불확정 (즉,이 설정되지 않습니다되어 사용할 수 있습니다 유효한 메모리 위치). – WhozCraig

+0

예를 들어, 그것을'char res [256] = "";'로 변경했다면 잘 될 것입니다. – TheBuzzSaw

+0

@TheBuzzSaw이 경우에는 그렇지 않습니다. 'return res;'는 죽은 사람의 선물이다. – WhozCraig

답변

7

직접적인 이유는 char *s;은 당신이 뭔가 읽으려고하는 내로 초기화되지 않은 포인터 선언이다 - cin>>s;합니다.

진짜 이유는입니다. C 코드를 작성하고 C++이라고 부르는 이유입니다.

+0

그래, 나는 그것을 알고있다.하지만, 나는 원래 생각에서 벗어났다. 이것은 나의 알곡이 잘 작동 하는지를 확인하기위한 것이다 ... 그래서, 새로운 연산자가 내 해결책이다? –

+0

@PrasannaChoudhari no. 'std :: string'과'std :: cin'은입니다. –

+0

마지막 문장에 거의 동의하지는 않지만이 경우에는 할 것입니다 만, 의도 된 이유가 아닙니다.이 경우,''#include ' – WhozCraig

3

당신은 할당되지 않은 메모리를하지만 메모리에 당신은

res[i+2] = (char)ascii; 

쓰기. 이는 정의되지 않은 동작이며 가장 자주 충돌로 이어집니다. 당신은 mainchar *s;에 같은 일을하고

:

cin>>s; 
+0

감사합니다 !!! 알았다! 글쎄, 솔루션 바보 때 나는 그것을 싫어! 이전 질문에 대한 답변조차도 너무 바보 같았습니다! :-( –

0

메인에 char* s에 메모리를 할당하지 않았습니다. 받은 문자열을 입력으로 저장할 공간이 없습니다. 당신은 (아직 결정되지 않은) 일부 지역에 대한 포인터

사용 std::string

를 선언하거나 new

char *s = new char[15]; 
관련 문제