EXC BAD ACCESS error on Deck Class

Good Evening,

I am having a problem with a section of code giving me the above error. I created a destructor, which I didn't have before, and still the problem persists. Here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
   Deck::Deck (size_t numDecks)
    {   int l = 1;
        discardPile = new Card[numDecks * 52];
        mainDeck = new Card[numDecks * 52];
        
        for (int i = 0; i < numDecks ; i++)
             {
                 for( int j = 0; j < 4; j++) // loop for each suit
                 {
                     for ( int k = 0; k< 13 ; j++)
                     {   
                         mainDeck[l] = Card(k, j, true, 0);
                         l++;
                     }
                 }
             }
    }


the line setting the maindeck[l] equal to a new card is where the error shows up in my debugging. This seems to be the only issue with the rest of my code. I'm relatively new to C++ and programming in general so I am completely stumped on how to fix this problem, and have been scouring my textbook and the internet to no avail. Any help is appreciated. Thank you.
assuming 'j' is the suit, you're giving bad suits to the Card() call:

1
2
3
4
5
                     for ( int k = 0; k< 13 ; j++)  // <- why are you incrementing j here?
                     {   
                         mainDeck[l] = Card(k, j, true, 0);
                         l++;
                     }


Because you're incrementing j in that loop, it will get higher than 3, which is likely a problem when you pass it to Card().
Thanks! That fixed the problem
I'm getting the same error on a different segment of code now, and this one is completely baffling as well.
Here is the code snippet:
1
2
3
4
	size_t Card::get_rank() const
	{
		return my_rank;
	}


the error is on the return my_rank line, where my_rank is the cards value 1-13. Does anyone know why this is so? I can always provide more code. Thank you.
The problem is not with that line of code.

What's happening is you're calling get_rank on a non-existant card. You're probably stepping out of bounds somewhere else.

Is this in VS? when it breaks and takes you to that line, go in the debugger and look for a drop box at the top labeled "Stack Frame". Click on that and select the previous function call (should be the 2nd option from the top). That'll show you where you're stepping out of bounds.
Thanks again, the problem was in another .cpp file where the card class was used. Working on debugging that issue now.
Topic archived. No new replies allowed.