Ask Your Question

How to get length of array of geometry_msg:Points in service handle?

asked 2020-08-06 06:39:17 -0500

LukeAI gravatar image

I'm trying to find the length of an array of geometry_msg:Point passed to a service handle, so I can iterate over each Point, apply a transform and then push onto a response. when I use sizeof(req.points) - I get "24" regardless of the number of points actually in the message.


geometry_msgs/Point[] points
geometry_msgs/Point[] points

The callback:

static bool pointsCallback(image_transform::TransformPoints::Request &req,
                           image_transform::TransformPoints::Response &res) {

      int no_points = sizeof(req.points);
      int point_size = sizeof(geometry_msgs::Point);
      std::cout << no_points << std::endl;
      std::cout << point_size << std::endl;
      std::cout << req.points[0] << std::endl;
      std::cout << req.points[1] << std::endl;

Will output something like this:

24   # sizeof(req.points)
24   # sizeof(geometry_msgs::Point);
x: 0
y: 1
z: 0

x: 1
y: 2
z: 0
edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2020-08-06 07:56:36 -0500

Weasfas gravatar image

Hi @LukeAI,

What you want is not sizeof that returns the size in bytes of the object but size function that returns the size of the vector. So in your case, since the points msgs instances are std vectors, it would be something like:

auto req_size = req.points.size();


edit flag offensive delete link more



I would perhaps even suggest using a range-based for-loop instead.

gvdhoorn gravatar image gvdhoorn  ( 2020-08-06 08:25:27 -0500 )edit

Weasfas answered my question, as stated. but using the range-based for-loop is what I really wanted. I didn't realise the an array in a msg or srv was a std vector.

LukeAI gravatar image LukeAI  ( 2020-08-06 08:43:41 -0500 )edit
gvdhoorn gravatar image gvdhoorn  ( 2020-08-06 08:46:34 -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

1 follower


Asked: 2020-08-06 06:39:17 -0500

Seen: 615 times

Last updated: Aug 06 '20