i have one example with dynamic allocating 2-d arrays. The this generic function must return a pointer on pointer. Type is type of element in 2-d matrix given by the parameter. 2-d matrix can be deque, vector, but must support size(). I have problem with segmentation. It fails when i for example send this:
std::vector<std::deque<double>> m {{1,2,3}, {1,2,3}, {1,2,3}};
It display segmentation failed that means that i enter part of memory which is not allocated. But when i try to set this to extra try{}catch(){} construction it did'nt catch any exception.
I am glad that problem is well defined.
I use continuous memoory allocation, and with one for loop I set pointers to imitate 2-d arrays that can be used
1 2 3 4 5
for(int i = 0; i < n; i++) // rows
{
for(int j = 0; j < m; j++) //columns
// something
}
Please use code tags. Edit your post, highlight the code, and then click the <> button to the right of the edit window.
1 2 3
template < typename tip > tip ** Function(tip a) {
return a;
}
First of all, why?? If Function(a) returns a, then why have the function at all?
Second, you declare the function as returning a tip** but you actually return a tip. That's never going to work. What are you trying to accomplish here?
Function() doesn't need a body (in fact, it is never instantiated because it wouldn't compile)
it's just there for the decltype crap, to avoid writing **
can't reproduce your crash
provide a testcase, and the debug backtrace
may also want to run through valgrind to check for invalid access
by the way, Dynamic [0] = nullptr; would be invalid if the matrix is empty
Your problem is somewhere else. You're probably corrupting the heap somewhere and the problem doesn't show up until you execute this code. This works fine for me: