Connecting Tech Pros Worldwide Forums | Help | Site Map

Stack Corrupted

Newbie
 
Join Date: Jan 2007
Posts: 2
#1: Jan 4 '07
Hi, could anyone help me with this problem? I'm getting the following error message when I try to run this code -
"Run-Time Check Failure #2 - Stack around the variable 'vY' was corrupted"
Where am I going wrong? (I'm using visual C++)


Expand|Select|Wrap|Line Numbers
  1. #include <iostream>        // Needed for cin, cout, cerr, endl
  2. #include <cmath>        // Needed for Sin/Cos functions
  3.  
  4.  
  5. using std::cout;
  6. using std::endl;
  7.  
  8. #include <iomanip>
  9.  
  10. using std::setw;
  11.  
  12. int main()
  13. {
  14. //Will need arrays to store particles inital x and y co-ordinates so declaring them
  15.  
  16. const int numParticles = 10;
  17.  
  18. double pX[numParticles];
  19. double pY[numParticles];
  20.  
  21. // And arrays for particles velocity in x and y directions
  22.  
  23. double vX[numParticles];
  24. double vY[numParticles];
  25.  
  26. int screenWidth = 100;
  27. int screenHeight = 100;
  28.  
  29. double dRand(double max);
  30.  
  31. // Initialise particle positions and velocities randomly.
  32. void initParticles();
  33. {
  34. //intialise particle position co-ordinates and velocities
  35.     int numParticles = 10; //need to set the number of particles in the array to something!
  36.     {
  37.         for(int i=0; i<numParticles; i++) //counting through each element of the array
  38.             pX[i] = dRand(screenWidth);   //particle's inital x co-ordinate
  39.                pY[i] = dRand(screenHeight);  //particle's inital y co-ordinate
  40.             vX[i] = dRand(100);           //initial x velocity
  41.             vY[i] = dRand(100);           //inital y velocity
  42.     }
  43. }
  44. for (int j=0; j<10; j++)
  45. cout<< setw ( 7 ) << j << setw ( 13 ) << pX[j] << endl;
  46.  
  47. return 0;
  48. }
  49. double dRand(double max)
  50. {
  51. return max * (static_cast<double>(rand()) /static_cast<double>(RAND_MAX));
  52. }
Thanks

Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,202
#2: Jan 4 '07

re: Stack Corrupted


In this section
Expand|Select|Wrap|Line Numbers
  1. for(int i=0; i<numParticles; i++) //counting through each element of the array
  2.     pX[i] = dRand(screenWidth);   //particle's inital x co-ordinate
  3.     pY[i] = dRand(screenHeight);  //particle's inital y co-ordinate
  4.     vX[i] = dRand(100);           //initial x velocity
  5.     vY[i] = dRand(100);           //inital y velocity
  6.  
You are missing { and } round the statements to execute in the for loop. The loop is equivilent to

Expand|Select|Wrap|Line Numbers
  1. for(int i=0; i<numParticles; i++) //counting through each element of the array
  2. {
  3.     pX[i] = dRand(screenWidth);   //particle's inital x co-ordinate
  4. }
  5.  
  6. pY[i] = dRand(screenHeight);  //particle's inital y co-ordinate
  7. vX[i] = dRand(100);           //initial x velocity
  8. vY[i] = dRand(100);           //inital y velocity
  9.  
When pY, vX and vY are accessed i = numParticles+1 making all of these out of bounds array accesses.

Correct use of { and } will solve the problem

Expand|Select|Wrap|Line Numbers
  1. for(int i=0; i<numParticles; i++) //counting through each element of the array
  2. {
  3.     pX[i] = dRand(screenWidth);   //particle's inital x co-ordinate
  4.     pY[i] = dRand(screenHeight);  //particle's inital y co-ordinate
  5.     vX[i] = dRand(100);           //initial x velocity
  6.     vY[i] = dRand(100);           //inital y velocity
  7. }
  8.  
Newbie
 
Join Date: Jan 2007
Posts: 2
#3: Jan 4 '07

re: Stack Corrupted


Thanks, should've noticed that!
Have now got another problem however, when I build and run the code it flashes up the program very quicky (too quickly to even be read really) and then it disappears! Do you know what's going on?
Ganon11's Avatar
Moderator
 
Join Date: Oct 2006
Location: New York, United States of America
Posts: 3,428
#4: Jan 4 '07

re: Stack Corrupted


When you finish your int main() function, you can include the statement

Expand|Select|Wrap|Line Numbers
  1. system("PAUSE");
right before your return statement to pause the screen before quitting. This should allow you to see any output generated before the program closes.

If this doesn't help, there may be another error inside your code causing the program to terminate early.
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,202
#5: Jan 4 '07

re: Stack Corrupted


Quote:

Originally Posted by Ganon11

Expand|Select|Wrap|Line Numbers
  1. system("PAUSE");

Personally I really hate this approach but can't think of a really good argument against it (it is a little non-portable though) although you can end up having to press space twice in some instances. Partly because it is not a part of the programs functionality.

I suspect the reason it flashes up is that you are running in debug mode. In most IDEs if you run in debug mode then the program will not pause once it has finished because it assumes you will set a break point if you want the program to stop (I assume). If instead of running it in the debugger you just run it (as normal) then in most environments the OS will pause before shutting the window (or at least it is configurable).
Reply