title: How to solve segmentation fault error C++
body: <p>I'm writing a program that gets an array of integers and its logical size. When called it creates a new array containing only the positive numbers from the arrays.</p>
  
  <p>Now, in order to do this I need to write a void type function  takes the following parameters: </p>
  
  <p><code>(int* arr, int arrSize, int** outPosArrPtr, int* outPosArrSizePTR)</code></p>
  
  <p>I'm supposed to use the pointer <code>int** outPosArrPtr</code> to update the base address of the array containing the positive numbers, and the pointer <code>outPosArrSizePtr</code> to update the array's logical size.</p>
  
  <p>now when I run my code on the xcode compiler the logical size gets updated to a very larger number. So, when I tried to run the program using the online gdb compiler I got the error "Segmentation fault."</p>
  
  <p>From reading what Segmentation fault means I learnt that it means that I'm trying to access memory that "does not belong to me" or memory that is not in the call stack or in the heap portion of the program. </p>
  
  <p>I tried to debug my code by seeing if I was referening any null pointers or see if I was referencing any dangling pointers but it seems like the problem is another one.  </p>
  
  <p>My code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  typedef int* IntArrPtr;
  using namespace std;
  
  
  int main() {
      int arrSize;
      int *ptrSize;
      ptrSize = &amp;arrSize;
  
      cout &lt;&lt; "How many integers will this array hold:\n ";
      cin &gt;&gt; arrSize;
  
      IntArrPtr a;
      a = new int[arrSize];
  
      fillArr(a, arrSize);
  
      getPosNums4(a, arrSize,&amp;a, ptrSize);
      cout &lt;&lt; "The new size in main is: " &lt;&lt; arrSize &lt;&lt; endl;
  
      cout &lt;&lt;"The new array with positive integers is:\n";
      /*for(int i =0; i&lt;arrSize;i++) // this runs for a large size of arrSize
          cout&lt;&lt; a[i] &lt;&lt; " ";
      cout&lt;&lt;endl; */
      return 0;
  }
  
  void fillArr(int a[], int size){
      cout &lt;&lt; "Please enter " &lt;&lt; size &lt;&lt; " Integers separated by spaces\n";
      cout &lt;&lt; "Press enter when finished &gt;\n";
      int i;
  
      for (i=0;i&lt;size;i++)
          cin &gt;&gt; a[i];
  
  }
  
  void getPosNums4(int* arr, int arrSize, int** outPosArrPtr,int* outPosArrSizePtr){
      IntArrPtr newArr;
      newArr = new int[arrSize];
      int i;
      int newIndx = 0;
      outPosArrSizePtr = &amp;newIndx;//initiliaze the pointer.
      for(i=0;i&lt;arrSize;i++){
          if(arr[i] &gt; 0){
              newArr[newIndx] =arr[i];
              newIndx++;
          }
      }
      arrSize = newIndx;
      *outPosArrSizePtr = arrSize;
      cout &lt;&lt; "The new size is of *outPosArrSizeptr is: " &lt;&lt; *outPosArrSizePtr &lt;&lt; endl;
  
      for(int j=0;j&lt;newIndx;j++)
          outPosArrPtr[j] = &amp;newArr[j];
      delete []newArr;
      newArr = NULL;
      for(int i=0;i&lt;newIndx;i++)
          arr[i] = *outPosArrPtr[i];
  
  }
  </code></pre>
  
  <p>an example When I run this program on Xcode:</p>
  
  <pre><code>How many integers will this array hold:
   6
  Please enter 6 Integers separated by spaces
  Press enter when finished &gt;
  3 -1 -3 0 6 4
  The new size is of *outPosArrSizeptr is: 3
  The new array with positive integers is:
  The new size in main is: 7445512
  The program ended with exit code: 0
  </code></pre>