microsoft / vscode

Visual Studio Code
https://code.visualstudio.com
MIT License
163.75k stars 29.1k forks source link

segmentation fault (core dumped) #169796

Closed leoLuisMayaute closed 1 year ago

leoLuisMayaute commented 1 year ago

ADD ISSUE DESCRIPTION HERE when i compile the code that take command prompt argument (argc, argv) it is configurated to show an error message but instead it desplays a segmentation fault (core dumped) error and i do not know how to fix it.

Version: 1.74.2 Commit: e8a3071ea4344d9d48ef8a4df2c097372b0c5161 User Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36 Embedder: codespaces

lszomoru commented 1 year ago

@leoLuisMayaute, this seems like a compile issue? What language are you using, and what compiler?

leoLuisMayaute commented 1 year ago

@lszomoru No, it compiles normaly but when is executed, into an if statement thats supposed to return an error message that i wrote in the code instead it shows the error 'segmentation fault (core dumped)'. The code is in C and im using the command 'make' to compile it.

Here I left the code:

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

int main(int argc, string argv[])
{
    int size = strlen(argv[1]);
    string key = argv[1];

    string plaintext = get_string("plaintext: ");

    int textlength = strlen(plaintext);
    char ciphertext[textlength];

    // Validate key
    if(argc != 2)
    {
        printf("Usage: ./substitution KEY \n");  //Here is where the error
        return 1;
    }

    if(size != 26)
    {
        printf("Key must contain 26 chatacters\n");
        return 1;
    }

    for(int i = 0; i < size; i++)
    {
        if(isalpha(key[i]) == 0)
        {
            printf("All characters in the KEY must be letters\n");
            return 1;
        }

        for(int j = i + 1; j <= size; j++)
        {
            if(key[i] == key[j])
            {
                printf("Key must not contain repeated characters\n");
                return 1;
            }
        }
    }

    // Cipher the plaintext

    for(int i = 0; i <= textlength; i++)
    {
        if(isalpha(plaintext[i]) != 0)
        {
            if(islower(plaintext[i]) != 0)
            {
                int tocipherlower = plaintext[i] - 97;
                ciphertext[i] = tolower(key[tocipherlower]);
            }
            if(isupper(plaintext[i]) != 0)
            {
                int tocipherupper = plaintext[i] - 65;
                ciphertext[i] = toupper(key[tocipherupper]);
            }
        }
        else if(isspace(plaintext[i]) != 0)
        {
            ciphertext[i] = ' ';
        }
        else
        {
            ciphertext[i] = plaintext[i];
        }
    }
    printf("ciphertext: %s\n", ciphertext);
}
lszomoru commented 1 year ago

@leoLuisMayaute, thank you very much for the additional information. Given the fact that the segmentation fault occurs when the application is running, I do not believe that this is an issue with VS Code but with the application itself. I think that it would be better if you would post this question on https://stackoverflow.com as I am sure that there someone would be able to point you in the right direction.