Jump to content
  • Advertisement


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


Dynamic 2-dimensional arrays

This topic is 5423 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
This is just a guess, but could it be an operator precedence issue?
What happens if you try:
lodData->rg_iHeightArray[0][0] = 3;

Remember: Silly is a state of Mind, Stupid is a way of Life.
-- Dave Butler

Share this post

Link to post
Share on other sites
Same problem. A weird thing is that when I initialise the array, instead of having a value of some huge negative number, it has a value of -12851.

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

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!