Jump to content
  • Advertisement

Archived

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

MindWipe

Pointer Hell!

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

Ok... playing around with networking and got a little problem. I get a packet that looks like this: char | char | ....(dynamic mem) the first two describes the dynamic mem (not important so I won''t explain). Anyway, what I want to do is to cast the dymamic mem into a struct. so I''ve got: PacketPointer (dunno what kinda of type it is, and doesn''t matter) and I''ve got my struct: MYSTRUCT HereItIs so this is how I figured of doing it: HereItIs = (MYSTRUCT*)&(((unsigned char*)PacketPointer)[2]); I should work, but it really doesn''t give me a nice result, so maybe it doesn''t. But how would you do it? Do it in a few lines? What would be the smartest way to do it? /MindWipe

Share this post


Link to post
Share on other sites
Advertisement
Assuming you mean HereItIs is a MYSTRUCT* (pointer):
HereItIs = (MYSTRUCT*)(PacketPointer+2);
And make PacketPointer a BYTE (unsigned char) pointer.

Edit: What would be better, is if you amke you MYSTRUCT type have the first two bytes as part of its structure (if they describe the memory, don't you need them as part of the structure anyway?), then you can just do a straight HereItIs = (MYSTRUCT*)PacketPointer



[edited by - Evil Bill on December 1, 2002 5:33:49 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Evil Bill
Assuming you mean HereItIs is a MYSTRUCT* (pointer):
HereItIs = (MYSTRUCT*)(PacketPointer+2);
And make PacketPointer a BYTE (unsigned char) pointer.

Edit: What would be better, is if you amke you MYSTRUCT type have the first two bytes as part of its structure (if they describe the memory, don't you need them as part of the structure anyway?), then you can just do a straight HereItIs = (MYSTRUCT*)PacketPointer




Thx for the reply, first, yeah I meant MYSTRUCT *HereItIs.
Well, could I do (MYSTRUCT*)PacketPointer? because the struct includes a pointer.

If I had:
{
char blah;
char blah2;
unsigned char *pointer;
}

will it really set the pointer if I cast it? Or just change it to the value at that memory address!?

/MindWipe

[edit] forgot the * again [/edit]

[edited by - MindWipe on December 1, 2002 5:43:31 PM]

Share this post


Link to post
Share on other sites
Ahh... i see, sorry i thought your PacketPointer was more or less the data you''ve recieved. In this case, i''d make a member function in PacketPointer, GetData(), which would return a MYSTRUCT* from its pointer.

Share this post


Link to post
Share on other sites
I''m not sure I understand what you''re doing, but if you have initialized that pointer on one computer, sends the struct to the other, that pointer will be garbage when it arrives. If you also want to send the content of the string, you should send its length and content after the struct, copy it into memory on the other machine and (re)initialize the pointer to point to it.


Share this post


Link to post
Share on other sites
Thanks all.

CWizard> Well, I do send the length. But in this case the struct has a limited size. If the first char, as it does here, is a flag PLAYERINFO then the buffer will include (and be the length of) a set structure.

/MindWipe

Share this post


Link to post
Share on other sites
In your PacketPointer structure, is the dynamic memory a pointer to memory, or is it an undefined block of data (e.g. if you did:
PacketPointer* p = malloc(SomeLengthOfData+sizeof(char)*2)
?

Share this post


Link to post
Share on other sites
Do I understand your situation correct, if I guess that you have an instance of your structure on computer A and want to send it to computer B?

And, the problem is that the structure includes a pointer, and you want to also send what it points to?

If that data (the dynamic data) is of a type dependent size (given by the first bytes), you should just send the block it points to. Then on the other side, read the two bytes, and then you know the length of the data block. Allocate memory for it and cast it properly, and rebuild your structure.

I''m still not sure what the problem is, but it should not be very hard to make it work.


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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!