This topic is now archived and is closed to further replies.

Dynamic 2-dimensional arrays

This topic is 5112 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Here''s how I set up my array:
class lodData{
short rg_iHeightArray**;

lodData[0].rg_iHeightArray = new short* [128];
		int qw;
		for(qw=0; qw<128; qw++)
			lodData[0].rg_iHeightArray[qw] = new short[128];
But when I try to do this: lodData[0].rg_iHeightArray[0][0] = 3; I get an access violation.

Share this post

Link to post
Share on other sites
i just made a templete class for this

#ifndef ARRAY2D_H
#define ARRAY2D_H

template <class item>
class TArray2D
// Constructors

TArray2D(void): mLoaded(false), mWidth(0), mHeight(0), mItems(NULL){;}
TArray2D(unsigned int Width, unsigned int Height){Create(Width, Height);}
// Copy Constructor

TArray2D(const item& FromCopy);
// Destructor


// Create and Destroy our array

bool Create(unsigned int Width, unsigned int Height);
void Destroy(void);

// Accessor functions

inline bool IsLoaded(void){return mLoaded;}
inline unsigned int GetHeight(void){return mHeight;}
inline unsigned int GetWidth(void){return mWidth;}
inline unsigned int GetSize(void){return mWidth * mHeight;}
inline item& GetCell(unsigned int X, unsigned int Y){return mItems[Y * mWidth + X];}
inline item& GetCell(unsigned int Index){return mItems[Index];}

// Helper functions

void Fill(const item& FillItem);

item* mItems;
unsigned int mWidth;
unsigned int mHeight;
bool mLoaded;

// Implamentation -----------------------------------------------

template <class item>
TArray2D<item>::TArray2D(const item& FromCopy)
int size = sizeof(FromCopy);
memcpy(this, FromCopy, size);

template <class item>
bool TArray2D<item>::Create(unsigned int Width, unsigned int Height)
mWidth = Width;
mHeight = Height;
mItems = new item[mWidth * mHeight];
mLoaded = true;
return false;

template <class item>
void TArray2D<item>::Destroy(void)
if (mItems != NULL)
delete[] mItems;

mItems = NULL;
mWidth = mHeight = 0;
mLoaded = false;

template <class item>
void TArray2D<item>::Fill(const item& FillItem)
for (unsigned int i = 0; i < mWidth * mHeight; ++i)
mItems[i] = FillItem;


Share this post

Link to post
Share on other sites
Ah, I''ve seen that value many, many times. VC++ initializes every byte of data-page and heap memory to 0xCD. -12851 is two''s-complement of 0xCDCD. Why were you expecting only gigantic negative numbers from what is normally near-random values?

Share this post

Link to post
Share on other sites