Sign in to follow this  

bad pointer

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

My pointer gets changed some strange way, but I don't know why.

// --- Code from my 3DS loader in static lib
CLight *CFile::getLight(int i)
{
	if( i >= (int)m_LightList.size() )
		return NULL;

	CLight *pLight = m_LightList[i];

        // Print out men address here
	cout << "npos: " << &pLight->Name << endl;
	cout << "lpos: " << &pLight->m_Position << endl;

        // return pointer to light
	return pLight;
}

// --- code from my .Exe file that use the static lib.
CLight *pLight = pFile->getLight(0);
cout << "name pos: " << &pLight->Name << endl;
cout << "light pos: " << &pLight->m_Position << endl;

// --- Light class 
class CLight {
public:
 char Name[21];

 CVertex3f m_Position;

 // Function to read data from 3DS file.
 void readData(...);
};

So my problem is that I don't get the correct position on my light, when calling the pFile->getLight(0); the function prints the memory adresses for both members of the light (name and position) and so it does just after retriving the light. giving bad result, the pLight->Name has the same memory address in both cases BUT not the m_Position so WHY? there is NOTHING that change the lights position.

Share this post


Link to post
Share on other sites
Your memory is simply overwritten by some nasty code. =)

The easiest way to solve it is to use the debugger!
Try setting a breakpoint on the memory address that change value. Or step through the code and see what happens on every line.

Share this post


Link to post
Share on other sites
Once more (with feeling?), what does your debugger tell you?

Quote:
Original post by erissian
Most immediately I notice that your function returns CLight, when it should return CLight*.

No, he's correctly returning a CLight *.

Enigma

Share this post


Link to post
Share on other sites
The debugger gave me the same result as my added cout-lines, altough I solved the problem late last night. My static Lib3DS.lib file was compiled using the struct member alignment set to one byte and the .exe file was compiled with the struct member alignment set to default (Using VC++ 2005 Express).

So now my question is, Can I some way change the alignment of the structs that need this in my code? this way the user of the 3DS library doesn't need to compile his/hers code using 1 byte alignment of structures.

Edit: I found the '#pragma pack' command to change the alignment of a struct like this:

#pragma pack(push, 1)
// Some code with alignment 1

#pragma pack(pop)
// continue with code at default alignment

[Edited by - McZ on December 18, 2005 2:23:26 PM]

Share this post


Link to post
Share on other sites

This topic is 4376 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this