Stack around the variable "xPos" was corrupted

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • decidence
    New Member
    • Jun 2007
    • 2

    Stack around the variable "xPos" was corrupted

    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

    Code:
    	int xPos[32];
    	int yPos[32];
    	char buffer[256];
    	char line[255];
    	int i = 0;
    	string newstring;
    
    	// Convert all the input data to integer arrays
    	do
    	{
    		// Data before the comma
    		fIn.getline(line, sizeof(buffer), ',');
    		newstring.assign(line);
    		xPos[i] = atoi(newstring.c_str());
    
    		// Data after the comma
    		fIn.getline(line, sizeof(buffer));
    		newstring.assign(line);
    		yPos[i] = atoi(newstring.c_str());
    
    		// Increment array iterator and continue looping down the file's lines until end of file
    		i++;
    	}while(!fIn.eof());
    
    	fIn.close();
    
    	cout << xPos[31] << ", " << yPos[31] << endl;
    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.
  • svlsr2000
    Recognized Expert New Member
    • Feb 2007
    • 181

    #2
    Originally posted by decidence
    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

    Code:
    	int xPos[32];
    	int yPos[32];
    	char buffer[256];
    	char line[255];
    	int i = 0;
    	string newstring;
     
    	// Convert all the input data to integer arrays
    	do
    	{
    		// Data before the comma
    		fIn.getline(line, sizeof(buffer), ',');
    		newstring.assign(line);
    		xPos[i] = atoi(newstring.c_str());
     
    		// Data after the comma
    		fIn.getline(line, sizeof(buffer));
    		newstring.assign(line);
    		yPos[i] = atoi(newstring.c_str());
     
    		// Increment array iterator and continue looping down the file's lines until end of file
    		i++;
    	}while(!fIn.eof());
     
    	fIn.close();
     
    	cout << xPos[31] << ", " << yPos[31] << endl;
    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.

    Comment

    • decidence
      New Member
      • Jun 2007
      • 2

      #3
      I get another run time error saying vector subscript out of range. This time the program doesn't run at all.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        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:

        [code=cpp]
        // Data before the comma
        fIn.getline(lin e, sizeof(buffer), ',');
        newstring.assig n(line);
        xPos[i] = atoi(newstring. c_str());

        [/code]

        1) why use a string?? fln.getline() has converted the ',' into a \0.
        Just do:
        [code=cpp]
        xPos[i] = atoi(buffer);
        [/code]

        2) atoi() is a deprecated function in C++. Use a stringstream instead:
        [code=cpp]
        stringstream ss;
        fIn.getline(lin e, sizeof(buffer), ',');
        ss << buffer;
        ss >> xPos[i];
        [/code]
        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.

        Comment

        Working...