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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:36:50+00:00 2026-06-18T04:36:50+00:00

I have to work on an prop. binary file format and want to realize

  • 0

I have to work on an prop. binary file format and want to realize it with the use of structs.
I need constant byte sequence in my structs and atm I don’t get it how to realize that.

I thought about something like that:

#include <cstdint>
#pragma pack(push,1)

typedef struct PAYLOAD_INFO {
    const uint8_t magicnumber[4] =  { 0xFA, 0x11 , 0x28 , 0x33 };
    uint16_t UMID;
    const uint16_t VID = 1487 ;
    uint32_t crc;
};

#pragma pack(pop)

int main (){
  PAYLOAD_INFO pldInst;
  pldInst.UMID = 5;
  pldInst.crc = 0x34235a54;
  ...
  writeToFile(&PAYLOAD_INFO,sizeof(PAYLOAD_INFO));
}

In the end “pldInst” should look some like that ( in the memory) , without regard of the byteorder in this example:

0x00000000:  0xFA, 0x11 , 0x28 , 0x33
0x00000004:  0x00, 0x05 , 0x05 , 0xCF
0x00000008:  0x34, 0x23 , 0x5a , 0x54

I already tried the “default” approach:

#include <cstdint>
#pragma pack(push,1)

typedef struct PAYLOAD_INFO {
    static const uint8_t magicnumber[4];
    uint16_t UMID;
    static const uint16_t VID = 1487 ;
    uint32_t crc;
};

const uint8_t magicnumber[4] =  { 0xFA, 0x11 , 0x28 , 0x33 };

#pragma pack(pop)

but doesn’t work as intend.

Is there a way to get this done without calculating the memory size of every struct member,allocating new memory and copying every member ?

I using g++ 4.6.3.

Regard,
Thomas

UPDATE:
The the c++11 solution provided by @bikeshedder works very well but it compiles only with g++ 4.7 or higher.

  • 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-18T04:36:51+00:00Added an answer on June 18, 2026 at 4:36 am

    I would recommend against any kind of casting of structs to raw memory unless you are using some kernel interface which is guaranteed to run on the same machine and is supposed to be used that way.

    This is not portable (endian issues), every compiler does it a little different and the performance really is not so much better. All in all it does not justify this bad practice.

    The reason why your code example doesn’t work are the static members. Since static members are not part of the object (but the class) only UMID and crc are written that way.

    struct PAYLOAD_INFO {
        const uint8_t magicnumber[4] = { 0xFA, 0x11, 0x28, 0x33 };
        uint16_t UMID;
        const uint16_t VID = 1487 ;
        uint32_t crc;
    };
    

    This code only works in C++11, but since you include cstdint I’m assume that you already use that C++ version. If you don’t use C++11 you need to initialize the members in the constructor and you must not use const for the magic number because it can not be initialized. (See: initialize a const array in a class initializer in C++)

    struct PAYLOAD_INFO {
        const uint8_t magicnumber[4] = { 0xFA, 0x11, 0x28, 0x33 };
        uint16_t UMID;
        const uint16_t VID = 1487 ;
        uint32_t crc;
    };
    

    For C++98 you need to do:

    struct PAYLOAD_INFO {
        uint8_t magicnumber[4];
        uint16_t UMID;
        const uint16_t VID;
        uint32_t crc;
        PAYLOAD_INFO() : VID(1487)
        {
            magicnumber[0] = 0xFA;
            magicnumber[1] = 0x11;
            magicnumber[2] = 0x28;
            magicnumber[3] = 0x33;
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to have something like this work: var Events=require('events'), test=new Events.EventEmitter, scope={ prop:true
I need to have multiple data bindings on one element. For example, I want
I have a project and it only contains *.jars and prop.properties and build.xml file.
I have two queries which both work but need to combine them into one
The commandlink link2 does not have work after an ajax call is made to
I have to work with an existing DB and right now I have to
I have inherited work from a previous employee. The issue I'm having is a
I have to work on a database in which there are many stored-procedures &
I have a work page that shows all projects. I am using a taxonomy
I have to work with a string standing for a keystroke (for instance A,

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.