Segmentation Fault error in c for some unknown reason

ghz 8months ago ⋅ 72 views

Wondering why I get this error Segmentation Fault (Core dumped) when running my code. I am trying to code blackjack and my code is probably very inefficient lol. But if anyone can help, thanks in advance!

I pinpointed the area where it happens and it happens where my "ERROR HAPPENS HERE" comment is. If you have any questions about the rest of the code, ask below.

Edit: Here is my Code in text:

char conv(int num)
{
    // Converts numbers into respective deck values
    char c = 'h';
    if (num == 1)
    {
        c = 'A';
    }
    else if (num == 11)
    {
        c = 'J';
    }
    else if (num == 12)
    {
        c = 'Q';
    }
    else if (num == 13)
    {
        c = 'K';
    }
    else if (num == 10)
    {
        c = 't';
    }
    else
    {
        c = num + '0';
    }
    return c;
}

int deal(void)
{
    // Deals Card between 13 and 1
    int card = (rand() % 13) + 1;
    return card;
}

int hit(void)
{
    int card = deal();
    string c = "he";
    // Converts cards into printable format
    if (card != 10)
    {
        c[0] = conv(card);
        c[1] = ' ';
    }
    else
    {
        c[0] = conv(card);
    }
    // Prints card
    printf("┌─────────┐\n");
    printf("│%s       │\n", c);
    printf("│         │\n");
    printf("│         │\n");
    printf("│         │\n");
    printf("│         │\n");
    printf("│         │\n");
    printf("│       %s│\n", c);
    printf("└─────────┘\n");

    // Returns card number dealt
    return card;
}

int main(int argc, string argv[])
{
    bool play = true;
    if (argc < 2)
    {
        printf("Usage: casino [money]");
        return 1;
    }
    bool round = true;
    while (round)
    {
        // Draws cards
        srand(time(NULL));
        int pcard1 = deal();
        int pcard2 = deal();
        int pscore = pcard1 + pcard2;
        int dcard1 = deal();
        int dcard2 = deal();
        int dscore = dcard1 + dcard2;

        // Converts card numbers into a printable format
        string p1 = "hello";
        if (pcard1 == 10)
        {
            p1 = "10";
        }
        else
        {
            p1[0] = conv(pcard1);
            // ERROR HAPPENS RIGHT HERE
            p1[1] = ' ';
        }

        string p2 = "hello";
        if (pcard2 == 10)
        {
            p2 = "10";
        }
        else
        {
            p2[0] = conv(pcard2);
            p2[1] = ' ';
        }

        string d1 = "NULL";
        if (dcard1 == 10)
        {
            d1 = "10";
        }
        else
        {
            d1[0] = conv(dcard1);
            d1[1] = ' ';
        }
        string d2 = "NULL";
        if (dcard2 == 10)
        {
            d2 = "10";
        }
        else
        {
            d2[0] = conv(dcard1);
            d2[1] = ' ';
        }

Answers

The issue in your code likely arises from accessing memory that is not allocated properly when you're trying to modify the characters in your strings p1, p2, d1, and d2. Specifically, the error occurs when you try to modify the second character of the strings p1, p2, and d1.

Here's the problematic part of your code:

string p1 = "hello";
if (pcard1 == 10)
{
    p1 = "10";
}
else
{
    p1[0] = conv(pcard1);
    p1[1] = ' ';  // ERROR HAPPENS RIGHT HERE
}

When you assign a string literal like "hello" to p1, it allocates memory for the string and sets p1 to point to that memory. However, modifying the characters of p1 after it has been assigned to a string literal can lead to undefined behavior because string literals are typically stored in read-only memory.

To fix this issue, you should use std::string and its replace function to modify the characters of the string. Here's how you can rewrite the problematic part of your code:

#include <string>

std::string p1 = "hello";
if (pcard1 == 10)
{
    p1 = "10";
}
else
{
    p1.replace(0, 1, 1, conv(pcard1));  // Replace the first character
    p1.replace(1, 1, 1, ' ');            // Replace the second character with a space
}

Similarly, you should make the same modifications for p2, d1, and d2. This should resolve the segmentation fault issue you're encountering.