Ask Your Question
11

What is ConstPtr&?

asked 2015-07-03 14:02:45 -0500

OmoNaija gravatar image

updated 2015-07-03 14:36:57 -0500

Hey All, I'm still new to ROS and C++. I'm having trouble understanding what the ConstPtr& does when writing the callback function for a simple subscriber:

 void chatterCallback(const std_msgs::String::ConstPtr& msg) 

     {ROS_INFO("I heard: [%s]", msg->data.c_str());}

Wouldn't the code work with just:

 void chatterCallback(const std_msgs::String msg)

      {ROS_INFO("I heard: [%s]", msg);}
edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
24

answered 2015-07-04 01:05:17 -0500

fergs gravatar image

updated 2018-11-10 13:11:15 -0500

ahendrix gravatar image

When messages are automatically generated into C++ code, there are several typedefs defined. One of them is ::Ptr, which is typedef-ed to be a boost::shared_ptr<MSG>, and another is ::ConstPtr which is boost::shared_ptr<MSG const>.

By passing a const pointer into the callback, we avoid doing a copy. While this might not make much difference for std_msgs::String, it can make a huge difference for sensor_msgs::PointCloud2.

edit flag offensive delete link more

Comments

Thank You!

OmoNaija gravatar image OmoNaija  ( 2015-07-06 14:28:54 -0500 )edit

thanks a lot for your help

feixiao gravatar image feixiao  ( 2016-01-21 21:57:54 -0500 )edit

but what does & means? If msg is already a pointer, why do we take the address of msg? Or does that & means passing by reference?

wy3 gravatar image wy3  ( 2017-07-29 15:27:13 -0500 )edit
2

@wy3: the ampersand (&) means pass by reference as you said.

ksirks gravatar image ksirks  ( 2017-09-15 10:47:56 -0500 )edit

Note: today I think boost::shared_ptr<> is replaced in ROS by std::shared_ptr<>, since this is now part of the C++ Standard Library.

eRCaGuy gravatar image eRCaGuy  ( 2020-06-10 14:40:22 -0500 )edit

@wy3, the & is to pass the shared_ptr object itself by reference. The shared_ptr object is a class which manages the object it wraps and "points to", and the & is to pass the shared_ptr object by reference. So, we are passing a message by a reference to a smart pointer which points to it--yeah, it's kind of a double-layered approach. In C you'd just pass the message by pointer and be done, but the added benefit of using a shared pointer, which is a type of "smart pointer", is that it automatically manages the storage duration of the memory (for the message) it points to, meaning you never have to manually free or delete this dynamically-allocated memory block for the message because it's done automatically when all shared pointers to that memory are gone or out of scope.

eRCaGuy gravatar image eRCaGuy  ( 2020-06-10 14:47:18 -0500 )edit

I just added my above comments, and more, into my own answer here. I realized my comments were becoming more than just comments, so I made them into an answer.

eRCaGuy gravatar image eRCaGuy  ( 2020-06-10 15:15:18 -0500 )edit

ROS1 still very much uses boost in roscpp - changing to std::shared_ptr would break the world. ROS2 does drop boost for std::shared_ptr in rclcpp.

fergs gravatar image fergs  ( 2020-08-18 18:05:26 -0500 )edit
3

answered 2020-06-10 15:13:05 -0500

eRCaGuy gravatar image

updated 2020-06-10 15:31:36 -0500

I'd like to expound upon the main answer here.

::ConstPtr can be understood and read as "shared pointer to a constant message", and ::Ptr can be understood and read as "shared pointer to a (modifiable) message". It's just a smart-pointer-based light-weight way of passing messages. Choose the correct one based on whether or not the recipient of the passed message will need to modify the message. Read more about std::shared_ptr<>here: https://en.cppreference.com/w/cpp/mem....

Therefore, ::ConstPtr& can be understood and read as "reference to a shared pointer to a constant message", and ::Ptr& can be understood and read as "reference to a shared pointer to a (modifiable) message".

Today I think boost::shared_ptr<> is replaced in ROS by std::shared_ptr<>, since this is now part of the C++ Standard Library. See here: https://github.com/ros-planning/movei....

That means this is what these definitions would be today:

::Ptr is typedef-ed to std::shared_ptr<MSG>, which is a shared pointer to a message, and ::ConstPtr is typedef-ed to std::shared_ptr<MSG const>, which is a shared pointer to a const message.

Addressing this question in the comments under the main answer:

but what does & means? If msg is already a pointer, why do we take the address of msg? Or does that & means passing by reference?

The & is to pass the shared_ptr object itself by reference. The shared_ptr object is a class which manages the object it wraps and "points to", and the & is to pass the shared_ptr object by reference. So, we are passing a message by a reference to a smart pointer which points to it--yeah, it's kind of a double-layered approach. In C you'd just pass the message by pointer and be done, but the added benefit of using a shared pointer, which is a type of "smart pointer", is that it automatically manages the storage duration of the memory (for the message) it points to, meaning you never have to manually free or delete this dynamically-allocated memory block for the message because it's done automatically when all shared pointers to that memory are gone or out of scope.

So, passing a smart pointer saves potentially a TON of data and time in the passing of the message, and passing the smart pointer itself by reference rather than value saves a tiny bit more of data and time by not having to pass the entire smart pointer object, but rather just a reference to it. It is another small bit of savings in the message passing.

Also, I hate the names ::ConstPtr and ::Ptr. They create confusion. I think they should be named ::PtrToConstMsg and ::PtrToMsg, respectively. This is so much more clear and easy to understand. I recommend that ROS make that change.

edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools

3 followers

Stats

Asked: 2015-07-03 14:02:45 -0500

Seen: 17,473 times

Last updated: Jun 10