Problem with fragmentation(Dynamic matrix Allocation)

Hi,

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 crash when:

Dynamic [0] = new decltype(Matrix.at(0).at(0)+ Matrix.at(0).at(0))[size];

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
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
template<typename tip>
tip** Function(tip a)
{
    return a;
}
template<typename Container>
auto MakeDynamicCopy2D(Container &Matrix) -> decltype(Matrix.at(0).at(0) + Matrix.at(0).at(0))**
{
	decltype(Function(Matrica.at(0).at(0) + Matrix.at(0).at(0)))Dynamic = nullptr;
	 try {
		
     Dynamic = new decltype(Matrix.at(0).at(0)+ Matrix.at(0).at(0))*[Matrix.size()];
	 }
     catch(std::bad_alloc) {
         delete [] Dynamic; 
         throw;
     }
	
	try {
     int size = 0;
     int k = 0;
     while (k < Matrica.size()) size+=Matrix.at(k++).size();
     Dynamic [0] = nullptr;
     Dynamic [0] = new decltype(Matrix.at(0).at(0)+ Matrix.at(0).at(0))[size];

	for(int i = 1; i < Matrix.size();i++) {
	  Dynamic [i] = Dynamic [i-1] + Matrix.at(i-1).size();
	}
	for(int i = 0; i < Matrix.size();i++)
	{
		for(int j = 0; j < Matrix.at(i).size();j++)
		Dynamic [i][j] = Matrix.at(i).at(j);
	}
	}
	catch(std::bad_alloc) {
	    if(Dynamic ) delete[] Dynamic [0];
	    delete[] Dynamic ;
	} 
	return Dynamic ;	
}


Thanks forward. Greetings
Last edited on
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?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
template < typename Container >
auto MakeDynamicCopy2D(Container & Matrix)->
    decltype(Matrix.at(0).at(0)+Matrix.at(0).at(0)) **
{
    typedef decltype(Matrix.at(0).at(0)+Matrix.at(0).at(0)) BaseType;
    BaseType **Dynamic = new BaseType *[Matrix.size()];

    int size = 0;
    for (unsigned k = 0;k < Matrix.size(); ++k) {
        size += Matrix.at(k).size();
    }
    Dynamic[0] = new BaseType[size];

    for (unsigned i = 1; i < Matrix.size(); i++) {
        Dynamic[i] = Dynamic[i - 1] + Matrix.at(i - 1).size();
    }
    for (unsigned i = 0; i < Matrix.size(); i++) {
        for (unsigned j = 0; j < Matrix.at(i).size(); j++)
            Dynamic[i][j] = Matrix.at(i).at(j);
    }
    return Dynamic;
}


Main function is function named MakeDynamicCopy2D. I use ' Function' to detect type of variable. It can be without it.


Problem is in MakeDynamicCopy2D. I get segmentation failed when i send

std::vector<std::deque<double>> m {{1,2,3}, {1,2,3}, {1,2,3}};

On this line:

Dynamic [0] = new decltype(Matrix.at(0).at(0)+ Matrix.at(0).at(0))[size];

It didn't catch any exception. But i have warning memory error and memory leak.
Last edited on
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
What does this have to do with fragmentation?
l'I1 blame bad translation
segmentation fault, memory segment, memory segmentation, fragmentation
Last edited on
I have found solution. Ne555 thanks you
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <vector>
#include <deque>
#include <iostream>

using std::vector;
using std::deque;
using std::cout;

template < typename Container >
auto MakeDynamicCopy2D(Container & Matrix)->
    decltype(Matrix.at(0).at(0)+Matrix.at(0).at(0)) **
{
    typedef decltype(Matrix.at(0).at(0)+Matrix.at(0).at(0)) BaseType;
    BaseType **Dynamic = new BaseType *[Matrix.size()];

    int size = 0;
    for (unsigned k = 0;k < Matrix.size(); ++k) {
	size += Matrix.at(k).size();
    }
    Dynamic[0] = new BaseType[size];

    for (unsigned i = 1; i < Matrix.size(); i++) {
	Dynamic[i] = Dynamic[i - 1] + Matrix.at(i - 1).size();
    }
    for (unsigned i = 0; i < Matrix.size(); i++) {
	for (unsigned j = 0; j < Matrix.at(i).size(); j++)
	    Dynamic[i][j] = Matrix.at(i).at(j);
    }
    return Dynamic;
}

int main()
{
    std::vector<std::deque<double>> m {{1,2,3}, {1,2,3}, {1,2,3}};

    auto TwoD = MakeDynamicCopy2D(m);
    for (unsigned i=0; i<3; ++i) {
	for (unsigned j=0; j<3; ++j) {
	    cout << TwoD[i][j] << ' ';
	}
	cout << '\n';
    }
}

dhayden,


I also concluded that. I was running it on server that has corrupted autotest. It was error in autotest. Thank you.
Last edited on
Topic archived. No new replies allowed.