hi Adam,
many thanks for the clarification and for sure I will stick to fix-sized type. the whole story was:
- before I write a message to file, I have to write the length of the object first, so I can parse and reconstruct it in memory thru SerializeFromArray(char*, size).
- when I do it, I defined a simple message(containing one single fixed32) to keep track of the length, and you know we must make it a fix-sized type because we have to parse the length itself thru SerializeFromArray which requires a exact correct SIZE!
- so, in my case, I need to tell the size before I parse my size-tracking message before hand in a static way, without breaking my code even
if I got Google Protocolbuf updated someday in the future.
- do you know some good practice to do the serialization? I am not sending and receiving data via network so I do have to keep track of message length myself for every object to be written to the file, and I want both backward and forward compatibility!
thanks and looking forward to hearing form you
all the best,
Charlie Chen,
发自我的小米手机
在 Adam Cozzette <[email protected]>,2016年9月26日 下午6:43写道:
-- Yes, in your case the serialized size will remain the same because you are using simple fixed-size fields and always initializing them.There are a few things to watch out for, though:- If you use proto3 syntax, zero-valued fields will be omitted from the serialized output because zero is the default anyway. (But based on your example you're using proto2, so that's not an issue.)- Varints of course will be variable sized, so you will have to avoid those and stick to fixed-size types.- If you parse a message that has any unknown fields then those unknown fields could take up an arbitrary amount of space when you reserialize the message.On Mon, Sep 26, 2016 at 12:06 PM, <[email protected]> wrote:
在 2016年9月26日星期一 UTC-4下午2:57:14,[email protected]写道:Will Google Protocol Buffer ensure this? This is a very important I think.Notes: My message only uses fixed32 and all fields are initialized(values might vary)Message test_msg{optional fixed32 field1 = 1;optional fixed32 field2 = 2;}test_msg msg;msg.set_field1(0);msg.set_field2(1);int size = msg.ByteSize(); // will this be a fixed length even if I upgrade Google Protocol Buffer?--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.
