Chris
Chris

Reputation: 29

Segmentation fault (core dumped) CS50 error

I have been writing a program for CS50 (caesar) that is supposed to use a key given by the user in the command line to encrypt a message. The program is supposed to test the key given for a number of conditions. I want my program to use return values to stop running when the command line argument given is invalid.

Code below: (I have not finished it, so far it only checks for the command line input)

#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

int char_repeat(string key);
int key_check(string key);

int main(int argc, string argv[])
{
    string key = argv[1];
    for (int i = 0; i < strlen(key); i++)
    {
        char c = toupper(key[i]);
        // conditions for key (string) written in argv[1]
        if (argc < 2 || strlen(key) != 26 || c < 65 || c > 90)
        {
            printf("Usage: ./substitution key\n");
            return 1;
        }
    }
    char_repeat(key);
    printf("this should not print if invalid...");
    return 0;
}



int char_repeat(string key)
{
    // check for repeated characters in key
    for (int i = 0; i < strlen(key); i++)
    {
        for (int j = i + 1; j < strlen(key); j++)
        {
            if (toupper(key[i]) == toupper(key[j]))
            {
                printf("Usage: ./substitution key\n");
                return 1;
            }
        }
    }
    printf("this should not print if invalid...");
    return 0;
}

When I give 'invalid' inputs such as

$ ./sub AB*DEFGHIJKLMNOPQRSTUVWXYZ
$ ./sub ABC
$ ./sub ABCABCABCABCABCABCABCABCAB 
$ ./sub ABCDEFGHIJKLMNOPQRSTUVWXYZ- 

The code works perfectly by printing "Usage: ./substitution key\n" skipping my test print message, "this should not print if invalid...".

YET, and this is the problem, if I run ./sub without giving it a second argument. That is,

$ ./sub 

I get an error message

segmentation fault (core dumped) 

In the code itself I use the condition (argc < 2) in one of the if statements so shouldn't it work as all the other conditions and print "Usage: ./substitution key\n" ? I know segmentation fault is an error with trying to access memory that shouldn't be accessed or some other type of memory corruption, I just don't see how this relates to my program. I would imagine it is a problem the way I am using return.

Thank you in advance!

Upvotes: 0

Views: 621

Answers (1)

Harry Ramsey
Harry Ramsey

Reputation: 149

A segmentation fault is an error caused by attempting to access memory that does not belong to you.

When you run your program without providing additional arguments by using the command ./sub the code in the program tries to access argument 2 which does not exist, so you do not have access to that memory.

string key = argv[1];

Instead, you should check before you try to access that memory that arguments have been passed in.

if(argc < 2)
{
    printf("Usage: ./substitution key\n");
    return 1;
}

string key = argv[1];

Upvotes: 1

Related Questions