Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int main( int argc, char * argv[] )
- {
- argc; argv;
- #ifdef _WIN64
- printf( "_WIN64\n\n");
- #else
- printf( "_WIN32\n\n");
- #endif
- {
- PacketRef packet1 = NewPacket(); // Valid: Intended use, + 1 ref
- // PacketRef * ptr_ref = &packet1; // DANGEROUS: Can have a "dangling pointer" to an object that no longer exists.
- packet1->Write<uint16_t>( 0 ); // API example
- packet1->Write<uint16_t>( 0x5000 );
- packet1->Write<uint16_t>( 0 );
- packet1->Write<uint8_t>( 1 );
- packet1->Overwrite<uint16_t>( 0, (uint16_t)packet1->GetTotalBytes() - 6 ); // API example
- packet1->Finalize(); // Required for further API use
- // void F( PacketRef packet )
- F( packet1 ); // Valid: copy ctor'ing is ok, + 1 ref, then - 1 ref
- // NOTE: The 'smart pointer' object is copied, actual data is always used via a pointer
- // void G( PacketRef & packet )
- G( packet1 ); // Valid: Just a reference, no copying
- // NOTE: susceptible to dangling reference issues noted below
- //PacketRef packet2; // Invalid: no appropriate default constructor available
- //packet1 = NewPacket(); // Valid: However, packet is freed since no more refs exist
- PacketRef packet3( packet1 ); // Valid: copy ctor'ing is 'ok', + 1 ref
- packet1 = NewPacket(); // Valid: since a ref exists, old packet is not freed
- // Valid: simply holding a normal reference to an object
- PacketRef & packet4 = packet3;
- //packet3 = NewPacket(); // DANGEROUS: We now have a "dangling reference" to an object that no longer exists
- // Valid: Object still exists since packet3 has a ref to it
- uint16_t packet_size = packet4->Read<uint16_t>(); // API use
- uint16_t packet_opcode = packet4->Read<uint16_t>();
- uint16_t packet_security = packet4->Read<uint16_t>();
- printf( "[%.4X][%i bytes][%.4X]\n\n", packet_opcode, packet_size, packet_security );
- } // packet3 dtor, packet1 dtor since no more refs for either
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement