I have an array of CameraSpacePoint in C# programming language on Windows 10. The CameraSpacePoint
have three float variables, and defined inside Microsoft SDK as follows:
using System;
using System.Diagnostics;
namespace Microsoft.Kinect
public struct CameraSpacePoint : IEquatable<CameraSpacePoint>
{
public float X;
public float Y;
public float Z;
public bool Equals(CameraSpacePoint point);
public sealed override bool Equals(object obj);
public sealed override int GetHashCode();
public static bool operator ==(CameraSpacePoint a, CameraSpacePoint b);
public static bool operator !=(CameraSpacePoint a, CameraSpacePoint b);
}
}
I have serialized the array of CameraSpacePoint
in C# by following this suggestion.
private int data_width = 512;
private int data_height = 424;
private int data_size = data_width * data_height; // 217088
var cameraSpacePoints = new CameraSpacePoint[data_size];
// fill the values in cameraSpacePoints here
var bytes = ObjectToByteArray(cameraSpacePoints);
Console.WriteLine(bytes.Length); //prints 4559058
The converted byte array is then transferred to Ubuntu.
In order to deserialize the byte array, first I designed a similar structure in C++ (Ubuntu) as follow:
struct CameraSpacePoint
{
float X;
float Y;
float Z;
};
Now I am using memcpy(&cameraSpacePoints, data_buffer, data_size_bytes)
to deserialize but it gives segmentation fault
. I highly doubt that it is because of mismatching of array size in C++.
Below is the code snippet:
#include <ros/ros.h>
#include <boost/asio.hpp>
using boost::asio::ip::tcp;
constexpr size_t data_width = 512;
constexpr size_t data_height = 424;
constexpr size_t data_size = data_width * data_height;
constexpr size_t data_size_bytes = data_size * 3 * sizeof(float); // each point have three float variables i.e., X, Y and Z
unsigned char data_buffer[data_size_bytes];
CameraSpacePoint cameraSpacePoints[data_size];
boost::asio::io_service io_service;
tcp::resolver resolver(io_service);
tcp::resolver::iterator endpoint_iterator = resolver.resolve({server_host, server_port});
tcp::socket socket(io_service);
try
{
boost::asio::connect(socket, endpoint_iterator);
}
catch (boost::system::system_error const& e)
{
std::cout << "Failed to connect to server "<< server_host.c_str() <<":"<< server_port.c_str() << e.what() << std::endl;
return -1;
}
while(ros::ok())
{
boost::asio::read(socket, boost::asio::buffer(data_buffer, data_size_bytes));
memcpy(&cameraSpacePoints, data_buffer, data_size_bytes);
//CameraSpacePoint midPoint = cameraSpacePoints[data_size/2];
//std::cout << ":{" << midPoint.X << "," << midPoint.Y << "," << midPoint.Z << "}" << std::endl;
std::cout << "Data Received" << std::endl;
boost::asio::write(socket, boost::asio::buffer("OK\n"));
ros::spinOnce();
}
Please note that, if I comment memcpy
command, Data Received
is being printed on the terminal. But with memcpy
, the program stops by throwing segmentation fault error.