title: Point Cloud Library (PCL) - rule of thumb for when ::Ptr should be used when declaring a point cloud?
body: <p>As I'm getting into Point Cloud Library (PCL) I'm encountering some confusion on when to declare point clouds with <code>::Ptr</code> verses without this.</p>
  
  <p>It seems to work either way, for example both of these programs compile, run, and product the expected results (writing a random 20 point cloud to file):</p>
  
  <pre><code>// WriteCloud.cpp
  
  #include &lt;iostream&gt;
  
  #include &lt;pcl/common/common_headers.h&gt;
  #include &lt;pcl/io/pcd_io.h&gt;
  
  int main(void)
  {
    std::cout &lt;&lt; "\n\n" &lt;&lt; "starting program" &lt;&lt; "\n\n";
  
    pcl::PointCloud&lt;pcl::PointXYZ&gt; cloud;    // !!! without ::Ptr !!!
  
    int numPoints = 20;
  
    for (int i = 0; i &lt; numPoints; i++)
    {
      pcl::PointXYZ point;
  
      point.x = 1024 * rand() / (RAND_MAX + 1.0f);
      point.y = 1024 * rand() / (RAND_MAX + 1.0f);
      point.z = 1024 * rand() / (RAND_MAX + 1.0f);
  
      cloud.points.push_back(point);
    }
  
    // for simplicity, use an "unorganized" cloud, "width" = num points, "height" = 1
    cloud.width = (int)cloud.points.size();
    cloud.height = 1;
  
    pcl::io::savePCDFileASCII("my_cloud.pcd", cloud);
  
    std::cout &lt;&lt; "\n\n" &lt;&lt; "program complete" &lt;&lt; "\n\n";
  
    return (0);
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>// WriteCloudPtr.cpp
  
  #include &lt;iostream&gt;
  
  #include &lt;pcl/common/common_headers.h&gt;
  #include &lt;pcl/io/pcd_io.h&gt;
  
  int main(void)
  {
    std::cout &lt;&lt; "\n\n" &lt;&lt; "starting program" &lt;&lt; "\n\n";
  
    pcl::PointCloud&lt;pcl::PointXYZ&gt;::Ptr cloud(new pcl::PointCloud&lt;pcl::PointXYZ&gt;);      // !!! with ::Ptr !!!
  
    int numPoints = 20;
  
    for (int i = 0; i &lt; numPoints; i++)
    {
      pcl::PointXYZ point;
  
      point.x = 1024 * rand() / (RAND_MAX + 1.0f);
      point.y = 1024 * rand() / (RAND_MAX + 1.0f);
      point.z = 1024 * rand() / (RAND_MAX + 1.0f);
  
      cloud-&gt;points.push_back(point);
    }
  
    // for simplicity, use an "unorganized" cloud, "width" = num points, "height" = 1
    cloud-&gt;width = (int)cloud-&gt;points.size();
    cloud-&gt;height = 1;
  
    pcl::io::savePCDFileASCII("my_cloud.pcd", *cloud);
  
    std::cout &lt;&lt; "\n\n" &lt;&lt; "program complete" &lt;&lt; "\n\n";
  
    return (0);
  }
  </code></pre>
  
  <p>Is there a rule of thumb as to which should be used?  More specifically, here are some questions I have:</p>
  
  <p>-In all of the PCL GitHub examples <a href="https://github.com/PointCloudLibrary/pcl/tree/master/examples" rel="nofollow noreferrer">https://github.com/PointCloudLibrary/pcl/tree/master/examples</a> the programs use <code>::Ptr</code>, and on the website most (but not all) of the examples use <code>::Ptr</code>.  Also, for the most part, the examples I've found not using <code>::Ptr</code> seem to be very old.  Based on this more than anything I'm under the impression that <code>::Ptr</code> is generally regarded as the current PCL standard, is this correct?</p>
  
  <p>-The non-<code>::Ptr</code> way seems simpler, is there any disadvantage to the non-<code>::Ptr</code> way that is not apparent?  Before somebody says "passing these into a function would make a copy of the cloud and therefore be inefficient" I would pass by reference so as to not make copies unnecessarily.</p>
  
  <p>-Clearly <code>::Ptr</code> is a typedef (essentially a renaming) of <code>boost::shared_ptr</code>.  Still, it makes me uncomfortable to use pointers when not necessary as funny things can happen in odd situations.  For example the accepted answer to this post <a href="https://stackoverflow.com/questions/10644429/create-a-pclpointcloudptr-from-a-pclpointcloud">Create a pcl::PointCloud::Ptr from a pcl::PointCloud</a> mentions an odd conversion situation which could seem to cause a crash.  Are there any risks with using the <code>::Ptr</code> way in certain situations that PCL users should be aware of?</p>