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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:17:59+00:00 2026-06-10T16:17:59+00:00

I have the following struct defining data returned from a collection of HID device

  • 0

I have the following struct defining data returned from a collection of HID device reports:

struct DevInfo {
    char unknown[0x40];
    string name;
    char unknown2[0x240];
};

It’s currently incomplete, but that’s irrelevant to my question. Previously, I was populating an instance of this struct using memcpy to copy data from a char array, like so:

// get data from HID device
unsigned char *response = sendCommand(DEV_REPORT);

// Copy to struct
DevInfo *info;
memcpy(&info, &response[0], sizeof(response));

// Output name
cout << "Name: " << info->name << "\n";

This worked, except that I was apparently doing something that I shouldn’t (returning a reference to a char array from a function). So, after researching, I switched to a safer std::vector<unsigned char> approach, but now I can’t use memcpy to populate the data in the struct.

Someone advised me to use std::vector<DevInfo> instead of std::vector<unsigned char>, but the problem with that is there are several different reports that can be retrieved from the HID device, so I need to be able to populate different structs using the same function (sendCommand).

What’s an appropriate way to get the binary data from my std::vector<unsigned char> to my DevInfo struct?

  • 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-10T16:18:01+00:00Added an answer on June 10, 2026 at 4:18 pm

    There’s nothing stopping you from using either std::memcpy or std::copy to populate an object from binary data stored in a vector, as long as it is a trivially-copyable standard-layout type:

    DevInfo info;
    std::vector<char> response = get_response();
    
    assert(response.size() == sizeof info);
    std::copy(response.begin(), response.end(), reinterpret_cast<char*>(&info));
    std::memcpy(&info, &response[0], sizeof info); // C++11 allows response.data()
    

    However, in your case it appears that you have a non-trivial data member (assuming that string refers to std::string), so you can’t do either of these things.

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

Sidebar

Related Questions

I have declared the following struct: typedef struct _RECOGNITIONRESULT { int begin_time_ms, end_time_ms; char*
I have the following struct: struct Records { int Number; char Name[20]; float Salary;
I have the following struct in C++: #define MAXCHARS 15 typedef struct { char
I have the following struct: typedef struct{ int vin; char* make; char* model; int
VC++ 2010 Lets say I have the following: struct person { char * name;
I have following struct to store my vertex data. struct Rz3DContourNode { float x;
I have following struct defination: typedef union U08_16_t { unsigned long s32;//message32 unsigned char
I have the following struct typedef char String[256]; typedef struct { String name; int
I have the following struct: struct elem { int number; char character; }; struct
I have the following struct, from the NRPE daemon code in C: typedef struct

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.