# Array problems

This topic is 4943 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am using an array of pointers to sprites like this: Sprite* _pBrickSprites[_iNumBrickRows][_iNumBrickCols]; And then I go through and initialize each like this:
    for(int i=1; i<=_iNumBrickRows; i++)
{
for(int j=1; j<=_iNumBrickCols; j++)
{
_pBrickSprites[j] = new Sprite(_pBrickBitmap);
_pBrickSprites[j]->SetPosition(_pGame->GetWidth()*j/(_iNumBrickCols+1) - _pBrickBitmap->GetWidth()/2,
_iVerticalBrickSpacing*i + _pBrickBitmap->GetHeight()*(i-1));
}
}


In detecting collisions I use this code:
    // Loop through and check if the ball hit any of the bricks
RECT brickRect;
for(int i=0; i<_iNumBrickRows; i++)
{
for(int j=0; j<_iNumBrickCols; j++)
{
brickRect = _pBrickSprites[j]->GetPosition();

}
}


Everything compiles fine, but the line where I assign brickRect the position causes the game to crash. I have no clue why. This is the declaration of GetPosition() and the rect in the sprite class: RECT m_rcPosition; RECT& GetPosition() { return m_rcPosition; }; I have a feeling it has something to do with the reference being returned, but I can't figure it out. Thanks for any help.

##### Share on other sites
you never assign a sprite to the 0 row or column entries.

Chees
Chris

##### Share on other sites
This isn't a problem with your array, it's a problem with RECTs. IIRC, Windows RECTs don't like being copied/assigned. It would work if you did this instead:

RECT& brickRect = _pBrickSprites[j]->GetPosition();

Because then you're not copying the RECT itself, just the reference.

But, aside from being bad OOP practice, I'd just avoid using RECT entirely and use your own structure. I try to avoid Windows structures in general, though...

##### Share on other sites
Quote:
 Original post by MorboThis isn't a problem with your array, it's a problem with RECTs. IIRC, Windows RECTs don't like being copied/assigned. It would work if you did this instead:*** Source Snippet Removed ***Because then you're not copying the RECT itself, just the reference.But, aside from being bad OOP practice, I'd just avoid using RECT entirely and use your own structure. I try to avoid Windows structures in general, though...

I've never heard of this, RECT is just a struct with 4 longs, the default shallow assingment operator should be able to handle this. I think its more likely what I mentioned earlier that the 0 row and column aren't being initialized.

Cheers
Chris

##### Share on other sites
I agree, there is nothing wrong with RECT.

##### Share on other sites
Quote:
Original post by chollida1
Quote:
 Original post by MorboThis isn't a problem with your array, it's a problem with RECTs. IIRC, Windows RECTs don't like being copied/assigned. It would work if you did this instead:*** Source Snippet Removed ***Because then you're not copying the RECT itself, just the reference.But, aside from being bad OOP practice, I'd just avoid using RECT entirely and use your own structure. I try to avoid Windows structures in general, though...

I've never heard of this, RECT is just a struct with 4 longs, the default shallow assingment operator should be able to handle this. I think its more likely what I mentioned earlier that the 0 row and column aren't being initialized.

Cheers
Chris
Exactly! chollida1 is correct.

Sorry Morbo, but what you said is merely superstition/heresay or something. Copying RECTs works absolutely fine. His code wouldn't crash like that anyway.

The problem is that he initialises his array using for loops like this:
    for(int i=1; i<=_iNumBrickRows; i++)
but accesses them with for loops like this:
    for(int i=0; i<_iNumBrickRows; i++)
(The second one is correct) It goes from 0 to _iNumBrickRows-1. The first loop has to be the same.
The first bit of code will need to change slightly to accomodate the shift in starting values...
    for(int i=0; i<_iNumBrickRows; i++)    {        for(int j=0; j<_iNumBrickCols; j++)        {            _pBrickSprites[j] = new Sprite(_pBrickBitmap);            _pBrickSprites[j]->SetPosition(_pGame->GetWidth()*(j+1)/(_iNumBrickCols+1) - _pBrickBitmap->GetWidth()/2,                _iVerticalBrickSpacing*(i+1) + _pBrickBitmap->GetHeight()*i);            _pGame->AddSprite(_pBrickSprites[j]);        }    }

1. 1
Rutin
26
2. 2
JoeJ
20
3. 3
4. 4
5. 5

• 10
• 10
• 9
• 9
• 10
• ### Forum Statistics

• Total Topics
631751
• Total Posts
3002086
×