Jump to content
  • Advertisement

Archived

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

cryo75

Pointer problem??

This topic is 5387 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

Hi all, I have the following structure:
struct ELEMENT {
	LPDIRECT3DTEXTURE9 mptex;
	int				   x, y;
	float			   xSize, ySize;

};
ELEMENT* pElements;

miMaxTextures = 32;
miTextures = 0;

ZeroMemory(&pElements, sizeof(pElements) * miMaxTextures);
Then I want to load textures into the structure using the following function:
HRESULT CPane::LoadTexture(int x, int y, float xs, float ys, LPCSTR szFileName)
{
	LPDIRECT3DTEXTURE9 mptextmp;

	//Do we have a device?

	if (!mpdev)
		return E_FAIL;

	//Do we have empty textures?

	if (miTextures == miMaxTextures)
		return E_FAIL;

	//Load the texture from file

	if (FAILED(D3DXCreateTextureFromFile(mpdev, szFileName, &mptextmp))) {
		miTextures--;
		return E_FAIL;
	}

	//Set the appropriate values in the structure

	pElements[miTextures].mptex = mptextmp;
	pElements[miTextures].x		= x;
	pElements[miTextures].y		= y;
	pElements[miTextures].xSize = xs;
	pElements[miTextures].ySize = ys;

	//Increment the number of textures we''re using

	miTextures++;

	return D3D_OK;
}
But I get an access violation on the line: pElements[miTextures].mptex = mptextmp; Ummm... anyone can see the problem?? Thanks, Ivan

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
perhaps because your simple declaring a pointer and not an array..?

Share this post


Link to post
Share on other sites
cryo75,

The line:

ELEMENT* pElements;

Merely creates a pointer to an element, but nowhere do you either point pElements at an ELEMENT or array of elements, or do you allocate memory for it.

Also, ZeroMemory(&pElements, sizeof(pElements) * miMaxTextures); should give you an error for the same reason, but I guess the compiler is being nice.

What you need to do is call the following

pElements = new ELEMENT[miMaxTextures];

This will allocate an array of miMaxTextures number of ELEMENT''s....which you can then index into with miTextures.

One thing to note, nowhere in your code are you checking if miTextures is less than miMaxTextures...I''m affraid you may have more pointer problems down the road without taking steps now to ensure pointer integrity.

Best Regards,

Jeromy Walsh
Programmer
Liquid Entertainment



Share this post


Link to post
Share on other sites
Thanks for the quick reply.

At the beginning of the function I''m doing the following check:

if (miTextures == miMaxTextures)
return E_FAIL;

What do you mean that I have to check that miTextures is less than miMaxTextures??

Ivan

Share this post


Link to post
Share on other sites
cryo75,

I missed the line you referred to. That line returns an error if the two are equal to each other...but what happens if miTextures is ever set to be GREATER than miMaxTextures in another function, etc... then you''ll be calling

pElements[miTextures]

Where miTextures is an index larger than the size of your array. If you know you''re not modifying either of the values anywhere except in this function, you should be ok, but its best to test for as many cases as possible.

A better test is:

if( miTextures >= miMaxTextures ) return E_FAIL;

Lemme know how the rest of the information I provided you manages.

Best Regards,

Jeromy Walsh
Programmer
Liquid Entertainment
------------------------
"The question isn''t how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" -Boondock Saints

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!