Ask Your Question

Determine runtime array length in arduino IDE

asked 2017-06-28 11:44:45 -0500

Felix_N gravatar image

Hello everyone,

I am currently trying to get my arduino ROS node to work. I programmed an tested the whole code using the catkin workspace.

I now copied the code into the arduino IDE and suddenly a lot of functions, which previously worked just fine throw errors. I already fixed all but one: I need to determine an array length for further data processing. The code I used until now is:

int wp_size = msg.goal.trajectory.joint_trajectory.points.size();

Another method of finding out an arrays length seems to be:

int wp_size = sizeof(  msg.goal.trajectory.joint_trajectory.points ) / sizeof( double );

This does not work, because the array is empty at compile time and thus the code above returns "1" every time.

The length of the array changes with every service call. Is there any other method to find out the length of the array?

Maybe the error message might help someone:

error: request for member ‘size’ in ‘msg.moveit_msgs::ExecuteTrajectoryActionGoal::goal.moveit_msgs::ExecuteTrajectoryGoal::trajectory.moveit_msgs::RobotTrajectory::joint_trajectory.trajectory_msgs::JointTrajectory::points’, which is of pointer type ‘trajectory_msgs::JointTrajectory::_points_type* const {aka trajectory_msgs::JointTrajectoryPoint* const}’ (maybe you meant to use ‘->’ ?)

If I use "->" though, I get:

 error: ‘trajectory_msgs::JointTrajectory::_points_type’ has no member named ‘size’

Thank you in advance,


edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2017-06-29 01:48:29 -0500

gvdhoorn gravatar image

updated 2017-06-29 01:57:42 -0500

From your references to size(), I get the feeling that you've been converting 'regular' roscpp based code to rosserial. You might already have realised it, but roscpp == C++, while rosserial is C only. size() is a method on a std::vector. rosserial only uses raw pointers as arrays and those don't have any intrinsic size property that you could query. The sizeof(..) trick would work, if those arrays had a static size, but they don't, as you found out.

The only way -- that I know of -- to know the size of an array for a message that you populate is to keep track of what you pass to malloc(..) to init those arrays and use that.

For messages that you receive things are slightly different. See wiki/rosserial/Overview - Limitations - Arrays for some that.

And cccording to wiki/rosserial/Overview/Messages:

Also be aware that the arrays in the messages are not defined as vector objects. Thus you have to predefine the array and then pass it as pointer to the message. To determine the end of the array each one has an auto-generated integer companion with the same name and the suffix _length.

edit flag offensive delete link more


Well.... No i did not realize that. And I wondered why my c++ functions suddenly do not work anymore...... In my project I can luckily do the .size() job on the pc and pass the processed data to the arduino as well. Thank you very much for your time again @gvdhoorn!!

Felix_N gravatar image Felix_N  ( 2017-06-29 07:54:08 -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: 2017-06-28 11:44:45 -0500

Seen: 659 times

Last updated: Jun 29 '17