Jump to content

  • Log In with Google      Sign In   
  • Create Account


Reading from file to structs


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
22 replies to this topic

#21 Tispe   Members   -  Reputation: 978

Like
1Likes
Like

Posted 12 October 2011 - 03:30 PM

The usual way of using in-place memory offsets:
1) int offset = int((char*)(&object+1) - (char*)(&object.name)); //offset address is now between bar and baz?
2) object->name = ((char*)&object->name) + int(object->name);//did you mean offset?

1)Offset is the distance from the start of the 'name' field to the end of the structure. The string data itself is written to the file after the structure, so the offset tells you how far forward in the file to jump in order to find the string.
2) Upon deserialisation, 'name' actually contains the above offset value, not a pointer. The offset is relative to the address of the 'name' field, so the address of 'name' is added to the integer value of 'name', resulting in a pointer to the string data.


small problem:
struct LOADSPRITEOBJECT
{
	char* name;
	D3DCOLOR Color;
	D3DXVECTOR3 OffsetPosition;
	bool visible;
};
int offset = int((char*)(&object+1) - (char*)(&object.name));
write( &offset, sizeof(int) );

What happens is that (char*)(&object+1) - (char*)(&object.name) lands you exactly 3 bytes too far. This is because it assumes that there is only a (char*) written (1 Byte) when there is actually 4 bytes written (int). You must subract 3 bytes from offset.

Sponsor:

#22 Yann L   Moderators   -  Reputation: 1794

Like
0Likes
Like

Posted 12 October 2011 - 04:54 PM

The usual way of using in-place memory offsets:

struct Foo {
	int bar;
	char* name;
	int baz;
};

-- serialization:
Foo object;
write( &object.bar, sizeof(int) );
int offset = int((char*)(&object+1) - (char*)(&object.name));
write( &offset, sizeof(int) );
write( &object.baz, sizeof(int) );
write( object.name, strlen(object.name)+1 );

-- deserialization:
void* buffer = readWholeFile();
Foo* object = (Foo*)buffer;
object->name = ((char*)&object->name) + int(object->name);

This is going to blow up right into your face the day you decide to port your code to 64bit. And your data file layout will change with the memory architecture. Ugh.

#23 Hodgman   Moderators   -  Reputation: 27622

Like
1Likes
Like

Posted 12 October 2011 - 06:04 PM

This is going to blow up right into your face the day you decide to port your code to 64bit. And your data file layout will change with the memory architecture. Ugh.

See the caveat in a later post regarding struct Offset, which I find preferable to patching pointers into loaded blobs. If we do need to store pointers in blobs, I'd use the largest pointer type for the targets that the data file is being built for.

You must subract 3 bytes from offset.

Good catch. That's what I get for writing code from memory.




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