By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,193 Members | 843 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,193 IT Pros & Developers. It's quick & easy.

Stack around the variable "xPos" was corrupted

P: 2
Ok, this is an odd problem for me, I know how to stop it from happening but I get incorrect functionality when I do.

This is a run time error that occurs when the program is shut down, I get the correct functionality up until that point.

Here is the code block

Expand|Select|Wrap|Line Numbers
  1.     int xPos[32];
  2.     int yPos[32];
  3.     char buffer[256];
  4.     char line[255];
  5.     int i = 0;
  6.     string newstring;
  7.  
  8.     // Convert all the input data to integer arrays
  9.     do
  10.     {
  11.         // Data before the comma
  12.         fIn.getline(line, sizeof(buffer), ',');
  13.         newstring.assign(line);
  14.         xPos[i] = atoi(newstring.c_str());
  15.  
  16.         // Data after the comma
  17.         fIn.getline(line, sizeof(buffer));
  18.         newstring.assign(line);
  19.         yPos[i] = atoi(newstring.c_str());
  20.  
  21.         // Increment array iterator and continue looping down the file's lines until end of file
  22.         i++;
  23.     }while(!fIn.eof());
  24.  
  25.     fIn.close();
  26.  
  27.     cout << xPos[31] << ", " << yPos[31] << endl;
  28.  
The error does not occur if I add the ',' delimiter to the second getline call but doing so gives me the incorrect functionality.

I am pulling integer values out of a .csv file if that helps.
Jun 4 '07 #1
Share this Question
Share on Google+
3 Replies


Expert 100+
P: 181
Ok, this is an odd problem for me, I know how to stop it from happening but I get incorrect functionality when I do.

This is a run time error that occurs when the program is shut down, I get the correct functionality up until that point.

Here is the code block

Expand|Select|Wrap|Line Numbers
  1.     int xPos[32];
  2.     int yPos[32];
  3.     char buffer[256];
  4.     char line[255];
  5.     int i = 0;
  6.     string newstring;
  7.  
  8.     // Convert all the input data to integer arrays
  9.     do
  10.     {
  11.         // Data before the comma
  12.         fIn.getline(line, sizeof(buffer), ',');
  13.         newstring.assign(line);
  14.         xPos[i] = atoi(newstring.c_str());
  15.  
  16.         // Data after the comma
  17.         fIn.getline(line, sizeof(buffer));
  18.         newstring.assign(line);
  19.         yPos[i] = atoi(newstring.c_str());
  20.  
  21.         // Increment array iterator and continue looping down the file's lines until end of file
  22.         i++;
  23.     }while(!fIn.eof());
  24.  
  25.     fIn.close();
  26.  
  27.     cout << xPos[31] << ", " << yPos[31] << endl;
  28.  
The error does not occur if I add the ',' delimiter to the second getline call but doing so gives me the incorrect functionality.

I am pulling integer values out of a .csv file if that helps.
There is no bound checking for array xpos and ypos. This might be the reason for your error.
Try using vector instead of array. Its more safe.
Jun 4 '07 #2

P: 2
I get another run time error saying vector subscript out of range. This time the program doesn't run at all.
Jun 4 '07 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
That's probably because the vector error checking comes into play. Your code has no index binds checking as svlsr2000 has said already.

However, you do have some odd code:

Expand|Select|Wrap|Line Numbers
  1. // Data before the comma
  2.         fIn.getline(line, sizeof(buffer), ',');
  3.         newstring.assign(line);
  4.         xPos[i] = atoi(newstring.c_str());
  5.  
  6.  
1) why use a string?? fln.getline() has converted the ',' into a \0.
Just do:
Expand|Select|Wrap|Line Numbers
  1.           xPos[i] = atoi(buffer);
  2.  
2) atoi() is a deprecated function in C++. Use a stringstream instead:
Expand|Select|Wrap|Line Numbers
  1.           stringstream ss;
  2.           fIn.getline(line, sizeof(buffer), ',');
  3.           ss << buffer;
  4.           ss >>  xPos[i];
  5.  
As to the ',' delimiter, if you don't put that in the default is a \n, which is not what you want.

You might show us your vector code. Certainly in C++ you don't want to use naked arrays. vector is required to implement an array so you are really using the same data structure but you don't have to code for it.
Jun 4 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.