[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [ns] NS2 problem with Packet::datalen() function (packet.h)
I had code in the "recv(Packet* p, Handler* h)" method of my derived
Agent class which _was_ originally calling:
int len = p->datalen();
The returned value of that was "8" while the code:
int len = ((PacketData*)(p->userdata()))->size();
returns the proper length of of the PackData my sender agent had
attached to the Packet (512 bytes)
This was with ns-2.1b7a compiled on Redhat 7.1 with gcc 2.96 ...
I suspect the compiler isn't properly setting up the
AppData/PacketData virtual function tables as a result of all of the
inline functions? Historically with the evolution of C++ compilers,
I think inline virtual functions are an area with which to be
cautious.
At 3:19 PM -0700 5/21/01, Haobo Yu wrote:
> > 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
>
>I don't understand.
>
> inline int datalen() const { return data_ ? data_->size() : 0; }
>
>Here is Packet::datalen(), and data_ is:
>
> AppData* data_; // variable size buffer for 'data'
>
>And here is AppData::size():
>
> virtual int size() const { return sizeof(AppData); }
>
>Sicne it's a virtual function, when you call it from a pointer to the base
>class it'll automatically return whatever the actual value is of the
>derived class.
>
>I'm curious how did you test your code to know it always returns
>sizeof(AppData)?
>
>> 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 ...
>
>I'm very confused. Since the size() function is virtual you do not need
>this explict typecast, which is exactly what a virtual method is intented
>to be...
--
Brian
__________________________________
Brian Adamson
<http://manimac.itd.nrl.navy.mil>
<mailto:[email protected]>