Subject: CGAL users discussion list
List archive
- From: Alessandro Attanasi <>
- To: Philipp Moeller <>
- Cc:
- Subject: Re: [cgal-discuss] lower_hull_2() problem
- Date: Thu, 16 Jan 2014 21:23:21 +0100
// definition of the input points
K::Point_2 p1(2,5);
K::Point_2 p2(0,4);
K::Point_2 p3(0,0);
K::Point_2 p4(4,0);
K::Point_2 p5(4,4);
K::Point_2 p6(1,3);
K::Point_2 p7(1,1);
K::Point_2 p8(3,1);
K::Point_2 p9(3,3);
K::Point_2 p10(2,2);
int totPoints = 10;
// array storing input points
K::Point_2 *points = NULL;
points = (K::Point_2*)malloc(totPoints * sizeof(K::Point_2));
points[0] = p1;
points[1] = p2;
points[2] = p3;
points[3] = p4;
points[4] = p5;
points[5] = p6;
points[6] = p7;
points[7] = p8;
points[8] = p9;
points[9] = p10;
// array storing results
K::Point_2 *result = NULL;
result = (K::Point_2*)malloc(totPoints * sizeof(K::Point_2));
ptr = CGAL::ch_akl_toussaint( points, points+totPoints, result );
// see how many points are into the convex hull
int ch_points = (ptr - result);
// print out the number of points of the convex hull and the points themselves
std::cout << "1) Points on the convex hull = " << ch_points << std::endl;
for(int i =0;i<ch_points;i++)
{
std::cout << result[i] << std::endl;
}
free(points);
points = NULL;
free(result);
result = NULL;
Alessandro Attanasi <> writes:It was working because you were using std::vector with a back_inserter
> Ok, but why that procedure was working using all convex_hull
> functions?
which takes care of those things. At least this is what your code
did. There can be situations where using a malloc'd pointer as an
OutputIterator can work (with a POD-type assignment effectively becomes
a memcpy, IIRC this is still undefined behavior though).
Which is exactly the wrong thing to do because it won't call the
> I started from the cgal example
> http://doc.cgal.org/latest/Convex_hull_2/Convex_hull_2_2array_convex_hull_2_8cpp-example.html
>
> I freed the malloc at the end :)
destructors of the elements.
>
>
>
> On 15 January 2014 23:31, Philipp Moeller <
> > wrote:
>
>> Alessandro Attanasi <> writes:
>>
>> > Sorry, really stupid error :P. It works as expected!
>> >
>> > Just last question about this problem: if I want to use C pointers
>> instead
>> > of stl iterators, why this code is wrong? It gives to me zero points on
>> the
>> > lower hull (I tried to use the same logic of the example for convex hull
>> > computation)
>> >
>> > K::Point_2 *ptr2;
>> >
>> > K::Point_2 *result2 = NULL;
>> >
>> > result2 = (K::Point_2*)malloc(totPoints * sizeof(K::Point_2));
>> >
>> > ptr2 = CGAL::lower_hull_points_2( points, points+totPoints, result2 );
>>
>> Why would you ever call malloc instead of new in such a situation? It is
>> exactly where your problem comes from. You cannot simply use raw
>> uninitialized memory as an OutputIterator. You should wrap the pointer
>> into a raw_storage_iterator provided by the standard header memory. [1]
>>
>> You also need to take special care when releasing the memory (call the
>> destructor explicitly).
>>
>> >
>> >
>> > ch_points = (ptr2 - result2);
>> >
>> > std::cout << "6) Points on the lower hull = " << ch_points <<
>> std::endl;
>> >
>> > for(int i =0;i<ch_points;i++)
>> >
>> > {
>> >
>> > std::cout << result2[i] << std::endl;
>> >
>> > }
>>
>> Footnotes:
>> [1] http://en.cppreference.com/w/cpp/memory/raw_storage_iterator
- [cgal-discuss] lower_hull_2() problem, Alessandro Attanasi, 01/14/2014
- Re: [cgal-discuss] lower_hull_2() problem, Philipp Moeller, 01/15/2014
- Re: [cgal-discuss] lower_hull_2() problem, Alessandro Attanasi, 01/15/2014
- Re: [cgal-discuss] lower_hull_2() problem, Philipp Moeller, 01/15/2014
- Re: [cgal-discuss] lower_hull_2() problem, Alessandro Attanasi, 01/15/2014
- Re: [cgal-discuss] lower_hull_2() problem, Philipp Moeller, 01/15/2014
- Re: [cgal-discuss] lower_hull_2() problem, Alessandro Attanasi, 01/15/2014
- Re: [cgal-discuss] lower_hull_2() problem, Philipp Moeller, 01/16/2014
- Re: [cgal-discuss] lower_hull_2() problem, Alessandro Attanasi, 01/16/2014
- Re: [cgal-discuss] lower_hull_2() problem, Philipp Moeller, 01/16/2014
- Re: [cgal-discuss] lower_hull_2() problem, Alessandro Attanasi, 01/17/2014
- Re: [cgal-discuss] lower_hull_2() problem, Alessandro Attanasi, 01/15/2014
- Re: [cgal-discuss] lower_hull_2() problem, Philipp Moeller, 01/15/2014
- Re: [cgal-discuss] lower_hull_2() problem, Alessandro Attanasi, 01/15/2014
- Re: [cgal-discuss] lower_hull_2() problem, Philipp Moeller, 01/15/2014
- Re: [cgal-discuss] lower_hull_2() problem, Alessandro Attanasi, 01/15/2014
- Re: [cgal-discuss] lower_hull_2() problem, Philipp Moeller, 01/15/2014
Archive powered by MHonArc 2.6.18.