Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8211925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:35:29+00:00 2026-06-07T10:35:29+00:00

I have a binary packet format which I must implement a C++ reader for.

  • 0

I have a binary packet format which I must implement a C++ reader for. The library uses Qt 4, and the packet source could be any QIODevice, for example, QTcpSocket, QFile or QBuffer. The format includes the packet format, and each packet may also have a lot of sub-structures inside. I need the reader to return the following:

  • the packet header;
  • the array of sub-structures;
  • the error status of a read operation – success, an error, or not enough data (particularly when reading from a socket or another sort of buffering device).

There are various possible approaches to the reader API:

  1. Packet read(Status &status); – return by value, and return the error status through the reference argument.
  2. Packet *read(bool *ok); – return NULL on error or if not enough data, write true or false to the ok variable (if not NULL) depending on that.
  3. Packet *read(); – return NULL on error or if not enough data, call another method bool wasError(); to check what happened. This one could be merged with the previous one by making the ok parameter have the default value of NULL.
  4. Status read(Packet &packet); – if the returned status is Ok, the read value is placed into the packet variable, otherwise it indicates either an error or EOF.
  5. Packet read(); – return by value, in case of EOF or error return a special “null packet” value. Call wasError() to determine what happened.

There are other possible combinations, of course. There doesn’t seem to be the best choice. The approaches 1, 2 and 4 require the caller to declare a separate variable to store the result. The approaches 2 and 3 involve messing with the heap, which I don’t want to do for obvious reasons. The approach 1 doesn’t make it clear what is returned in case of error. The approach 5 fixes that, but introduces a special “null” flag into the packet structure, although it probably doesn’t belong there.

I could take the 5th approach, but return a special structure containing a packet and the status info, but that introduces another “synthetic” type and still leaves open the question “What will the packet field contain in case of an error?”

Or I could take the 3rd approach and return a QSharedPointer<Packet> instead, so the caller doesn’t have to mess with heap manually. But the Packet structure will probably already be a kind of smart pointer (a shared class) for the sake of the Pimpl. Maybe I can use that internal pointer instead and introduce an isNull() method, like QString does.

Is there a better, or traditional way of doing it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T10:35:31+00:00Added an answer on June 7, 2026 at 10:35 am

    Well, I’ve gone ahead and implemented it as follows.

    The signature of the reading method is:

    Packet readPacket(bool *error = NULL);
    

    The Packet class looks like this:

    class Packet {
        inline Packet(): p(NULL) {} // this is returned on error or EOF
        Packet(const Header &header, const Data &data);
        inline bool isNull() {return p == NULL;}
    private:
        QSharedDataPointer<PacketPrivate> p;
    };
    

    The packet reader also has the bool wasError() method used to retrieve the error status in case the error argument wasn’t provided to the readPacket() function.

    This implementation allows me:

    1. To create elegant loops like while (!(packet = reader->readPacket()).isNull()) ...
    2. To avoid having unrelated fields like “error” or “null” in the Packet class.
    3. To avoid manual allocating or deleting pointers, or having to mess with smart pointers explicitly at the caller’s side.
    4. To avoid having uninitialized fields when using default constructor.

    It is still far from perfect because it is necessary to either provide the error argument or to call wasError() later. But I believe the only way of forcing the caller to check for errors would be to use exceptions, but I don’t want to do it for portability reasons.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Binary Search Tree, I use template to add any class object
I have a binary file, that was written in C, which contains a long
i have a binary file which has 4 KB of header information and then
Say, i have binary protocol, where first 4 bits represent a numeric value which
I'm curious about few things about text algorithms. For example we have binary word
Okay so i have a packed a proprietary binary format. That is basically a
I have a basic client server in erlang which uses tcp. How does one
I have question about interpreting strings as packed binary data in C++. In python,
I have an external device that spits out UDP packets of binary data and
I have binary data in my database that I'll have to convert to bitmap

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.