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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:17:20+00:00 2026-05-12T06:17:20+00:00

I have to send and receive dynamic data using a SysV message queue for

  • 0

I have to send and receive dynamic data using a SysV message queue for a university project.

The length of the data is transmitted in a separate message, size is therefor already known.

And this is how I try to receive the data. I have to admit that I’m not a C++ specialist, especially when it comes to memory allocation.

struct {
    long mtype;
    char *mdata;
} msg;

msg.mdata = (char *)malloc(size * sizeof(char));

msgrcv(MSGQ_ID, &msg, size, MSG_ID, 0);

The problem seems to be the malloc call, but I don’t know how to do this right.

EDIT

What I try is to have a some sort of read method in a OO wrapper around the message queues. I’d like to read the data in the message queue into a char[] or a std::string. What I have now looks (simplified) like this.

bool Wrapper::read(char *data, int length)
{
    struct Message {
        long mtype;
        std::string mdata;
    };

    Message msg;
    msg.mdata = std::string(size, '\0');

    if(msgrcv(MSGQ_ID, &msg, size, MSG_ID, 0) < 0)
    {
        return false;
    }

    memcpy(data, msg.mdata.c_str(), msg.mdata.size());

    return true;
}

All I get is segmentation faults or completely corrupt data (although this data sometimes contains what I want).

  • 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-12T06:17:20+00:00Added an answer on May 12, 2026 at 6:17 am

    You can’t pass a pointer to a structure that contains a std::string member to msgrcv, this violates the interface contract.

    The second parameter passed to msgrcv needs to point to a buffer with sufficient space to store a ‘plain’ C struct of the form struct { long mtype; char mdata[size]; }; where size is the third parameter to msgrcv.

    Unfortunately, determining the size of this buffer might depend on size due to possible alignment issues but you have to assume that it doesn’t on a system that provides this sort of interface. You can use the standard offsetof macro to help determine this size.

    As a vector stores its components contiguously, once you know the size of the buffer, you can resize a vector of char and use this to hold the buffer. Using a vector relieves you of the obligation to free or delete[] a buffer manually.

    You need to do something like this.

    std::string RecvMessage()
    {
        extern size_t size; // maximum size, should be a parameter??
        extern int MSGQ_ID; // message queue id, should be a parameter??
        extern long MSG_ID; // message type, should be a parameter??
    
        // ugly struct hack required by msgrcv
        struct RawMessage {
            long mtype;
            char mdata[1];
        };
    
        size_t data_offset = offsetof(RawMessage, mdata);
    
        // Allocate a buffer of the correct size for message
        std::vector<char> msgbuf(size + data_offset);
    
        ssize_t bytes_read;
    
        // Read raw message
        if((bytes_read = msgrcv(MSGQ_ID, &msgbuf[0], size, MSG_ID, 0)) < 0)
        {
            throw MsgRecvFailedException();
        }
    
        // a string encapsulates the data and the size, why not just return one
        return std::string(msgbuf.begin() + data_offset, msgbuf.begin() + data_offset + bytes_read);
    }
    

    To go the other way, you just have to pack the data into a struct hack compatible data array as required by the msgsnd interface. As others have pointer out, it’s not a good interface, but glossing over the implementation defined behaviour and alignment concerns, something like this should work.

    e.g.

    void SendMessage(const std::string& data)
    {
        extern int MSGQ_ID; // message queue id, should be a parameter??
        extern long MSG_ID; // message type, should be a parameter??
    
        // ugly struct hack required by msgsnd
        struct RawMessage {
            long mtype;
            char mdata[1];
        };
    
        size_t data_offset = offsetof(RawMessage, mdata);
    
        // Allocate a buffer of the required size for message
        std::vector<char> msgbuf(data.size() + data_offset);
    
        long mtype = MSG_ID;
        const char* mtypeptr = reinterpret_cast<char*>(&mtype);
    
        std::copy(mtypeptr, mtypeptr + sizeof mtype, &msgbuf[0]);
        std::copy(data.begin(), data.end(), &msgbuf[data_offset]);
    
        int result = msgsnd(MSGQ_ID, &msgbuf[0], msgbuf.size(), 0);
        if (result != 0)
        {
            throw MsgSendFailedException();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 194k
  • Answers 194k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer _tf.filters = [new DropShadowFilter()]; May 12, 2026 at 6:41 pm
  • Editorial Team
    Editorial Team added an answer The gory details in the spec are actually reasonably readable.… May 12, 2026 at 6:41 pm
  • Editorial Team
    Editorial Team added an answer Reformulated the question in this thread: Can a stored procedure/function… May 12, 2026 at 6:41 pm

Related Questions

I'm not familiar with ADO.NET Data Services but it looks usable. All I need
What would be the easiest way to be able to send and receive raw
I have a very simple problem. I have an application which is written in
I have managed to send out (and receive) binary SMSs, but what I want

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.