Passing pointer crash my program

So I'm trying to make elements from an array know about their neighbors, so they can interfer with each other.
Here I initialize them:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
    for(int x = 0; x < SCX; x++)
        for(int y = 0; y < SCY; y++)
            for(int z = 0; z < SCZ; z++)
            {
                chunks[x][y][z] = new Chunk(texPath, x, y, z);
                //top
                if(CheckForBounds(x, y+1, z))
                {
                    chunks[x][y][z]->SetNeighbor(chunks[x][y][z]->SIDE_FRONT, chunks[x][y+1][z]);
                }
                //bottom
                if(CheckForBounds(x, y-1, z))
                {
                    chunks[x][y][z]->SetNeighbor(chunks[x][y][z]->SIDE_BOTTOM, chunks[x][y-1][z]);
                }
                //left
                if(CheckForBounds(x-1, y, z))
                {
                    chunks[x][y][z]->SetNeighbor(chunks[x][y][z]->SIDE_LEFT, chunks[x-1][y][z]);
                }
                //right
                if(CheckForBounds(x+1, y, z))
                {
                    chunks[x][y][z]->SetNeighbor(chunks[x][y][z]->SIDE_RIGHT, chunks[x+1][y][z]);
                }
                //front
                if(CheckForBounds(x, y, z+1))
                {
                    chunks[x][y][z]->SetNeighbor(chunks[x][y][z]->SIDE_FRONT, chunks[x][y][z+1]);
                }
                //back
                if(CheckForBounds(x, y, z-1))
                {
                    chunks[x][y][z]->SetNeighbor(chunks[x][y][z]->SIDE_BACK, chunks[x][y][z-1]);
                }
            }
    //chunks[0][0][0]->Test();
}


And the function is:
1
2
3
4
5
void Chunk::SetNeighbor(int index, Chunk* neighbor)
{
    Neighbors[index] = neighbor;
    changed = true;
}


the above code just crash my program.
the above code doesn't compile, you need to provide enough code to reproduce your issue.
they are delcared like this
1
2
3
4
5

//inside the megachunk class
Chunk *chunks[SCX][SCY][SCZ];
//inside the chunk class
Chunk *Neighbors[SIDE_COUNT];
void Chunk::SetNeighbor(int index, Chunk* neighbor)
{
Neighbors[index] = neighbor;
changed = true;
}

If this code crashes, it is most likely because the array Neighbours is of a size such that there is no element number index; you're trying to write beyond the array. What's the value of index, how big is Neighbors ?
enum
{
SIDE_TOP,
SIDE_BOTTOM,
SIDE_LEFT,
SIDE_RIGHT,
SIDE_FRONT,
SIDE_BACK,
SIDE_COUNT
};

in first code you see I'm calling

Chunk *Neighbors[SIDE_COUNT];

but it is strange that it crash even if I change the function in this:
1
2
3
4
5
void Chunk::SetNeighbor(int index, Chunk* neighbor)
{
//Neighbors[index] = neighbor;
changed = true;
}


I think the way it is passed crash the program??

Highly speculative, but your line 9 isn't consistent. Should SIDE_FRONT be SIDE_TOP? May be a possibility of writing outside array bounds if wrong.

Otherwise, it's difficult to tell from the amount of code posted. Will your debugger tell which line it crashed on?
Also, depending on what CheckForBounds does, you're almost certainly passing bad pointers to the function SetNeighbour.

The very first time, when the only valid pointer in your array is chunks[0][0][0], will SetNeighbour be called?
No it's code::blocks, I dont really enjoy VS

Buy it's not a problem from bounds, like I said, if I comment it, it crashes
Last edited on
I expect you've written over the stack. Trashed some memory.
And how I can solve this?
Well I found the mistake, CheckForBounds, was glitched out because of a mistaken paste...
Topic archived. No new replies allowed.