pcl_ros: Use vec.data() instead of &vec[0] (#348)

Update pcl_ros to use the data() member function of STL containers to
get the pointer to the underlying storage, instead of dereferencing the
zeroth element and taking its reference.  When a container is of size 0,
dereferencing element 0 with operator[]() is undefined behavior, and
will trigger assertions when features like _GLIBCXX_ASSERTIONS are
enabled.

Fixes #333.
This commit is contained in:
Rich Mattes 2021-09-27 17:28:26 -04:00 committed by GitHub
parent 834eb111e8
commit 755f566356
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 3 additions and 3 deletions

View File

@ -255,7 +255,7 @@ struct Serializer<pcl::PointCloud<T>>
stream.next(row_step); stream.next(row_step);
uint32_t data_size = row_step * height; uint32_t data_size = row_step * height;
stream.next(data_size); stream.next(data_size);
memcpy(stream.advance(data_size), &m.points[0], data_size); memcpy(stream.advance(data_size), m.points.data(), data_size);
uint8_t is_dense = m.is_dense; uint8_t is_dense = m.is_dense;
stream.next(is_dense); stream.next(is_dense);
@ -296,7 +296,7 @@ struct Serializer<pcl::PointCloud<T>>
stream.next(data_size); stream.next(data_size);
assert(data_size == m.height * m.width * point_step); assert(data_size == m.height * m.width * point_step);
m.points.resize(m.height * m.width); m.points.resize(m.height * m.width);
uint8_t * m_data = reinterpret_cast<uint8_t *>(&m.points[0]); uint8_t * m_data = reinterpret_cast<uint8_t *>(m.points.data());
// If the data layouts match, can copy a whole row in one memcpy // If the data layouts match, can copy a whole row in one memcpy
if (mapping.size() == 1 && if (mapping.size() == 1 &&
mapping[0].serialized_offset == 0 && mapping[0].serialized_offset == 0 &&

View File

@ -166,7 +166,7 @@ transformPointCloud(
out.is_dense = in.is_dense; out.is_dense = in.is_dense;
out.data.resize(in.data.size()); out.data.resize(in.data.size());
// Copy everything as it's faster than copying individual elements // Copy everything as it's faster than copying individual elements
memcpy(&out.data[0], &in.data[0], in.data.size()); memcpy(out.data.data(), in.data.data(), in.data.size());
} }
Eigen::Array4i xyz_offset(in.fields[x_idx].offset, in.fields[y_idx].offset, Eigen::Array4i xyz_offset(in.fields[x_idx].offset, in.fields[y_idx].offset,