title: returning pointer from function giving me random numbers
body: <p>I am working on a small console game on my free time and have come across a bug I can't seem to fix no matter what I try. I have tried a lot of different things with the pointers so this is just the latest version of my code. I have been searching around and a few questions other's have asked indicated I may be experiencing a memory leak, or that I am reading values from beyond my arrays (don't understand how). However, those questions have been solved without leaving me any hints as to what is wrong with my code. </p>
  
  <p>Basically, I have a function called int <code>* spendAttribute(int point);</code>
  Since anything created in that function is out of scope in my <code>main()</code> I want to take 6 int out of that function, and bring them into my <code>main()</code>.</p>
  
  <p>I thought "hey why not use a pointer!" The function is supposed to return a pointer to an array created during the function, and paste it to another pointer created in <code>main()</code>, this has worked once before in my main(), but now it's not working and I have no clue why.</p>
  
  <pre><code> int * spendAttribute(int point)
   {
   string choice;
   int hp,hpT,endur,endurT,dmg,dmgT,armor,armorT,agility,agilityT,evade,evadeT;
   while(condition)
    {
       //do a bunch of junk ....
    }
   int stats[6] = {hp,endur,dmg,armor,agility,evade}; 
  
   //some cout testing to see if values are correct (they are) 
  
   int* p_stats = new int [6]; //create a block of 6
   p_stats = &amp;stats[0]; //point to the first value of stats array
   return p_stats; //return pointer
   delete [] p_stats; //delete blocks
   }
  </code></pre>
  
  <p>Note: I have tried without deleting the blocks and it still does not work. I tried this since I read that it might be a memory leak.I have tried it without using new at all.</p>
  
  <pre><code>main()
  {
  
   //.... some junk
   while(main game loop)
     {
        int * pointer;
        cout &lt;&lt; "*************** Enter 'begin' to commence ****************** " &lt;&lt; endl ;
        cout &lt;&lt; "*************** Enter 'spend' to use new attribute points ** " &lt;&lt; endl ;
        cin &gt;&gt; beginChoice; //declared before while loop
        if(beginChoice == "spend")
        {
  
          cout &lt;&lt; "total attributes: " &lt;&lt; Bryan.returnAttribute() &lt;&lt; endl ;
          pointer = spendAttribute(Bryan.returnAttribute()); //copy pointer
          cout &lt;&lt; "TEST: " &lt;&lt; endl ;
          cout &lt;&lt; pointer[0] &lt;&lt; endl ; //out put is a bunch of random numbers..
          cout &lt;&lt; pointer[1] &lt;&lt; endl ;
          cout &lt;&lt; pointer[2] &lt;&lt; endl ;
          cout &lt;&lt; pointer[3] &lt;&lt; endl ;
          cout &lt;&lt; pointer[4] &lt;&lt; endl ; 
          cout &lt;&lt; pointer[5] &lt;&lt; endl ; //SOME DAMN BUG HERE
          Bryan.addMaxHp(pointer[0]);
          Bryan.addEndurance(pointer[0]);
          Bryan.addDmg(pointer[0]);
          Bryan.addArmor(pointer[0]);
          Bryan.addAgility(pointer[0]);
          Bryan.addEvasion(pointer[0]);
  
          //after this character ends up having some ridiculous stats like -564553535% 
          //evasion or 9879967856 armor...
  
        }
      }
    }
  </code></pre>
  
  <p>This method of transferring the array over to main worked for me before in this exact file, so I don't know exactly how I am getting these errors or what's causing them. I have even tried deleting the previous pointer used to see if thats what was causing it but it wasn't.
  Please Halp.</p>