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 57997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T17:46:44+00:00 2026-05-10T17:46:44+00:00

At a previous employer, we were writing binary messages that had to go over

  • 0

At a previous employer, we were writing binary messages that had to go ‘over the wire’ to other computers. Each message had a standard header something like:

class Header {     int type;     int payloadLength; }; 

All of the data was contiguous (header, immediately followed by data). We wanted to get to the payload given that we had a pointer to a header. Traditionally, you might say something like:

char* Header::GetPayload() {     return ((char*) &payloadLength) + sizeof(payloadLength); } 

or even:

char* Header::GetPayload() {     return ((char*) this) + sizeof(Header); } 

That seemed kind of verbose, so I came up with:

char* Header::GetPayload() {     return (char*) &this[1]; } 

It seems rather disturbing at first, possibly too odd to use — but very compact. There was a lot of debate on whether it was brilliant or an abomination.

So which is it – Crime against coding, or nice solution? Have you ever had a similar trade-off?

-Update:

We did try the zero sized array, but at the time, compilers gave warnings. We eventually went to the inhertited technique: Message derives from Header. It works great in practice, but in priciple you are saying a message IsA Header – which seems a little awkward.

  • 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. 2026-05-10T17:46:44+00:00Added an answer on May 10, 2026 at 5:46 pm

    I’d go for crime against coding.

    Both methods will generate the exact same object code. The first makes it’s intention clear. The second is very confusing, with the only advantage that it saves a couple keystrokes. (Just learn to freakin’ type).

    Also, note that NEITHER method is guaranteed to work. The sizeof() an object includes padding for word alignment, so that if the header was:

    class Header {     int type;     int payloadLength;     char  status; }; 

    Both methods you describe would have the payload starting at Header+12, when most likely it actually starts at Header+9.

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

Sidebar

Related Questions

From previous experience I had been under the impression that it's perfectly legal (though
In previous question of mine, someone had meantioned that using Semaphores were expensive in
I was working with a select box today that a previous employee had written.
For my previous employer I've worked with Hibernate, and now that I'm in a
In previous versions of Excel there was a registry entry that you could create
From previous post, I learnt that for there are two ways, at least, to
I've just changed jobs. My previous employer uses Subversion, my new employer uses Perforce.
I developed a web-based training matrix that shows the training record for each employee
Well in a previous question I asked the same thing, but the answer that
I have a database that tracks employee’s data for the current year and previous

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.