3D Array Template Class

Started by
7 comments, last by deks 18 years, 2 months ago
I have this array class listed below. I also have a 2d version. The 2d version works well, but when I use the 3d version I get exceptions being thrown. The exception is thrown on a cout call and not anything to do with my 3d array! Could someone help me figure this out. 3D Array code:

#ifndef ARRAYS_H
#define ARRAYS_H
#ifdef WIN32
#pragma once
#endif

template < class T > class Array3d
{
private:
	T *m_pArray;
	int m_Width;
	int m_Height;
	int m_Depth;

public:
	Array3d(int Width, int Height, int Depth)
	{
		this->m_pArray = new T[Width * Height * Depth];
		if (this->m_pArray == NULL)
		{
			this->m_Width = 0;
			this->m_Height = 0;
			this->m_Depth = 0;
		}
		else
		{
			this->m_Width = Width;
			this->m_Height = Height;
			this->m_Depth = Depth;
		}
	}

	~Array3d()
	{
		if (this->m_pArray != NULL)
		{
			delete [] this->m_pArray;
			this->m_Width = 0;
			this->m_Height = 0;
			this->m_Depth = 0;
			this->m_pArray = NULL;
		}
	}

	bool Resize(int Width, int Height, int Depth)
	{
		T *newArray = new T[Width * Height * Depth];
		if (newArray == NULL)
			return false;

		int minWidth = (Width < this->m_Width) ? Width : this->m_Width;
		int minHeight = (Height < this->m_Height) ? Height : this->m_Height;
		int minDepth = (Depth < this->m_Depth) ? Depth : this->m_Depth;
		this->m_Width = Width;
		this->m_Height = Height;
		this->m_Depth = Depth;

		int offset1 = 0;
		int offset2 = 0;
		int offset3 = 0;
		int offset4 = 0;

		for (int z = 0; z < minDepth; z++)
		{
			offset1 = z * minWidth * minHeight;
			offset3 = z * this->m_Width * this->m_Height;
			for (int y = 0; y < minHeight; y++)
			{
				offset2 = y * minWidth;
				offset4 = y * this->m_Width;
				for (int x = 0; x < minWidth; x++)
					newArray[offset1 + offset2 + x] = this->m_pArray[offset3 + offset4 + x];
			}
		}

		if (this->m_pArray != NULL)
			delete [] this->m_pArray;

		this->m_pArray = newArray;
		newArray = NULL;

		return true;
	}

	T& Index(int x, int y, int z)
	{
		return this->m_pArray[(z * this->m_Width * this->m_Height) + (y * this->m_Width) + x];
	}

	int Width()
	{
		return this->m_Width;
	}

	int Height()
	{
		return this->m_Height;
	}

	int Depth()
	{
		return this->m_Height;
	}

	int Size()
	{
		return this->m_Width * this->m_Height * this->m_Depth;
	}
};

#endif

Test application:

#include "stdafx.h"
#include <Arrays.h>
#include <iostream>

using namespace std;

void Array3dTest();
void Array2dTest();
void ArrayTest();

int _tmain(int argc, _TCHAR* argv[])
{
	Array3dTest();

	return 0;
}

void Array3dTest()
{
	Array3d<int> test(5, 5, 2);

	for (int z = 0; z < test.Depth(); z++)
	{
		for (int y = 0; y < test.Height(); y++)
		{
			for (int x = 0; x < test.Width(); x++)
				test.Index(x, y, z) = x;
		}
	}

	cout << "Values:" << endl;
	for (int z = 0; z < test.Depth(); z++)
	{
>>ERROR HERE    cout << z << ": " << endl;
		for (int y = 0; y < test.Height(); y++)
		{
			cout << y << ": ";
			for (int x = 0; x < test.Width(); x++)
				cout << test.Index(x, y, z) << " ";
			cout << endl;
		}
	}

	cout << endl << endl;

	test.Resize(10, 10, 2);

	for (int z = 0; z < test.Depth(); z++)
	{
		for (int y = 0; y < test.Height(); y++)
		{
			for (int x = 0; x < test.Width(); x++)
				test.Index(x, y, z) = x;
		}
	}

	cout << "Values:" << endl;
	for (int z = 0; z < test.Depth(); z++)
	{
		cout << z << ": " << endl;
		for (int y = 0; y < test.Height(); y++)
		{
			cout << y << ": ";
			for (int x = 0; x < test.Width(); x++)
				cout << test.Index(x, y, z) << " ";
			cout << endl;
		}
	}
}

The error happens on the line indicated above. Any clues? [Edited by - Akusei on January 26, 2006 8:20:07 PM]
-akusei
Advertisement
I'm not a c++ pro so probably can't help much, but is there any more info you could provide? For example, does the program output anything before the exception is thrown, and if so, what? Also, do you know what kind of exception it is?
It outputs "Values:" then throws an exception.

The app says this in release mode

The instruction at "0x7x910de3" referenced memory at "0xfffffffc". The memory could not be "read".

and this in debug

Unhandled exception at 0x7c81eb33 in TestArray.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x0012fbd0..

and breaks in file tidtable.c on line 112
    if (pfnEncodePointer != NULL)    {>> HERE        ptr = (*pfnEncodePointer)(ptr);    }    return ptr;
-akusei
I ran your code, and it looks like you have a buffer overrun. I have to leave right now, but make sure you're never accessing outside m_pArray in the Index() function. Commenting the first loop that assign the values and it doesn't crash anymore.
Just use the stl vector template
Programming since 1995.
Or
Boost Multi-array
Using a 3rd party library or class is fine, but the whole point of creating my own 3d array class is for experience and knowledge... If I can create one on my own, I can understand one and therefore further my knowledge of the language and certain techniques. If I simply read about it and use a 3rd party class, then I'm not really hands on learning.
-akusei
I think I found it! I tested the change and it worked....

Faulty code
int Depth(){   return this->m_Height //HEIGHT NOT DEPTH!!!}


Corrected code
int Depth(){   return this->m_Depth;}


Man do I feel stupid! Thanks for the push in the right direction deks.
-akusei
Quote:Original post by Akusei
Man do I feel stupid! Thanks for the push in the right direction deks.


Hehe no problem.

This topic is closed to new replies.

Advertisement