Array SEGFAULT problems.

Hello everyone, I am programming a 3d terrain generator, and I use an array to store block information, and store the groups of 64x64x64 chunks to ease up on the processor demand. (Rendering 26 meshes of 64x64x64 pregenerated meshes is a lot easier than rendering 1 mesh of 4607442944 cubes)
So the problem is, when I start my engine up, and the chunk manager lays out 2x2x2 chunks, and there is a giant fissure between each chunk. When I say fissure, it means a complete absence of blocks on the outside of each chunk. This makes me think that something is wrong with the way my arrays are handled, however, when I try to use the arrays as proper C++ dictates, I get a SEGFAULT crash, which makes no sense. So the fix that leaves fissures is really ghetto and not proper.
Here's the code:

The chunk class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#define CHUNK_SIZE 64
class Chunk{

public:
    Chunk(int x,int y,int z,int id);
    ~Chunk();
void Update(float delta);
void Render(Ogre::SceneManager* sceneManager);

void loadTerrain(terrainGenerator* terraGen);

private:
Block *chunkBlocks[CHUNK_SIZE][CHUNK_SIZE][CHUNK_SIZE];
int chunkPos[3];
int m_ChunkID;
};


And heres some of the implementation that causes problems:
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
39
40
41

void Chunk::Render(Ogre::SceneManager* sceneManager)
{
	Ogre::ManualObject* MeshChunk = new Ogre::ManualObject("MeshManChunk" + Ogre::StringConverter::toString(m_ChunkID));
	MeshChunk->begin("BoxColor");

	int iVertex = 0;
	bool Block;
	bool Block1;

	for (int z = 0; z < CHUNK_SIZE-1; z++)// + chunkPos[3]
	{
		for (int y = 0; y < CHUNK_SIZE-1; y++)// + chunkPos[2]
		{
			for (int x = 0; x < CHUNK_SIZE-1; x++)// + chunkPos[1]
			{
                int px = x+chunkPos[1];//World Position
                int py = y+chunkPos[2];
                int pz = z+chunkPos[3];

				Block = chunkBlocks[x][y][z]->isActive();//This crashes
					

				Block1 = 0;
				if (x-1 >= 0) Block1 = chunkBlocks[x-1][y][z]->isActive();//So does this
//Note the check to make sure that x-1 would not go out of bounds..

                if (Block1 == 0)
				{

				//arbitrary render code that makes no use of the array
}
//The above block check repeats for each face..
			}
		}
	}

	MeshChunk->end();
sceneManager->getRootSceneNode()->createChildSceneNode()->attachObject(MeshChunk);

}

I don't think I need to explain the complete implementation to solve the problem, unless someone wants me to in which case I will..but the problem is the for loop. The one above is the one that works, but shouldn't it be:
1
2
3
4
5
6
	for (int z = 0; z <= CHUNK_SIZE; z++)
	{
		for (int y = 0; y <= CHUNK_SIZE; y++)
		{
			for (int x = 0; x <= CHUNK_SIZE; x++)
			{

To loop through all the elements in the array? Cause this crashes.
Thanks
Last edited on
Valid array indices are 0 to size-1.

So, for your arrays it is 0 to 63.

If you loop on z <= 64 then you would be going out of the bounds of the arrays.

If you loop on z < 63, which is what you currently have, you miss the last element in the array.

What you want is z < 64 or z <= 63.

so:

1
2
for ( int z = 0 ; z < CHUNK_SIZE; ++z )
// ... 
Wow. Why I didn't try that is beyond me. Been a long day.
It worked!
Thanks so much!
Topic archived. No new replies allowed.