【问题标题】:Why Am I Getting Segmentation Fault When I Am Taking String Input?为什么我在输入字符串时会出现分段错误?
【发布时间】:2013-03-13 14:55:16
【问题描述】:

好吧,我正在尝试制作一个消息加密和解密程序。 那么,为什么我会出现分段错误? 如果有人可以请帮助我,我将非常感激! 我只运行了加密功能。它产生了适当的结果。有什么线索吗?

        #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;
        }

gdb 给了我这个信息:

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

【问题讨论】:

  • 因为res 是不确定的(即它永远不会设置为有效的内存位置)。
  • 例如,如果您将其更改为char res[256] = "";,它会正常工作。
  • @TheBuzzSaw 在这种情况下,不会。 return res; 是一个死人的赠品。
  • @WhozCraig 啊,你是对的。我没有看到正在发生的事情的全部范围。是的,它必须是一个堆分配(或者最好是std::string)。

标签: c++ g++ segmentation-fault stdin


【解决方案1】:

直接原因是char *s; 声明了一个未初始化的指针,您尝试在其中读取某些内容 - cin&gt;&gt;s;

真正的原因是您正在编写 C 代码并将其称为 C++。

【讨论】:

  • 是的,我知道!它的 c,但是,我偏离了我最初的想法。这只是为了确保我的算法运行良好......所以,新运营商是我的解决方案?
  • @PrasannaChoudhari 没有。 std::stringstd::cin 是。
  • 虽然我很少同意最后一个陈述,但在这种情况下我同意,但不是出于预期的原因。在这种情况下,#include 列表让我毛骨悚然。
  • 好吧,我在任何地方都做了 char *res = new char [max],也为 *s 做了。工作得很好......加密是完美的。解密需要调整..
  • @PrasannaChoudhari:我确信它运行良好,除了可能的内存泄漏和可能的缓冲区溢出。
【解决方案2】:

你还没有分配内存

char *res;

但是你

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

写入该内存。这是未定义的行为,通常会导致崩溃。

你在 mainchar *s; 做同样的事情:

cin>>s;

【讨论】:

  • 谢谢!!!知道了!好吧,当解决方案很愚蠢时,我讨厌它!甚至我上一个问题的答案也太愚蠢了! :-(
【解决方案3】:

您还没有在 main 中为 char* s 分配任何内存。没有空间决定存储您作为输入接收到的字符串。您只声明了指向某个区域的指针(尚未确定)

使用std::string

或者你可以使用new

char *s = new char[15];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-07
    • 2021-09-08
    • 1970-01-01
    • 1970-01-01
    • 2021-11-21
    • 2019-10-04
    • 2011-12-23
    • 1970-01-01
    相关资源
    最近更新 更多