Ask Your Question

Why Publisher publish a message but Subscriber callback recieves a pointer of message?

asked 2021-10-15 01:20:35 -0500

Hieu Joestar gravatar image

updated 2021-10-15 02:11:01 -0500

gvdhoorn gravatar image

I wonder why publisher publish a message. EX:


But the Subscriber CallBack function has a argument of pointer of msg. EX:

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

Link of above code:

edit retag flag offensive close merge delete



I've changed mess to message, as "a mess" is something completely different and rather confusing.

When I first read your question title I assumed you had a problem with data corruption.

gvdhoorn gravatar image gvdhoorn  ( 2021-10-15 02:10:36 -0500 )edit

oh sorry for my bad english.Message is really the word i want to say. Thank you.

Hieu Joestar gravatar image Hieu Joestar  ( 2021-10-15 08:02:09 -0500 )edit

No problem.

gvdhoorn gravatar image gvdhoorn  ( 2021-10-15 08:05:18 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2021-10-15 08:54:46 -0500

Mike Scheutzow gravatar image

A ConstPtr is the most efficient way to pass the message to your function, especially if it's a very large message. ros does support passing the message using other prototypes, but there's rarely a good reason to use any of them.

edit flag offensive delete link more


Hi sir, i know the reason why we use pointer. But why publish() just expect the msg( EX:std_msgs::String msg) instead of std_msgs::String::ConstPtr like a Callback function expect.

Hieu Joestar gravatar image Hieu Joestar  ( 2021-10-16 20:11:24 -0500 )edit

publish() takes a plain std_msgs::String to make writing code more convenient for you. It will also accept a ::Ptr or a ::ConstPtr message if that's what you have.

Mike Scheutzow gravatar image Mike Scheutzow  ( 2021-10-17 10:05:47 -0500 )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: 2021-10-15 01:20:35 -0500

Seen: 95 times

Last updated: Oct 15 '21