Re: TBuffer streaming from Char *

From: Fons Rademakers (Fons.Rademakers@cern.ch)
Date: Mon Oct 11 1999 - 12:16:04 MEST


Hi George,

   TBuffer &TBuffer::operator>>(Char_t *c) was indeed broken and I've fixed
it as per your suggestion.

Cheers, Fons.


George Heintzelman wrote:
> 
> Hi,
> 
> I'm using TBuffers so I can use Root streamers for writing to
> non-filesystem places (in my case an oracle database, but that doesn't
> really matter) and in the course of working with some char * variables
> came upon this:
> 
> inline TBuffer &TBuffer::operator>>(Char_t *c)
> {
>    ReadString(c, sizeof(c));
>    return *this;
> }
> 
> inline TBuffer &TBuffer::operator<<(const Char_t *c)
> {
>    WriteString(c);
>    return *this;
> }
> 
> where ReadString takes a maximum number of bytes parameter and
> WriteString writes up to the terminating 0 in the string passed.
> 
> With this code, using operator>> on a Char_t * variable will not read
> in the reverse of one that was written with an operator<<, since the
> sizeof(c) call will always resolve to 4 or maybe 8 on some machines.
> 
> I think the best way to deal with that is for the operator>> to be
> cavalier about it and just overwrite memory, since it has no way of
> knowing how big the buffer it should write into is, and since the user
> programmer is working with char *'s, it's his responsibility to make
> sure the memory allocation and stuff is correct. If it breaks, one just
> shrugs and says, "you should've used TString..."; but I think it's
> unfair for the user to be bitten by operator>>  not reading in, in
> exactly the same way, what operator<< wrote out.
> 
> George Heintzelman
> gah@bnl.gov

-- 
Org:    CERN, European Laboratory for Particle Physics.
Mail:   1211 Geneve 23, Switzerland
E-Mail: Fons.Rademakers@cern.ch              Phone: +41 22 7679248
WWW:    http://root.cern.ch/~rdm/            Fax:   +41 22 7677910



This archive was generated by hypermail 2b29 : Tue Jan 04 2000 - 00:43:40 MET