ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

How to correctly declare a Polygon message

asked 2014-03-26 21:42:45 -0500

Maya gravatar image

updated 2016-10-24 08:59:42 -0500

ngrennan gravatar image


Easy question, I'm not able to declare a Polygone message that does not core dump. Here is my code for now :

geometry_msgs::Point32 tl;

I have the doc of the Polygon Stamped and the Polygon but I guess I'm reading it really badly.

I tried declaring a table of Point32 as well and directly passing it to polygon.points but it wont compile then.

Thanks a lot for any answer.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2014-03-26 22:20:03 -0500

ahendrix gravatar image

You need to pre-allocate the points array before you assign members into it, or use a function that does the appropriate bounds-checking and resize for you. In particular, arrays in ROS messages in C++ are implemented as std::vectors, and std::vector::operator[] doesn't do any bounds checking.

If you just want to append items to your array, try:

geometry_msgs::Point32 tl;

If you already know how big your array is going to be, and you want to assign to specific indicies, try:

geometry_msgs::Point32 tl;
polyStamp.polygon.points.reserve(10); // ensure that there will be space for at least 10 points
edit flag offensive delete link more


Thanks a lot ! I didn't realize they were implemented as vectors !

Maya gravatar image Maya  ( 2014-03-26 22:29:17 -0500 )edit

Question Tools

1 follower


Asked: 2014-03-26 21:42:45 -0500

Seen: 4,883 times

Last updated: Mar 26 '14