Passing pointer crash my program

Aug 20, 2017 at 7:53pm
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.
Aug 21, 2017 at 12:39am
the above code doesn't compile, you need to provide enough code to reproduce your issue.
Aug 21, 2017 at 4:01pm
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];
Aug 21, 2017 at 4:30pm
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 ?
Aug 21, 2017 at 5:17pm
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??

Aug 21, 2017 at 5:46pm
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?
Aug 21, 2017 at 6:36pm
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?
Aug 21, 2017 at 6:37pm
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 Aug 21, 2017 at 6:38pm
Aug 21, 2017 at 6:49pm
I expect you've written over the stack. Trashed some memory.
Aug 21, 2017 at 8:15pm
And how I can solve this?
Aug 21, 2017 at 8:30pm
Aug 21, 2017 at 8:37pm
Well I found the mistake, CheckForBounds, was glitched out because of a mistaken paste...
Topic archived. No new replies allowed.