Ask Your Question

object pointer as ROS message

asked 2015-05-05 11:59:35 -0600

Yianni gravatar image

updated 2015-05-05 12:02:51 -0600

Is it possible to pass an object pointer as a ROS message? Below a MWE, that doesn't even get to compile with error message:

talker.cpp:20:12: error: invalid conversion from ‘Foo*’ to ‘std_msgs::UInt64_<std::allocator<void> >::_data_type {aka long unsigned int}’ [-fpermissive] = &foo;


#include <iostream>
#include "ros/ros.h"
#include "std_msgs/UInt64.h"

class Foo
      int count;

int main(int argc, char **argv)
  ros::init(argc, argv, "talker");
  ros::NodeHandle n;
  ros::Publisher chatter_pub = n.advertise<std_msgs::UInt64>("chatter", 1000);
  ros::Rate loop_rate(10);
  Foo foo;
  foo.count = 0;
  std_msgs::UInt64 msg; = &foo;
  while (ros::ok())
  return 0;

Might be a silly question give that the pointer is of the object type pointer, but all I need to do is pass the integer hex memory address to another node (and hopefully create a same object pointer that points to the same address). Possibly, a dangerous thing to do but it will suffice. Any help appreciated.

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2015-05-05 12:52:47 -0600

ahendrix gravatar image

You can't do this.

The OS will run each process in its own memory space, and this will prevent and kind of memory-sharing between processes. Even if your program compiled and ran, it would not work; you'd either get a segmentation fault or silently corrupt memory.

Instead, you should create a custom message type for the data you're trying to pass between nodes, and transmit the entire data structure from one node to another. (This will also work even if your nodes aren't running on the same computer).

edit flag offensive delete link more



@ahendrix is correct. I'd add that if you are concerned about the memory requirements of having two copies of the object or the communication bandwidth requirements, nodelets could be a useful solution for you:

jarvisschultz gravatar image jarvisschultz  ( 2015-05-05 13:22:02 -0600 )edit

@ahendrix@jarvisschultz Thanks. Yes, makes sense as there would be I guess nasty security loopholes then. Unfortunately, it is not a memory issue but rather trying to get openni2_launch with openni2_tracker to work together, only one can open the xtion with the other getting the device descriptor.

Yianni gravatar image Yianni  ( 2015-05-06 04:14:46 -0600 )edit

Please just ask a new question about using the openni2_launch and openni2_tracker together.

ahendrix gravatar image ahendrix  ( 2015-05-06 11:37:26 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



Asked: 2015-05-05 11:59:35 -0600

Seen: 2,350 times

Last updated: May 05 '15