Jump to content

  • Log In with Google      Sign In   
  • Create Account

Heightmap Generator problem. (C++)


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 mindspin311   Members   -  Reputation: 122

Like
0Likes
Like

Posted 10 January 2008 - 03:00 AM

Here's what I get: First-chance exception at 0x00401435 in Terrain.exe: 0xC0000005: Access violation writing location 0x003650f8. Unhandled exception at 0x00401435 in Terrain.exe: 0xC0000005: Access violation writing location 0x003650f8. It happens when I get the values z=16, i=1, x=0. I have no idea why this is happening. The heightmap I'm generating is 256*256, and it's an array of size 256*256, so no idea why at 256*17 in the array i get this error. Here's the code:
<code>
unsigned char* generateHeightmap(int size)
{
	unsigned char oldV;
	unsigned char* hm = new unsigned char(size*size);
	for (int z = 0; z < (size - 7); z+=8) //Loop through each strip of 8.
	{
		if (z==16) {
			oldV = oldV; //Error checking before problem... Does nothing
		}
		for (int x = 0; x < size; ++x)
		{
			if (z==0) //Set initial value
			{
				oldV = myRand(30,225);
				for (int i = 0; i < 8; ++i) //Make slight different values for rest of section
				{
					if (myBoolRand()) //Choose to increase/decrease values
						hm[(z+i)*size + x] = oldV + myRand(0,INC);
					else
						hm[(z+i)*size + x] = oldV - myRand(0,DEC);
				}
			}
			else
			{ //Check and make sure values don't go out of range (0-255)
				if (oldV < DEC)
				{
					oldV = oldV + myRand(0,INC);
				}
				else if (oldV > (255-INC))
				{
					oldV = oldV - myRand(0,DEC);
				}
				else
				{
					if (myBoolRand())
						oldV = oldV + myRand(0,INC);
					else
						oldV = oldV + myRand(0,DEC);
				}
				for (int i = 0; i < 8; ++i) 
				{
					if (myBoolRand())
						hm[(z+i)*size + x] = oldV + myRand(0,INC);
					else
						hm[(z+i)*size + x] = oldV - myRand(0,DEC);
				}
			}
		}
	}
	return hm;
}
</code>


Sponsor:

#2 hovermonkey   Members   -  Reputation: 129

Like
0Likes
Like

Posted 10 January 2008 - 03:14 AM

that should be

new unsigned char[size*size]

not

new unsigned char(size*size)


I'm not sure but I think the second one will allocate one char and sets its value to size*size, while the first one allocates an array of chars.
Also please put code, /code tags (in square brackets) around your code to make it more readable in future.
Also, use a debugger! It's a lot easier to find where your code is failing by single-stepping through it than trying to figure out which line of your code compiles to address 0x00401435.


#3 Antheus   Members   -  Reputation: 2397

Like
0Likes
Like

Posted 10 January 2008 - 03:24 AM

Pointer arrays truly are evil...

Here's a possible solution that doesn't involve any overhead.


template < unsigned int size >
struct HeightMap {

HeightMap() {
generateHeightMap();
}

unsigned int get_size() const { return size; }

unsigned char &at( unsigned int x, unsigned int y ) { return hm[y*size + x]; }
private:
void generateHeightmap()
{
unsigned char oldV;
for (int z = 0; z < (size - 7); z+=8) //Loop through each strip of 8.
{
...
}
// doesn't return anything
}

char hm[size*size];
};

...

HeightMap<256> hm;
// or
typedef HeightMap<512> HeightMap512;

HeightMap512 *hm = new HeightMap512();
...
delete hm;



#4 mindspin311   Members   -  Reputation: 122

Like
0Likes
Like

Posted 10 January 2008 - 03:33 AM

Thanks, it worked.

And btw, I did use a debugger. That's why I said the values of x, i, and z to show when the write error happened.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS