[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [ns] NS2 problem with Packet::datalen() function (packet.h)
At 7:34 PM -0700 5/17/01, Haobo Yu wrote:
> > For anyone interested, in "packet.h", there is a method define
>"datalen()" :
>>
>> inline int Packet::datalen() const {return data_ ? data_->size() : 0}
>>
>> Since "data_" is actually a pointer of type AppData (the base class
>> of PacketData), this actually ends up returning the sizeof the
>> AppData class instead of the actual data_'s datalen_ ...
>
>AppData is an abstract class, and its size() method is a virtual one so
>type checking that you mentioned below is done automatically. Typecasting
>it to 'PacketData' will ruin it.
The current problem I have is that the AppData base class
implementation of returns
"sizeof(AppData)"
So the current "int Packet::datalen()" function always returns
"sizeof(AppData)" instead of the packet's "datalen()" ... which I
believe was the intention of the "datalen()" method of the "Packet"
class. There are probably alternative (better) ways to remedy this
situation ... Perhaps the type of the "data_" member of the Packet
class should be of type PacketData instead of AppData? ... Any
suggestions for a better fix ... My current work-around in my own
code is to access the Packet "data_" pointer (via the "userdata()
method), cast is as type "PacketData" and get the PacketData size()
... This seems a faily non-intuitive approach as compared to using
the Packet::datalen() method given some fix for it ...
>
>- Haobo
>
>> A fix for this is to cast the "data_" member before calling the
>> virtual size() method:
>>
>> inline int Packet::datalen() const {return data_ ?
>> ((PacketData*)data_)->size() : 0}
>>
>>
>> (Note this assumption is that the "data_" member _is_ of type
>> PACKET_DATA ... the datalen() function could be made safer with a
>> check to verify the type is PACKET_DATA before accessing the size()
>> method ...)
>>
>> This is at least the case on ns compiled with gcc-2.96 under Redhat Linux
>>
--
Brian
__________________________________
Brian Adamson
<http://manimac.itd.nrl.navy.mil>
<mailto:[email protected]>