### Is this ok for a 2D dynamic array?

Hello,

I'm using this code in a project where I need to use a dynamic 2D array. So far it's working, but in a particular circumstances where I try do add an element to the 10th row I get a "segmentation fault: 11". Oddly, when I try to add to the 11th, 12th, ... 200th row it works fine! As I'm still trying to figure out where the problem might be, I decided to put here the code regarding the function that adds elements to my 2D array, the rest of the class is omitted as I think this method might not be optimal yet. The default constructor of my 2D array creates a 2x2 array. Thanks in advance for any feedback!

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263`` ``````/** * Inserts an element, passed by parameter, into the 2-dimensional array. If * necessary the array's capacity is changed in case the element is assigned to * a row or column that doesn't exist yet. This method allows the insertion of * an element in a specific position in the 2D array, which means that the row * and column are also passed into the function as parameters. Note that this * functions produces a simetric matrix, i.e., an element introduced into (a,b) * will also be added to (b,a). In case the matrix already contains an element of * the given type, that insertion will not occur. * @param elem it's the reference to the element to be added to the 2D array * @param row row where element is added * @param col column where element is added * @return true, in case the insertion is sucessful; false, otherwise */ template bool Dyn2DArray::insert(const T &elem, int row, int col){ /* checks whether row > col or col > row and assigns that value to maxTemp*/ int maxTemp; if(row > col) maxTemp = row; else maxTemp = col; /* checks whether the 2D array has enough rows or cols for the new element */ if(maxTemp > cap){ /* stores the previous capacity of the vector */ int prevCap = cap; /* array's new cap is maxTemp+1 */ cap = maxTemp + 1; /* temporary array with new capacity */ T **newVec = new T *[cap]; for(int i = 0; i < cap; i++) newVec[i] = new T[cap]; /* saving the content of the "smaller" array to the temporary array */ for(int i = 0; i < prevCap; i++) for(int j = 0; j < prevCap; j++) newVec[i][j] = vec[i][j]; /* releasing resources of the "smaller" array */ for(int i = 0; i < prevCap; i++) delete [] vec[i]; delete [] vec; /* assigning this "bigger" array to the original pointer */ vec = newVec; } /* adding the element to the given position, producing a simetric matrix is below */ /* pointer to new T, necessary to compare with un-initialized types */ T *blank = new T; /* if the element in the given row/col is equal to a un-initialized type then the insertion can proceed */ if((vec[row][col] == *blank) || (vec[col][row] == *blank)){ vec[row][col] = elem; vec[col][row] = elem; /* returns true to indicate sucessfull insertion */ return true; } else /* returns false in case the insertion cannot occur, i.e., if there's already an element in the given spot */ return false; }``````
So, you're leaking memory every time you call this function.

I suspect the segfault problem is the way you're using cap. On line 26 it seems as if you're treating it as the last valid row/column. On line 30, it seems as if you're treating it as the number of rows/columns. The two differ by one.

Line 49 should be: `T blank ;` And then you can remove the asterisks from line 52 and stop leaking that memory.

You should consider using vectors instead of arrays.
Last edited on
Topic archived. No new replies allowed.