Advertisement Jump to content
Sign in to follow this  
Burnt_Fyr

arrays and serialization

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

I'm working my way into animation, and am currently working on serialization of skeleton/animation data. Since VS2010 mangles strings in the debugger, I'm using the save file to double check my input data has been loaded correctly.

 

My skeleton consists of a name, a count of bones, and a pointer to the array of bones. Each bone consists of a name,a transform, and a pointer to it's parent bone.  I need to know how to get the index of a parent in the array via the pointer to the parent.

 

 

 

tl;dr; How can I get the index of an element in an array from a pointer to said element.

Share this post


Link to post
Share on other sites
Advertisement

ApochPiQ is right for char/byte arrays, but you need to divide by sizeof(element) if you're dealing with something other than bytes.

If you're using the appropriate types, then this all happens automatically.
i.e. this will be correct:
Foo data[42];
Foo* base = &data[0];
Foo* item = &data[12];
int itemIndex = item - base;
assert( itemIndex == 12 );
and so will this ugly version:
...
int byteOffset = ((char*)item) - ((char*)base);
assert( byteOffset == 12 * sizeof(Foo) );
itemIndex = byteOffset / sizeof(Foo);
assert( itemIndex == 12 );
Edited by Hodgman

Share this post


Link to post
Share on other sites

ApochPiQ is right for char/byte arrays, but you need to divide by sizeof(element) if you're dealing with something other than bytes.


To elaborate on Hodgman's answer, pointer arithmetic in C and C++ is not done in bytes. For some pointer type T*, adding 1 will increment the address by the size of T in bytes. Note that sizeof() offers the size in chars which is not necessarily the same thing as bytes (although it is on every contemporary architecture I'm aware of).

Share this post


Link to post
Share on other sites

 

ApochPiQ is right for char/byte arrays, but you need to divide by sizeof(element) if you're dealing with something other than bytes.

If you're using the appropriate types, then this all happens automatically.
i.e. this will be correct:
Foo data[42];
Foo* base = &data[0];
Foo* item = &data[12];
int itemIndex = item - base;
assert( itemIndex == 12 );
and so will this ugly version:
...
int byteOffset = ((char*)item) - ((char*)base);
assert( byteOffset == 12 * sizeof(Foo) );
itemIndex = byteOffset / sizeof(Foo);
assert( itemIndex == 12 );

You're absolutely right. Thanks for correcting me.

Share this post


Link to post
Share on other sites

Thanks Apoch + Hodg. Pointer arithmetic is something that I haven't had too much experience with but makes perfect sense now that I think about it.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!