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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:49:57+00:00 2026-06-12T12:49:57+00:00

I have a pointer to raw data using const u_char*, and a generic class

  • 0

I have a pointer to raw data using const u_char*, and a generic class like this

class Rectangle 
{
   u_int8_t length;
   u_int8_t height;
   ...
}

Assuming the raw data is a binary “stream” of bytes, what is the best way to get the raw data into the fields of the class.

-memcpy ?
-cast   ?

i could do this :

Rectangle *rect = (Rectangle*)rawdata;

but i know that is a “old style” cast.

What is the proper way ?

  • 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-12T12:49:58+00:00Added an answer on June 12, 2026 at 12:49 pm

    The easiest and most reliable method is to simply use a convert constructor:

    class Rectangle
    {
    public:
      Rectangle(uint8_t l, uint8_t h) : length(l), height(h) {};
      // ...
    };
    

    This should be your go-to method until for whatever reason this is impossible.

    Barring this, the next best thing to do is simply do a memberwise initialization:

    Rectangle rect;
    rect.width = 20;
    rect.height = 40;
    

    If it becomes impossible to do the above, and iff the object in question is what the Standard refers to as an “aggregate” (basically a POD), you can use an initializer like this:

    Recatagle rect = {10,20};
    

    When doing this, you must bear in mind that the members will be initialized in the order in which they are declared in the class. If you change the order of declaration, you will break every initialization like the above. This is very brittle. For this reason, I limit my use of a construct like this to cases where the class in question is highly localized (like a helper class in a single translation unit), and I document the need to keep the order of declaration intact.

    EDIT PER COMMENTS:

    In the instance you are trying to copy strings in to your class, or pointers to any sort of data, you will need to do a deep copy:

    class Gizmo
    {
    public:
      Gizmo(const char* str) : str_(0)
      {
        str_ = new char[strlen(str)+1];
        strcpy(str_,str);
      }
    };
    

    Note the clumsiness and how brittle the above code is. There are plenty of things that could go wrong here. Not the least of which are forgetting to delete str_ when Gizmo is destroyed, the ugliness and seeming lack of necessity for newing a char string in the first place, one-past-the-end errors … the list goes on. For these reasons, it’s best to avoid using raw pointers at all and using either smart pointers (ie unique_ptr, shared_ptr, etc) or collection classes. In this case, I’d use a std::string, which can be thought of as a collection class:

    class Gizmo
    {
    public:
      Gizmo(const char* str) : str_(str) {};
    private:
      std::string str_;
    };
    

    Feel free to convert this for use with a u_char*, and to add robustness by means of verifying the source pointer is valid.

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

Sidebar

Related Questions

I have defined a generic tree-node class like this: template<class DataType> class GenericNode {
I have an class which should send/receive data in packet form. This class contains
I have a pointer to integer array of 10. What should dereferencing this pointer
I have a pointer to a QScriptEngine that I'm passing through the overloaded class
I have a pointer to a map that I am trying to delete (this
I have a pointer which points to a function. I would like to: if
So I have a template class that I would like to accept an std::map
int str_len = read(m_events[i].data.fd, buf, BUF_SIZE); I have this and I read data into
I have a System.Array of value struct types, something like this: public value struct
I have an application that receives a pointer to JPEG data from a camera

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.