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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:06:42+00:00 2026-06-11T20:06:42+00:00

I am trying to build and write a binary request and have a is

  • 0

I am trying to build and write a binary request and have a “is this possible” type question. It might be important for me to mention the recipiant of the request is not aware of the data structure I have included below, it’s just expecting a sequence of bytes, but using a struct seemed like a handy way to prepare the pieces of the request, then write them easily.

Writing the header and footer is fine as they are fixed size but I’m running into problems with the struct “Details”, because of the vector. For now Im writing to a file so I can check the request is to spec, but the intention is to write to a PLC using boost asio serial port eventually

I can use syntax like so to write a struct, but that writes pointer addresses rather than values when it gets to the vector

myFile.write((char*) &myDataRequest, drSize);

I can use this sytax to write a vector by itself, but I must include the indexer at 0 to write the values

myFile.write((char*) &myVector[0], vectorSize);

Is there an elegant way to binary write a struct containing a vector (or other suitable collection), doing it in one go? Say for example if I declared the vector differently, or am I resigned to making multiple writes for the content inside the struct. If I replace the vector with an array I can send the struct in one go (without needing to include any indexer) but I dont know the required size until run time so I don’t think it is suitable.

My Struct

    struct Header
    {   ...     };

    struct Details
    {
           std::vector<DataRequest> DRList;
    };

    struct DataRequest
    {
           short numAddresses;          // Number of operands to be read   Bytes 0-1
           unsigned char operandType;   //                                  Byte 2
           unsigned char Reserved1;     //Should be 0xFF                    Byte 3
           std::vector<short> addressList;  // either, starting address (for sequence), or a list of addresses (for non-sequential) 
    };

    struct Footer
    {   ...     };
  • 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-11T20:06:43+00:00Added an answer on June 11, 2026 at 8:06 pm

    It’s not possible because the std::vector object doesn’t actually contain an array but rather a pointer to a block of memory. However, I’m tempted to claim that being able to write a raw struct like that is not desireable:

    1. I believe that by treating a struct as a block of memory you may end up sending padding bytes, I don’t think this is desireable.

    2. Depending on what you write to you may find that writes are buffered anyway, so multiple write calls aren’t actually less efficient.

    3. Chances are that you want to do something with the fields being sent over. In particular, with the numeric values you send. This requires enforcing a byte order which both sides of the transmission agree on. In order to be portable, you should exlicitely convert the byte order to make sure that your software is portable (if this is required).

    To make a long story short: I suspect writing out each field one by one is not less efficient, it also is more correct.

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

Sidebar

Related Questions

I'm trying to write a very lightweight build script which will basically just get
I'm trying to write an MSBuild task to build a database using FluentNhibernate mappings.
Trying to build out an exception if move.UserId does not equal currentUserId then Redirect
I am trying to build a website with mysql and php. This is the
I am trying to build an application which does the following : 1) write
I'm trying to build my first app on CodeIgniter. This is also my first
I'm trying to build my first app on CodeIgniter. This is also my first
I'm trying to build an object that looks something like this: public class MyObject
I'm trying to build a basic slider in jQuery. I have 5 primary sections
I'm trying to build a program that will be able to read and write

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.