Click here to Skip to main content
15,888,610 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: problem in C++ programming Pin
N a v a n e e t h3-Apr-09 4:38
N a v a n e e t h3-Apr-09 4:38 
GeneralRe: problem in C++ programming Pin
myprojectme3-Apr-09 4:50
myprojectme3-Apr-09 4:50 
AnswerRe: problem in C++ programming Pin
CPallini3-Apr-09 4:52
mveCPallini3-Apr-09 4:52 
QuestionRe: problem in C++ programming Pin
David Crow3-Apr-09 6:35
David Crow3-Apr-09 6:35 
AnswerRe: problem in C++ programming Pin
Thomas DeSalvo3-Apr-09 14:15
Thomas DeSalvo3-Apr-09 14:15 
GeneralRe: problem in C++ programming Pin
myprojectme3-Apr-09 20:59
myprojectme3-Apr-09 20:59 
QuestionMSADO15.tlh file not available in ..c:\documents and settings\user\local settings\temp\msado15.tlh Pin
pandit843-Apr-09 4:05
pandit843-Apr-09 4:05 
Questiondynamic 2D array template for non-built-in types. [modified] Pin
Sauce!3-Apr-09 2:36
Sauce!3-Apr-09 2:36 
I'm building an Array2D template class to hold a custom class (I am testing with int to keep things simple)

Unfortunately I keep running into the same brick wall. I'll show some code first and then outline my problem.

Array2D.h
#pragma once
#ifndef INC_ARRAY_2D
#define INC_ARRAY_2D

typedef unsigned int UINT;

template <class T>
class Array2D
{
public:
	Array2D(UINT TheWidth, UINT TheHeight)
{
	Width = TheWidth;
	Height = TheHeight;

	ppArrayColumns = new T*[Width];
	pArray = new T[Width*Height];
	
	for(UINT i = 0; i < Width; i++)
	{
		ppArrayColumns[i] = &pArray[i*Height];
	}
}

	Array2D(UINT Size);

	~Array2D();

	T* operator[] (UINT Index)
	{
		return ppArrayColumns[Index];
	}

private:
	T **ppArrayColumns;
	T *pArray;

	UINT Width;
	UINT Height;
};

#endif


Array2D.cpp
#include "Array2D.h"

template <class T>
Array2D<T>::Array2D(UINT Size)
{
	Array2D(Size, Size);
}

template <class T>
Array2D<T>::~Array2D()
{
	delete[] *ppArrayColumns;
	*ppArrayColumns = 0;
	delete[] ppArrayColumns;
	ppArrayColumns = 0;
	pArray = 0; //No need to delete pArray, ppArrayColumns takes care of that for us.
}


ppArrayColumns is an array of pointers to type T, of size Width. pArray is an array of type T. The constructor for Array2D sets each element of ppArrayColumns to the address of the appropriate element of pArray. The end result should allow us to access pArray like a regular 2D array, but it doesn't!

Each element of ppArrayColumns only points to one element of pArray. As such, when we use the [] operator, the whole thing falls apart because we've managed to walk into funny memory.

Is there any way I can make ppArrayColumns point to one element of pArray, yet still be able to access the next n elements (where n = Height)

Here is a diagram I have made for reference.[^]

modified on Friday, April 3, 2009 8:46 AM

AnswerRe: dynamic 2D array template for non-built-in types. Pin
Stuart Dootson3-Apr-09 2:58
professionalStuart Dootson3-Apr-09 2:58 
GeneralRe: dynamic 2D array template for non-built-in types. [modified] Pin
Sauce!3-Apr-09 4:13
Sauce!3-Apr-09 4:13 
GeneralRe: dynamic 2D array template for non-built-in types. Pin
Stuart Dootson3-Apr-09 6:48
professionalStuart Dootson3-Apr-09 6:48 
GeneralRe: dynamic 2D array template for non-built-in types. [modified] Pin
Sauce!3-Apr-09 20:19
Sauce!3-Apr-09 20:19 
GeneralRe: dynamic 2D array template for non-built-in types. Pin
Stuart Dootson4-Apr-09 2:43
professionalStuart Dootson4-Apr-09 2:43 
GeneralRe: dynamic 2D array template for non-built-in types. Pin
Sauce!4-Apr-09 5:32
Sauce!4-Apr-09 5:32 
AnswerRe: dynamic 2D array template for non-built-in types. Pin
Maximilien3-Apr-09 3:00
Maximilien3-Apr-09 3:00 
AnswerRe: dynamic 2D array template for non-built-in types. Pin
Iain Clarke, Warrior Programmer4-Apr-09 0:06
Iain Clarke, Warrior Programmer4-Apr-09 0:06 
GeneralRe: dynamic 2D array template for non-built-in types. Pin
Sauce!4-Apr-09 0:19
Sauce!4-Apr-09 0:19 
QuestionHow do i create an Executable (.exe) to debug my project? Pin
blokkies3-Apr-09 2:13
blokkies3-Apr-09 2:13 
AnswerRe: How do i create an Executable (.exe) to debug my project? Pin
Rajesh R Subramanian3-Apr-09 2:29
professionalRajesh R Subramanian3-Apr-09 2:29 
GeneralRe: How do i create an Executable (.exe) to debug my project? Pin
blokkies3-Apr-09 2:43
blokkies3-Apr-09 2:43 
GeneralRe: How do i create an Executable (.exe) to debug my project? Pin
Rajesh R Subramanian3-Apr-09 3:20
professionalRajesh R Subramanian3-Apr-09 3:20 
GeneralRe: How do i create an Executable (.exe) to debug my project? Pin
blokkies3-Apr-09 3:40
blokkies3-Apr-09 3:40 
GeneralRe: How do i create an Executable (.exe) to debug my project? Pin
Cedric Moonen3-Apr-09 3:52
Cedric Moonen3-Apr-09 3:52 
GeneralRe: How do i create an Executable (.exe) to debug my project? Pin
blokkies3-Apr-09 4:03
blokkies3-Apr-09 4:03 
QuestionRe: How do i create an Executable (.exe) to debug my project? Pin
David Crow3-Apr-09 4:13
David Crow3-Apr-09 4:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.