Pointer Memory Allocation in C++
<pre><code>#include &lt;iostream&gt;
#include &lt;cmath&gt;
#include &lt;limits.h&gt;
#include &lt;stdlib.h&gt;

using namespace std ;

long long NumberHouses = 0 ;
long long ClosestHouse[2] = {0,0} ;
long long TravelTimes = 0 ;

void GetHouses(long long NumH, long long House_X[], long long House_Y[])
{ 
     for (long long a = 0 ; a &lt; NumH ; a++)
     {
         cin &gt;&gt; House_X[a] ;
         cin &gt;&gt; House_Y[a] ;
     }
}

void SortArray(long long NumH, long long SortedArray[], long long HouseArray[])
{
     long long InsertPoint = 0 ; 

     for (int a = 0 ; a &lt; NumH ; a++)
     {
         SortedArray[a] = LONG_MAX ;
     }

     for (int a = 0 ; a &lt; NumH ; a++)
     {
         for (int b = 0 ; b &lt; NumH ; b++)
         {
             if (HouseArray[a] &lt; SortedArray[b]) 
             {
                 InsertPoint = b ; 

                 for (int c = NumH ; c &gt; b ; c--)
                 {
                     SortedArray[c] = SortedArray[c-1] ;
                 }

                 SortedArray[InsertPoint] = HouseArray[a] ;
                 b = NumH ;
             }
         } 
     }
}                 

void FindMedians(long long NumH, int NumMeds, long long SortedArray[], long long MediansArray[])
{
     int MedianNum = 0 ;

     long long *Sorted_X = new long long [NumH] ;
     long long *Sorted_Y = new long long [NumH] ;

     MedianNum = NumH / 2 ;

     if (NumMeds == 1)
     {
         MediansArray[0] = SortedArray[MedianNum] ; 
     }
     else
     {
         MediansArray[0] = SortedArray[MedianNum] ;
         MediansArray[1] = SortedArray[MedianNum + 1] ;
     }
}        

void FindMidHouse(long long NumH, int NumMeds, long long Cords_X[], long long Cords_Y[], long long Meds_X[], long long Meds_Y[]) 
{
     long long *CloseHouses = new long long [NumH] ;

     long long NumPoints = 0 ;
     long long LowestMoves = LONG_MAX ;

     for (int a = 0 ; a &lt; NumH ; a++)
     {
         if (abs(Cords_X[a] - Meds_X[0]) &gt; abs(Cords_X[a] - Cords_Y[0]))
         {
             if (abs(Cords_X[a] - Meds_X[0] &lt;= LowestMoves))
             {
                 LowestMoves = abs(Cords_X[a] - Meds_X[0]) ;
                 CloseHouses[a] = a ;
                 NumPoints++ ;
             }
         }
         else
         {
             if (abs(Cords_Y[a] - Meds_Y[0] &lt;= LowestMoves))
             {
                 LowestMoves = abs(Cords_Y[a] - Meds_Y[0]) ;
                 CloseHouses[a] = a ;
                 NumPoints++ ;
             }
         }
     }

     long long *CloseHousesSums = new long long [NumH] ;

     for (int a = 0 ; a &lt; NumPoints ; a++)
     {
         for (int b = 0 ; b &lt; NumH ; b++)
         {
             if ((abs(Cords_X[CloseHouses[a]]) - Cords_X[b]) &gt; (abs(Cords_Y[CloseHouses[a]]) - Cords_Y[b]))
             {
                 CloseHousesSums[a] = CloseHousesSums[a] + abs(Cords_X[CloseHouses[a]] - Cords_X[b]) ;
             }
             else
             {
                 CloseHousesSums[a] = CloseHousesSums[a] + abs(Cords_Y[CloseHouses[a]] - Cords_Y[b]) ;
             }
         }
     }

     for (int a = 0 ; a &lt; (NumPoints - 1) ; a++)
     {
         if (CloseHousesSums[a] &lt; CloseHousesSums[a+1])
         {
             ClosestHouse[0] = Cords_X[CloseHouses[a]] ;
             ClosestHouse[1] = Cords_Y[CloseHouses[a]] ;
         }
     }
}

void GetClosestHouse(long long NumH, long long House_X[], long long House_Y[], long long ClosestHouse[]) 
{   
     int MedianAmt = 0 ; 

     long long *Sorted_X = new long long [NumH] ;
     long long *Sorted_Y = new long long [NumH] ;

    /* long long *Sorted_1 = new long long ;//[NumH] ;
     long long *Sorted_a = new long long ;//[NumH] ;
     long long *Sorted_b = new long long ;//[NumH] ;
     long long *Sorted_c = new long long ;//[NumH] ;
     long long *Sorted_d = new long long ;//[NumH] ;
     long long *Sorted_e = new long long ;//[NumH] ;
     long long *Sorted_f = new long long ;//[NumH] ;*/


     SortArray(NumberHouses, Sorted_X, House_X) ;
     SortArray(NumberHouses, Sorted_Y, House_Y) ;

     for (int a = 0 ; a &lt; NumH ; a++)
     {
         cout &lt;&lt; Sorted_X[a] &lt;&lt; "," &lt;&lt; Sorted_Y[a] &lt;&lt; endl ;
     }

     if (NumH % 2 == 0)
     {
         MedianAmt = 1 ;
     }
     else
     {
         MedianAmt = 2 ;
     }
     cout &lt;&lt; MedianAmt &lt;&lt; endl ;
     cout &lt;&lt; "sun" ;
     long long *Medians_X = new long long [MedianAmt] ;
     long long *Medians_Y = new long long [MedianAmt] ;
     cout &lt;&lt; "shine" &lt;&lt; endl ;
     FindMedians(NumberHouses, MedianAmt, Sorted_X, Medians_X) ;
     FindMedians(NumberHouses, MedianAmt, Sorted_Y, Medians_Y) ;

     FindMidHouse(NumberHouses, MedianAmt, House_X, House_Y, Medians_X, Medians_Y) ;

}   

void GetHouseDistances(long long NumH, long long House_X[], long long House_Y[], long long ClosestHouse[], long long&amp; TravelTimes)
{ 
      long long Difference_X = 0 ;
      long long Difference_Y = 0 ; 

      for (int a = 0 ; a &lt; NumH ; a++)
      {
          Difference_X = abs(ClosestHouse[0] - House_X[a]) ;     
          Difference_Y = abs(ClosestHouse[1] - House_Y[a]) ;

          //cout &lt;&lt; Difference_X &lt;&lt; " - Difference_X" &lt;&lt; endl ;
          //cout &lt;&lt; Difference_Y &lt;&lt; " - Difference_Y" &lt;&lt; endl ;

          if (Difference_X &gt; Difference_Y)
          {
              TravelTimes = TravelTimes + Difference_X ;
          }
          else
          {
              TravelTimes = TravelTimes + Difference_Y ;
          }   
      }
      //cout &lt;&lt; TravelTimes &lt;&lt; endl ;
}                                 

int main()
{
     cin &gt;&gt; NumberHouses ;

     long long *House_X = new long long [NumberHouses] ; // x coordinate of houses
     long long *House_Y = new long long [NumberHouses] ; // y coordinate of houses

     GetHouses(NumberHouses, House_X, House_Y) ;
     GetClosestHouse(NumberHouses, House_X, House_Y, ClosestHouse) ;   
     GetHouseDistances(NumberHouses, House_X, House_Y, ClosestHouse, TravelTimes) ;

     cout &lt;&lt; TravelTimes &lt;&lt; endl ;
}
</code></pre>

<p>Now - you'll notice that I have a huge commented out section of code of other arrays being created similarly - my code works when I have those uncommented - but when I comment them it crashes after outputting 'sun' but before 'shine'. </p>

<p>I have tried using other arguments for the length of memory to create - eg MedianAmt, and this includes other variables and actual numbers. I have also tried using a long long variable as an argument as well.</p>

<p>I am not sure why it doesn't work - and if you would like for me to provide any more information about my code or situation I will do my best to answer them - thanks for the help.</p>

<p>edit: I run the executable and it gets to 'sun' and then it freezes - it can take a second or two before it goes unresponsive and closes. NuMH can be up to long long max amount - 64 bits I don't know where I freed any memory - I am a relatively novice programmer - and no error message was given. I was thinking it was a heap corruption problem as well - but why would adding in all of the extra long long arrays above that I have commented out cause it to not crash? </p>

<p>I can post the rest of my source code - but it's 200 lines long and a probably a tad messy - would this be beneficial?</p>

<p>edit 2: As for heap corruption I have my IDE and program saved on my flashdrive - but have tried to run it on two different computers - so I think it's safe to assume that the heap isn't corrupted. Maybe an invalid pointer to the memory is the problem, can the IDE affect where my pointer would go to choose the memory? </p>

<p>edit 3: I run windows 7 on both machines that I have tried running.</p>

<p>edit 4: Updated to have my full source code..</p>
