returning ArrayView in a function

16 views
Skip to first unread message

Laryssa Abdala

unread,
Nov 25, 2022, 11:43:52 PM11/25/22
to dea...@googlegroups.com
Hi, 

I am trying to create a function that returns an ArrayView to a Tensor, like this:
const ArrayView<const Tensor<1, spacedim>> function{
return make_array_view(table, row, start_col, size_view;
}
This gives me a segmentation fault. 

I looked through some of the ArrayView tests and at the function get_properties() defined on line 53 of file property_pool.cc, which helped me understand the pointer-like-nature of ArrayView. However, none of them seem to allocate dynamic memory for the ArrayView object before returning it. Should I allocate dynamic memory to avoid the segfault? If so, how?

Thanks, 
Laryssa

Wolfgang Bangerth

unread,
Nov 26, 2022, 12:04:12 AM11/26/22
to dea...@googlegroups.com
On 11/25/22 21:43, Laryssa Abdala wrote:
>
> I am trying to create a function that returns an ArrayView to a Tensor, like this:
> constArrayView<constTensor<1, spacedim>> function{
> return make_array_view(table, row, start_col, size_view;
> }
> This gives me a segmentation fault.
>
> I looked through some of the ArrayView tests and at the function
> get_properties() defined on line 53
> <https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fwww.dealii.org%2F9.2.0%2Fdoxygen%2Fdeal.II%2Fproperty__pool_8cc_source.html%23l00053&data=05%7C01%7CWolfgang.Bangerth%40colostate.edu%7C07cbb55748af4f7dcbef08dacf68d26a%7Cafb58802ff7a4bb1ab21367ff2ecfc8b%7C0%7C0%7C638050346380857335%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=ST3HnEXnZ4uXWSy342c5BlBzq039QDmtYs7mh46rIZM%3D&reserved=0> of file property_pool.cc <https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fwww.dealii.org%2F9.2.0%2Fdoxygen%2Fdeal.II%2Fproperty__pool_8cc_source.html&data=05%7C01%7CWolfgang.Bangerth%40colostate.edu%7C07cbb55748af4f7dcbef08dacf68d26a%7Cafb58802ff7a4bb1ab21367ff2ecfc8b%7C0%7C0%7C638050346380857335%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=UZ3%2FkbauY%2FRhaYsqnSGyvngQCneBxPupg0u8Dg2UEz0%3D&reserved=0>, which helped me understand the pointer-like-nature of ArrayView. However, none of them seem to allocate dynamic memory for the ArrayView object before returning it. Should I allocate dynamic memory to avoid the segfault? If so, how?
>

Laryssa,
start by explaining *what* it is you are trying to achieve, and then we can
see *how* to do it.

ArrayView only presents a view on an existing array that lives somewhere in
memory. But as long as you want to access that view, the underlying array must
continue to exist or you will get errors such as what you see. As a
consequence, a function must not return an ArrayView to an object that lives
only in that function (i.e., a local variable). I suspect that is what is
happening in your case, but since I don't know the code, I cannot say for
sure. As for how you should allocate the memory -- that comes back to *what*
you want to do, which I also don't know :-)

Best
W.

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


Reply all
Reply to author
Forward
0 new messages