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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:20:36+00:00 2026-05-13T22:20:36+00:00

Lately I’ve been diving into network programming, and I’m having some difficulty constructing a

  • 0

Lately I’ve been diving into network programming, and I’m having some difficulty constructing a packet with a variable “data” property. Several prior questions have helped tremendously, but I’m still lacking some implementation details. I’m trying to avoid using variable sized arrays, and just use a vector. But I can’t get it to be transmitted correctly, and I believe it’s somewhere during serialization.

Now for some code.

Packet Header

class Packet {

    public:         
        void* Serialize();
        bool Deserialize(void *message);

        unsigned int sender_id;
        unsigned int sequence_number;
        std::vector<char> data;
};

Packet ImpL

typedef struct {
    unsigned int sender_id;
    unsigned int sequence_number;
    std::vector<char> data;
} Packet;

void* Packet::Serialize(int size) {
    Packet* p = (Packet *) malloc(8 + 30);
    p->sender_id = htonl(this->sender_id);
    p->sequence_number = htonl(this->sequence_number);
    p->data.assign(size,'&'); //just for testing purposes
}

bool Packet::Deserialize(void *message) {
   Packet *s = (Packet*)message;
   this->sender_id = ntohl(s->sender_id);
   this->sequence_number = ntohl(s->sequence_number);
   this->data = s->data;
}

During execution, I simply create a packet, assign it’s members, and send/receive accordingly. The above methods are only responsible for serialization. Unfortunately, the data never gets transferred.

Couple of things to point out here. I’m guessing the malloc is wrong, but I’m not sure how else to compute it (i.e. what other value it would be). Other than that, I’m unsure of the proper way to use a vector in this fashion, and would love for someone to show me how (code examples please!) 🙂

Edit: I’ve awarded the question to the most comprehensive answer regarding the implementation with a vector data property. Appreciate all the responses!

  • 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-05-13T22:20:37+00:00Added an answer on May 13, 2026 at 10:20 pm

    This cast is very dangerous as you have allocated some raw memory and then treated it as an initialized object of a non-POD class type. This is likely to cause a crash at some point.

    Packet* p = (Packet *) malloc(8 + 30);
    

    Looking at your code, I assume that you want to write out a sequence of bytes from the Packet object that the seralize function is called on. In this case you have no need of a second packet object. You can create a vector of bytes of the appropriate size and then copy the data across.

    e.g.

    void* Packet::Serialize(int size)
    {
        char* raw_data = new char[sizeof sender_id + sizeof sequence_number + data.size()];
        char* p = raw_data;
        unsigned int tmp;
    
        tmp = htonl(sender_id);
        std::memcpy(p, &tmp, sizeof tmp);
        p += sizeof tmp;
    
        tmp = htonl(sequence_number);
        std::memcpy(p, &tmp, sizeof tmp);
        p += sizeof tmp;
    
        std::copy(data.begin(), data.end(), p);
    
        return raw_data;
    }
    

    This may not be exactly what you intended as I’m not sure what the final object of your size parameter is and your interface is potentially unsafe as you return a pointer to raw data that I assume is supposed to be dynamically allocated. It is much safer to use an object that manages the lifetime of dynamically allocated memory then the caller doesn’t have to guess whether and how to deallocate the memory.

    Also the caller has no way of knowing how much memory was allocated. This may not matter for deallocation but presumably if this buffer is to be copied or streamed then this information is needed.

    It may be better to return a std::vector<char> or to take one by reference, or even make the function a template and use an output iterator.

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

Sidebar

Related Questions

Lately I've been running into some subtle layout issues in my iOS app. For
Lately we've been having issues on entity framework saving data objects even though we
Lately I have been doing some numerical method programming in C. For the bug
Lately, I've gotten some weird linker errors. I've been taught that there's two ways
Lately I've been writing some JS code using jQuery and JavaScript as it is
Lately I have been having more and more issues with the content assist in
Lately we've been getting into a bit of co-development, and I don't think I'm
Lately I have been finding that some of the ColdFusion applications on my production
Lately, I've been reading some code that has if (! (a == b) )
Lately I have been trying my hands on Eclipse IDE for java development. I

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.