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

  • Home
  • SEARCH
  • 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 6718159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:56:12+00:00 2026-05-26T08:56:12+00:00

Is it possible to properly and safely pass a class object pointer through a

  • 0

Is it possible to properly and safely pass a class object pointer through a POSIX message queue?

For instance,

Object *obj = new Object();

mq_send(mqdes, static_cast<char*>&obj, sizeof(obj), 1);

and on the receive side, perform a reinterpret_cast back to my Object?

Since message queues use a file descriptor on Linux, I am curious how this works. I have tried it unsuccessfully but think I may be doing something wrong.

  • 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-26T08:56:13+00:00Added an answer on May 26, 2026 at 8:56 am

    Not really… only if the object contains only basic type fields and other structs with basic type fields. If you send a pointer it cannot be reused from the other side if it resides in another process or in another system.

    Also using classes with inheritance and virtual methods, it may be a mess!

    It is better to add a sort of Serialize method from my point of view.

    Also passing a struct binary serialized in this way is not portable at all and can bring you to several problems if you want to use the same mechanism with other systems or if you change the structure or things like the packing of the object.

    A custom serialization\deserialization would be preferred and more portable, but the choice is of course your.

    Something like …

    template<typename T>
    int SerializeAndSendObject(mqd_t mqdes, const T* instance)
    {
        MySerializationStream stream;
        instance->SerializeTo(stream);
        mq_send(stream.toBuffer(), stream.size());
    }
    

    If you are just sending between two threads instead of sending the content of the object i would send just the pointer to an object allocated with new and i would deallocate it from the other side.
    Be careful that when you dispose the queue you must have first destroyed all pending objects!

    Object* pointer = &obj;
    mq_send(mqdes, static_cast<char*>(pointer), sizeof(Object*), 1);
    

    Note the sizeof(Object*)… you need to send only the pointer, not the object itself.

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

Sidebar

Related Questions

Possible Duplicate: How to properly clean up Excel interop objects in C# I've read
Possible Duplicate: How to properly clean up Excel interop objects in C# I've this
Possible Duplicate: My regex is not working properly Suppose I have long text. From
Possible Duplicate: Hibernate: different object with the same identifier value was already associated with
How to properly synchronize this? At the moment it is possible that SetData is
Possible Duplicate: Python not sorting unicode properly. Strcoll doesn't help. I'm trying to sort
Possible Duplicate: Obfuscator which supports WPF properly I am looking for a free WPF
Is it possible to lose data if a SQLite DB is not closed properly
I want to, as properly as possible, animate a view from top to bottom.
How to properly call DBMS_OBFUSCATION_TOOLKIT.DESEncrypt ? (without using PL/SQL if possible) select DBMS_OBFUSCATION_TOOLKIT.DESEncrypt('x','y') from

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.