Strange problem with pointers
-
I wrote something like this :
MD2Mesh::Frame* md2Frame = (MD2Mesh::Frame*)buffer; uint8* tmp = buffer + 40; MD2Mesh::Vertex* vtx = (MD2Mesh::Vertex*)tmp; md2Frame->vertices = vtx;
The value of "vtx" is changed after the line "mdFrame->vertices = vtx" U can look at the following debugging table:vtx
0x00434140 {v=0x00434140 "ÐBÄ" normIdx=0 }
origin::MD2Mesh::Vertex *
after line "mdFrame->vertices = vtx" it becomes like this :
vtx
0x00434140 {v=0x00434140 "@AC" normIdx=0 }
origin::MD2Mesh::Vertex *
md2Frame->vertices
0x00434140 {v=0x00434140 "@AC" normIdx=0 }
origin::MD2Mesh::Vertex *
Do u have any ideas ?
-
I wrote something like this :
MD2Mesh::Frame* md2Frame = (MD2Mesh::Frame*)buffer; uint8* tmp = buffer + 40; MD2Mesh::Vertex* vtx = (MD2Mesh::Vertex*)tmp; md2Frame->vertices = vtx;
The value of "vtx" is changed after the line "mdFrame->vertices = vtx" U can look at the following debugging table:vtx
0x00434140 {v=0x00434140 "ÐBÄ" normIdx=0 }
origin::MD2Mesh::Vertex *
after line "mdFrame->vertices = vtx" it becomes like this :
vtx
0x00434140 {v=0x00434140 "@AC" normIdx=0 }
origin::MD2Mesh::Vertex *
md2Frame->vertices
0x00434140 {v=0x00434140 "@AC" normIdx=0 }
origin::MD2Mesh::Vertex *
Do u have any ideas ?
It looks like you've got some cyclic thing going on. You're setting
vtx
to something insidemd2Frame
. Butvtx
is cast fromtmp
which is obtained by manipulatingbuffer
. Butbuffer
is cast tomd2Frame
, in which you are trying to set this value... I can't say for certain that's the problem, but wouldn't hurt to sort this out properly first.Almost, but not quite, entirely unlike... me...
-
I wrote something like this :
MD2Mesh::Frame* md2Frame = (MD2Mesh::Frame*)buffer; uint8* tmp = buffer + 40; MD2Mesh::Vertex* vtx = (MD2Mesh::Vertex*)tmp; md2Frame->vertices = vtx;
The value of "vtx" is changed after the line "mdFrame->vertices = vtx" U can look at the following debugging table:vtx
0x00434140 {v=0x00434140 "ÐBÄ" normIdx=0 }
origin::MD2Mesh::Vertex *
after line "mdFrame->vertices = vtx" it becomes like this :
vtx
0x00434140 {v=0x00434140 "@AC" normIdx=0 }
origin::MD2Mesh::Vertex *
md2Frame->vertices
0x00434140 {v=0x00434140 "@AC" normIdx=0 }
origin::MD2Mesh::Vertex *
Do u have any ideas ?
What is type of the buffer? Is it uint8*? What expected by buffer + 40? See, buffer + 1 would point to next unint8 position in the buffer. Is that what intended?
-
I wrote something like this :
MD2Mesh::Frame* md2Frame = (MD2Mesh::Frame*)buffer; uint8* tmp = buffer + 40; MD2Mesh::Vertex* vtx = (MD2Mesh::Vertex*)tmp; md2Frame->vertices = vtx;
The value of "vtx" is changed after the line "mdFrame->vertices = vtx" U can look at the following debugging table:vtx
0x00434140 {v=0x00434140 "ÐBÄ" normIdx=0 }
origin::MD2Mesh::Vertex *
after line "mdFrame->vertices = vtx" it becomes like this :
vtx
0x00434140 {v=0x00434140 "@AC" normIdx=0 }
origin::MD2Mesh::Vertex *
md2Frame->vertices
0x00434140 {v=0x00434140 "@AC" normIdx=0 }
origin::MD2Mesh::Vertex *
Do u have any ideas ?
You haven't given us all the types and class/structure definitions involved. You are also using a mess of casts and potential aliasing issues. You have to know what you are doing and be very careful to get this kind of stuff right.
anbluemoon wrote:
MD2Mesh::Frame* md2Frame = (MD2Mesh::Frame*)buffer;
Apparently, buffer is a
SomeType_T *buffer
set to the address of a block of allocated memory. What is SomeType_T? How big a block of memory? What is a MD2Mesh::Frame? How big is a MD2Mesh::Frame? Also if MD2Mesh::Frame is not a pod type, you are dead without properly constructing it via its constructor.anbluemoon wrote:
uint8* tmp = buffer + 40; MD2Mesh::Vertex* vtx = (MD2Mesh::Vertex*)tmp;
Your magic number could be subject to breakage due to changes. Additionally, it hides your intent. Also don't forget about alignment issues and padding that the compiler can insert in structs. md2Frame now points to the beginning of the buffer. vtx now points inside the buffer.
anbluemoon wrote:
md2Frame->vertices = vtx;
You have now changed part of the contents of the buffer. vtx has not changed at all. It is however a pointer, and what it points to in the buffer has changed because you wrote code to change it. Evidently, MD2Mesh::Frame.vertices is 40 * sizeof(SomeType_T) bytes from the beginning of the structure.
Please do not read this signature.
-
You haven't given us all the types and class/structure definitions involved. You are also using a mess of casts and potential aliasing issues. You have to know what you are doing and be very careful to get this kind of stuff right.
anbluemoon wrote:
MD2Mesh::Frame* md2Frame = (MD2Mesh::Frame*)buffer;
Apparently, buffer is a
SomeType_T *buffer
set to the address of a block of allocated memory. What is SomeType_T? How big a block of memory? What is a MD2Mesh::Frame? How big is a MD2Mesh::Frame? Also if MD2Mesh::Frame is not a pod type, you are dead without properly constructing it via its constructor.anbluemoon wrote:
uint8* tmp = buffer + 40; MD2Mesh::Vertex* vtx = (MD2Mesh::Vertex*)tmp;
Your magic number could be subject to breakage due to changes. Additionally, it hides your intent. Also don't forget about alignment issues and padding that the compiler can insert in structs. md2Frame now points to the beginning of the buffer. vtx now points inside the buffer.
anbluemoon wrote:
md2Frame->vertices = vtx;
You have now changed part of the contents of the buffer. vtx has not changed at all. It is however a pointer, and what it points to in the buffer has changed because you wrote code to change it. Evidently, MD2Mesh::Frame.vertices is 40 * sizeof(SomeType_T) bytes from the beginning of the structure.
Please do not read this signature.
Here is data structures:
typedef unsigned char uint8; class MD2Mesh { struct Vertex { uint8 coord[3], normalIdx; }; struct Frame { float translation[3], rotation[3]; char name[16]; Mesh::Vertex* vertices; }; };
At first I tried to do something like :MD2Mesh::Frame* md2Frame = (MD2Mesh::Frame*)buffer
to get the values from the buffer without initializing the pointer and I managed to get the values of translation, rotation and name but the vertices address didn't point to the "buffer + 40(size of translation + rotation + name)" so i just pass the the address of vtx which is buffer + 40 -
Here is data structures:
typedef unsigned char uint8; class MD2Mesh { struct Vertex { uint8 coord[3], normalIdx; }; struct Frame { float translation[3], rotation[3]; char name[16]; Mesh::Vertex* vertices; }; };
At first I tried to do something like :MD2Mesh::Frame* md2Frame = (MD2Mesh::Frame*)buffer
to get the values from the buffer without initializing the pointer and I managed to get the values of translation, rotation and name but the vertices address didn't point to the "buffer + 40(size of translation + rotation + name)" so i just pass the the address of vtx which is buffer + 40Again you forgot to mention the type of buffer?