# Unable to cast from double\* to vtkSmartPointer\<double\>

**URL:** https://discourse.vtk.org/t/unable-to-cast-from-double-to-vtksmartpointer-double/6723
**Category:** Support
**Created:** [September 29, 2021, 8:59am UTC](https://discourse.vtk.org/t/unable-to-cast-from-double-to-vtksmartpointer-double/6723 "2021-09-29T08:59:34Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![huaweiping](https://discourse.vtk.org/user_avatar/discourse.vtk.org/huaweiping/32/8231_2.png) [@huaweiping](https://discourse.vtk.org/u/huaweiping)
#### Post date: [September 29, 2021, 8:59am UTC](https://discourse.vtk.org/t/unable-to-cast-from-double-to-vtksmartpointer-double/6723/1 "2021-09-29T08:59:34Z")

</div>

Hi,

Today I find a problem about casting a raw pointer to a SmartPointer. I’m actually randomly facing a segmentation fault when just simply allocating vector (which is very rare I guess) and I can’t figure out the reason. Then I decided to abandon all raw pointers in my code.

The current code is used to read the data from vtkDataset:

```auto
// The variable has 6 different data like velocity, salinity, etc.

vtkNew<vtkDataset> in_ds;
double* boxPoint_current_data = new double[6];

// I use memcpy() here because I find the program would crash sometimes if I use operaor"="
memcpy(boxPoint_current_data, in_ds->GetPointData()->GetTensors()->GetTuple(boxPoint_current_index),sizeof(double)*6);

// calculate arctan here
angle_current = atan2(boxPoint_current_data[2],boxPoint_current_data[1]);

```

I tried multiple ways from unique\_ptr to vtkSmartPointer/vtkNew but none of them would work and try to get the return of GetTuple.

```auto
vtkSmartPointer<double>boxPoint_current_data;
boxPoint_current_data = in_ds->GetPointData()->GetTensors()->GetTuple(boxPoint_current_index);

```

The biggest problem is that GetTuple return a double\* while there is not a valid conversion from double\* to vtkSmartPointer.

If I use unique\_ptr, I have to define the variable either  
`auto boxPoint_current_data = make_unique<double>(6)`  
or  
`auto boxPoint_current_data = make_unique<double[]>(6)`

If I use the first one, I think it would be difficult to access the boxPoint\_current\_data (honestly I don’t know how to do it for now). The latter one would meet a problem that unable to cast from double\* to double[].

I would be appreciate if someone could help me sort it out.

---

<div class="post-metadata">

### Author: ![codeling](https://discourse.vtk.org/user_avatar/discourse.vtk.org/codeling/32/3792_2.png) [@codeling](https://discourse.vtk.org/u/codeling)
#### Post date: [September 29, 2021, 12:34pm UTC](https://discourse.vtk.org/t/unable-to-cast-from-double-to-vtksmartpointer-double/6723/2 "2021-09-29T12:34:33Z")

</div>

vtkSmartPointer is only useful/usable for [vtkObject](https://vtk.org/doc/nightly/html/classvtkObject.html#details)-derived classes as far as I can tell - vtkSmartPointer helps with the reference counting done by the vtkObject.

What’s your intention behind trying to create a smart pointer to a double array? Do you intend to store the values and want them to be automatically be deleted at a later point?  
Just for accessing the tuple values, this should be enough:

```auto
double const * values = in_ds->GetPointData()->GetTensors()->GetTuple(boxPoint_current_index);

```

Of course you need to take care that you don’t access values after in\_ds has been deleted; if you require to hold on to the values, you can use [GetTuple’s second variant](https://vtk.org/doc/nightly/html/classvtkDataArray.html#aaa831fe9346616024faf04d644cd5f14), e.g. like this:

```auto
double * values = new double[6];
in_ds->GetPointData()->GetTensors()->GetTuple(boxPoint_current_index, values);

```

---

<div class="post-metadata">

### Author: ![huaweiping](https://discourse.vtk.org/user_avatar/discourse.vtk.org/huaweiping/32/8231_2.png) [@huaweiping](https://discourse.vtk.org/u/huaweiping)
#### Post date: [October 5, 2021, 4:00am UTC](https://discourse.vtk.org/t/unable-to-cast-from-double-to-vtksmartpointer-double/6723/3 "2021-10-05T04:00:37Z")

</div>

Yeah, this is what I’m exactly doing right now.

However, I really want to use the smart pointer instead of those raw pointer in my code, which could possibly help me avoid the segmentation fault. Do you have any idea about that?

---

<div class="post-metadata">

### Author: ![codeling](https://discourse.vtk.org/user_avatar/discourse.vtk.org/codeling/32/3792_2.png) [@codeling](https://discourse.vtk.org/u/codeling)
#### Post date: [October 5, 2021, 6:35am UTC](https://discourse.vtk.org/t/unable-to-cast-from-double-to-vtksmartpointer-double/6723/4 "2021-10-05T06:35:18Z")

</div>

In case you are using C++17, you can use a `std::shared_ptr` to a `double []` (see [this stackoverflow answer](https://stackoverflow.com/a/13062069).

Otherwise, you can use an `std::vector`, see [for example here](https://stackoverflow.com/a/10865922). You’d have to `resize` the vector to the proper size, and then pass in the address of the first element, something like this:

```
std::vector t(6); // or `std::vector t; t.resize(6);
...->GetTensors(&t[0]);
```

---

<div class="post-metadata">

### Author: ![huaweiping](https://discourse.vtk.org/user_avatar/discourse.vtk.org/huaweiping/32/8231_2.png) [@huaweiping](https://discourse.vtk.org/u/huaweiping)
#### Post date: [October 5, 2021, 10:15am UTC](https://discourse.vtk.org/t/unable-to-cast-from-double-to-vtksmartpointer-double/6723/5 "2021-10-05T10:15:37Z")

</div>

I tried the shared\_ptr before but it has the same problem with unique\_ptr, which is:

`auto boxPoint_current_data = make_unique<double[]>(6)`

> The latter one would meet a problem that unable to cast from double\* to double.

On the other hand, the vector seems to be a good choice for this problem. Thanks for your help!

---

<div class="post-metadata">

### Author: ![codeling](https://discourse.vtk.org/user_avatar/discourse.vtk.org/codeling/32/3792_2.png) [@codeling](https://discourse.vtk.org/u/codeling)
#### Post date: [October 5, 2021, 10:23am UTC](https://discourse.vtk.org/t/unable-to-cast-from-double-to-vtksmartpointer-double/6723/6 "2021-10-05T10:23:50Z")

</div>

> [@huaweiping](#):
>
> `auto boxPoint_current_data = make_unique<double[]>(6)`
> 
> > The latter one would meet a problem that unable to cast from double\* to double.

You don’t show how you would use the unique/shared\_ptr to get the “unable to cast” problem; it should work, provided it is used correctly, something like

```
...->GetTuple(boxPoint_current_data.get());

```

(untested).

---

<div class="post-metadata">

### Author: ![huaweiping](https://discourse.vtk.org/user_avatar/discourse.vtk.org/huaweiping/32/8231_2.png) [@huaweiping](https://discourse.vtk.org/u/huaweiping)
#### Post date: [October 5, 2021, 10:36am UTC](https://discourse.vtk.org/t/unable-to-cast-from-double-to-vtksmartpointer-double/6723/7 "2021-10-05T10:36:07Z")

</div>

I used the shared\_ptr in c++ 17 like this first:

```auto
.....
std::shared_ptr<double[]> boxPoint_current_data;
......
boxPoint_current_data = in_ds->GetPointData()->GetTensors()->GetTuple(boxPoint_current_index);

```

And this would gives me the problem about unable to cast.

Then I realize that I need the address of the pointer so I tried this one and get a lvalue problem.  
`boxPoint_current_data = in_ds->GetPointData()->GetTensors()->GetTuple(boxPoint_current_index);`

After I check your code I find that I could use the your version and it works well. Thanks so much!
