Prasanna Choudhari
Prasanna Choudhari

Reputation: 189

Why Am I Getting Segmentation Fault When I Am Taking String Input?

Well, i was trying to make a message encryption and decryption program. So, why am i getting segmentation fault? If anyone could please help me i will be very very grateful! I had run only the encryption function. It produced proper results. Any clue what happened?

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

The gdb gives me this message:

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

Upvotes: 2

Views: 1314

Answers (3)

Suvarna Pattayil
Suvarna Pattayil

Reputation: 5239

You have not allocated any memory to char* s in main. There is no space decided to store the string you received as input. You have only declared a pointer to some area (which is not yet decided)

Use std::string

or you can use new

char *s = new char[15];

Upvotes: 0

Luchian Grigore
Luchian Grigore

Reputation: 258698

The immediate reason is that char *s; declares an uninitialized pointer into which you attempt to read something - cin>>s;.

The real reason is that you're writing C code and calling it C++.

Upvotes: 7

Daniel Fischer
Daniel Fischer

Reputation: 183988

You have not allocated memory to

char *res;

but you

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

write to that memory. That is undefined behaviour, and most often leads to a crash.

You're doing the same in main to char *s;:

cin>>s;

Upvotes: 3

Related Questions