Skip to main content
gamedev.net
🔒 Locked

bad pointer

Started by McZ 5 replies 700+ views
Original Post
McZ
McZ
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;

        // 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.
grelle
grelle
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.
erissian
erissian
Most immediately I notice that your function returns CLight, when it should return CLight*.
We''re sorry, but you don''t have the clearance to read this post. Please exit your browser at this time. (Code 23)
Enigma
Enigma
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
erissian
erissian
Whoops.
We''re sorry, but you don''t have the clearance to read this post. Please exit your browser at this time. (Code 23)
McZ
McZ
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]
Puzzler183
Puzzler183
Just don't set your original alignment to 1?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.