You probably shouldn't rely on this.  Some of the serialization methods will
guarantee this, some won't.  What you can do is call ByteSize() yourself,
and then use SerializeWithCachedSizes(), which requires that ByteSize() was
already called and won't call it again.  You'll have to do a small amount of
extra setup (allocating a ZeroCopyOutputStream and a CodedOutputStream), but
it's pretty straightforward -- you can just copy the code out of message.cc.
Another thing that you could do which is specific to your use case:  Instead
of allocating a byte array to serialize into, just use a string:

string bytes;
pkt.SerializeToString(&bytes);
send(fd, bytes.data(), bytes.size(), 0);

This is better anyway since it won't break if your messages become larger
than SIZE.

On Fri, Sep 19, 2008 at 2:12 PM, Leandro Lucarella <[EMAIL PROTECTED]> wrote:

>
> Hi.
>
> I guess SerializeToXxx() methods internally calls to ByteSize(), so after
> a serialization, I can call GetCachedSize().
>
> My question is, first, is this true? =) And second, can I rely on this?
>
> The use case is this:
>
> pkt.SerializeToArray(buffer, SIZE);
> send(fd, buffer, pkt.GetCachedSize(), 0);
>
> Is this safe or should I use pkt.ByteSize() instead?
>
>
> Thank you.
>
> --
> Leandro Lucarella (luca) | Blog colectivo:
> http://www.mazziblog.com.ar/blog/
>
> ----------------------------------------------------------------------------
> GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145  104C 949E BFB6 5F5A 8D05)
>
> ----------------------------------------------------------------------------
> Soñé que tenia un caballo
> Que me trataba mejor que vos
> Tenia tan buena onda con ella
> Era mi yegua, mucho mas que vos
>
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To post to this group, send email to protobuf@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/protobuf?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to