Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ 2D array of pointers segmentation fault
    primarykey
    data
    text
    <p>I am facing an issue with a 2D array of pointers. It compiles with no errors, however when I try to run the file, all I get is a single line saying I have a segmentation fault.</p> <p>My header file:</p> <pre><code>#ifndef __TWODARRAY_H__ #define __TWODARRAY_H__ template &lt;typename T&gt; class TwoDArray { private: T** theArray; int numRows; int numCols; T defSpace; public: TwoDArray&lt;T&gt; (int r, int c, T def); ~TwoDArray&lt;T&gt;(); void insert(int r, int c, T value); T access(int r, int c); void remove(int r, int c); void print(); int getNumRows(); int getNumCols(); }; #endif </code></pre> <p>My Methods:</p> <pre><code>#include "TwoDArray.h" #include &lt;iostream&gt; #include &lt;assert.h&gt; #include &lt;string&gt; //initializes the 2D Array template &lt;typename T&gt; TwoDArray&lt;T&gt;::TwoDArray(int r, int c, T def) { assert(r &gt; 0 &amp;&amp; c &gt; 0); numRows = r; numCols = c; defSpace = def; theArray = new T*[r]; for(int i=0; i&lt;r; i++) { theArray[i] = new T[c]; } //sets all values to the default for(int i=0; i&lt;r; i++) { for(int j=0; j&lt;c; j++) { theArray[i][j] = defSpace; } } } //deletes the 2D Array template&lt;typename T&gt; TwoDArray&lt;T&gt;::~TwoDArray() { for(int i=0; i&lt;numRows; i++) { delete[] theArray[i]; } delete[] theArray; } //inserts value v at row r and column c template&lt;typename T&gt; void TwoDArray&lt;T&gt;::insert(int r, int c, T value) { assert(r &lt; numRows &amp;&amp; c &lt; numCols); assert(value != defSpace); theArray[r][c] = value; } //get value at row r, column c template&lt;typename T&gt; T TwoDArray&lt;T&gt;::access(int r, int c) { assert(r &lt; numRows &amp;&amp; c &lt; numCols); T result = theArray[r][c]; return result; } //set value at row r and column c back to default template&lt;typename T&gt; void TwoDArray&lt;T&gt;::remove(int r, int c) { assert(r &lt; numRows &amp;&amp; c &lt; numCols); assert(theArray[r][c] != defSpace); theArray[r][c] = defSpace; } //print the 2D Array template&lt;typename T&gt; void TwoDArray&lt;T&gt;::print() { for(int i=0; i&lt;numRows; i++) { for(int j=0;j&lt;numCols; i++) { std::cout &lt;&lt; theArray[i][j]; std::cout &lt;&lt; " "; } std::cout &lt;&lt; std::endl; } } //gets number of rows for test template&lt;typename T&gt; int TwoDArray&lt;T&gt;::getNumRows() { return numRows; } //gets number of columns for test template&lt;typename T&gt; int TwoDArray&lt;T&gt;::getNumCols() { return numCols; } template class TwoDArray&lt;int&gt;; template class TwoDArray&lt;std::string&gt;; </code></pre> <p>And my main:</p> <pre><code>#include &lt;iostream&gt; #include &lt;string&gt; #include "TwoDArray.h" using std::cout; using std::endl; int main() { TwoDArray&lt;int&gt;* i = new TwoDArray&lt;int&gt;(5, 5, 0); TwoDArray&lt;std::string&gt;* s = new TwoDArray&lt;std::string&gt;(5, 5, "o"); i-&gt;insert(1, 1, 1); i-&gt;insert(1, 3, 1); i-&gt;insert(3, 2, 8); i-&gt;insert(2, 0, 3); i-&gt;insert(2, 4, 3); i-&gt;insert(3, 2, 8); i-&gt;print(); s-&gt;insert(0, 2, "North"); s-&gt;insert(4, 2, "South"); s-&gt;insert(2, 4, "East"); s-&gt;insert(2, 0, "West"); s-&gt;print(); return 0; } </code></pre> <p>Any ideas why I'm getting a segmentation fault?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload