KDTree question

27 views
Skip to first unread message

A.Z Ihsan

unread,
Jan 14, 2020, 7:08:32 AM1/14/20
to deal.II User Group
Hi,

i have question regarding the kdtree class in deal ii, though it has become a deprecated class, but for my particular problem it might be helpful.
suppose i have a class

template<int dim>

class A
{
public:
    A
();
   
private:
    KDTree<dim> kdtree;
};



the question is how we can get a get function to the return the private kdtree, e.g. A::get_kdtree() ? i still could not figure out what is the type of the KDTree object.

BR,
Ihsan

A.Z Ihsan

unread,
Jan 14, 2020, 9:35:22 AM1/14/20
to deal.II User Group
to be precise
template<int dim>
class A
{
public:

    A
(std::vector<Point<dim>> &points);
   
const KDTree<dim> & get_kdtree() const;
   
double doing_something();
   
private:
    KDTree<dim> kdtree;
};

and in my implementation i wrote
template<int dim>
A
<dim>::A(std::vector<Point<dim>> &points)
{
    kdtree
.set_points(points);
}

template <int dim>
const KDTree<dim> &
A
<dim>::get_kdtree() const
{
   
return kdtree;
}

template <int dim>
double
A
<dim>::doing_something()
{
   
const auto& _v_kdtree = this->get_kdtree();
   
....
}


But everytime i called the get_kdtree function, i got the segmentation fault( you may see the snippet in doing_something() function. Any idea to resolve this problem?

Wolfgang Bangerth

unread,
Jan 14, 2020, 7:49:38 PM1/14/20
to dea...@googlegroups.com

> |
> |template<intdim>
> classA
> {
> public:
>     A(std::vector<Point<dim>>&points);
> constKDTree<dim>&get_kdtree()const;
> doubledoing_something();
>
> private:
> KDTree<dim>kdtree;
> };
> |
> |
>
> and in my implementation i wrote
> |
> template<intdim>
> A<dim>::A(std::vector<Point<dim>>&points)
> {
>     kdtree.set_points(points);
> }
>
> template<intdim>
> constKDTree<dim>&
> A<dim>::get_kdtree()const
> {
> returnkdtree;
> }
>
> template<intdim>
> double
> A<dim>::doing_something()
> {
> constauto&_v_kdtree =this->get_kdtree();
> ....
> }
>
> |

Thus looks right to me.


> But everytime i called the get_kdtree function, i got the segmentation fault(
> you may see the snippet in doing_something() function. Any idea to resolve
> this problem?

We just had another thread on the mailing list about segmentation faults and
how one debugs them. I don't think that your get_kdtree() function is wrong.
Something must be wrong in how you set up the object that contains it. You
will need to find out which pointer it is that creates the segmentation fault;
if you know this part, it is typically easy to understand what is happening.

My suspicion is that in your case, the 'this' pointer is a nullptr.

Best
W.

--
------------------------------------------------------------------------
Wolfgang Bangerth email: bang...@colostate.edu
www: http://www.math.colostate.edu/~bangerth/

A.Z Ihsan

unread,
Jan 16, 2020, 7:30:23 AM1/16/20
to deal.II User Group
Dear Wolfgang,

yes you are right, i did some mistakes in the initialization step and it ends up the pointer taking the kdtree is nullptr.

thank you.

BR,
Ihsan

On Tuesday, January 14, 2020 at 1:08:32 PM UTC+1, A.Z Ihsan wrote:
Reply all
Reply to author
Forward
0 new messages