Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


arrays and serialization


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 Burnt_Fyr   Members   -  Reputation: 1456

Like
0Likes
Like

Posted 13 January 2014 - 06:32 PM

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.



Sponsor:

#2 ApochPiQ   Moderators   -  Reputation: 17632

Like
1Likes
Like

Posted 13 January 2014 - 06:54 PM

index = (pointer_to_given_element - pointer_to_element_zero)


#3 richardurich   Members   -  Reputation: 1191

Like
-2Likes
Like

Posted 13 January 2014 - 07:27 PM

<Deleted since it was 100% wrong. Responses below correct the mistake, so read those instead.>


Edited by richardurich, 13 January 2014 - 11:29 PM.


#4 Hodgman   Moderators   -  Reputation: 40230

Like
3Likes
Like

Posted 13 January 2014 - 07:34 PM

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, 13 January 2014 - 07:38 PM.


#5 ApochPiQ   Moderators   -  Reputation: 17632

Like
1Likes
Like

Posted 13 January 2014 - 07:53 PM

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).

#6 richardurich   Members   -  Reputation: 1191

Like
0Likes
Like

Posted 13 January 2014 - 11:27 PM

 

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.



#7 Burnt_Fyr   Members   -  Reputation: 1456

Like
0Likes
Like

Posted 14 January 2014 - 10:26 AM

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.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS