473,508 Members | 2,367 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Stack around the variable "xPos" was corrupted

2 New Member
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
3 8816
svlsr2000
181 Recognized Expert New Member
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
decidence
2 New Member
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
9,208 Recognized Expert Moderator Expert
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

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

Similar topics

6
4939
by: cleary1981 | last post by:
I have adapted code from http://dunnbypaul.net/js_mouse/ I want to use a button to create new draggable divs but i keep getting error "is null or not an object" heres the code <html> <head>...
0
7115
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7377
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7036
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5047
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4705
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1547
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
414
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.