CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Hybrid View

  1. #1
    Join Date
    May 2010
    Location
    Glasgow, Scotland
    Posts
    9

    Unhappy Stack around the variable '<array name>' was corrupted

    I've written some code for a Uni assignment, and it worked perfectly. Basically the task was to declare a global array, then write 4 functions:

    1) enter 10 values and store them in an array
    2) display the populated array
    3) find the min & max values in array and display them
    4) accept a value from the keyboard and search for it in the array.

    then call them from main().

    As I said, what I wrote compiles, builds & runs perfectly, with no errors, and the code is below.

    The next part of the assignment was to modify the above code so that the array is local to main(), then pass the array to each of the functions. I did this, and again it compiles and builds ok, with no errors, but it comes up with a runtime error "Run-Time Check Failure #2 - Stack around the variable 'myArray' was corrupted". However the program seems to actually run and terminate just fine if I click "Abort" to the error.

    The error comes up immediately after I press return after entering a value to search for in the searchArray() function, so I presume the error lies in that function (although like I said, the search is performed and displayed just fine if I click "Abort" to the error message).

    I tried moving the #define statement from inside main() to the top of the code to make it global, but that produces the same error.

    First here is my code (working 100%) that has the array as global:

    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    #define SIZE 9
    
    int myArray[SIZE];
    
    void takeValues()
    {
    	int a;
    
    	for(a=0; a<=SIZE; a++)
    	{
    		cout << "Enter value " << a + 1 << ": ";
    		cin >> myArray[a];
    	}
    }
    
    void displayValues()
    {
    	int b;
    
    	cout << endl << "Element" << setw(8) << "Value" << endl;
    
    	for(b=0; b<=SIZE; b++)
    	{
    		cout << b << setw(12) << myArray[b] << endl;
    	}
    }
    
    void findMinMax()
    {
    	int c, min=myArray[0], max=myArray[0];
    
    	for(c=1; c<=SIZE; c++)
    	{
    		if(myArray[c] > max) max = myArray[c];
    		else
    			if(myArray[c] < min) min = myArray[c];
    	}
    	
    	cout << endl << "Min is " << min << endl;
    	cout << "Max is " << max << endl << endl;
    }
    
    void searchArray()
    {
    	int d, e, f;
    	bool found = false;
    
    	cout << "Enter a value to search for: ";
    	cin >> d;
    
    	{
    		for(e=0; e<=SIZE; e++)
    		{
    			if(myArray[e] == d)
    			{
    				f = d;
    				found = true;
    				cout << "I found " << f << " at position " << e << " of the array." << endl <<endl;
    				break;
    			}
    		}
    	}
    
    	if(!found) cout << d << " was not found in the array." << endl;
    }
    		
    
    int main()
    {
    	takeValues();
    	displayValues();
    	findMinMax();
    	searchArray();
    }
    Secondly, here is my code that has the array local to main() and produces the runtime error:

    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    void takeValues(int myArray[], const int n)
    {
    	int a;
    
    	for(a=0; a<=n; a++)
    	{
    		cout << "Enter value " << a + 1 << ": ";
    		cin >> myArray[a];
    	}
    }
    
    void displayValues(int myArray[], const int n)
    {
    	int b;
    
    	cout << endl << "Element" << setw(8) << "Value" << endl;
    
    	for(b=0; b<=n; b++)
    	{
    		cout << b << setw(12) << myArray[b] << endl;
    	}
    }
    
    void findMinMax(int myArray[], const int n)
    {
    	int c, min=myArray[0], max=myArray[0];
    
    	for(c=1; c<=n; c++)
    	{
    		if(myArray[c] > max) max = myArray[c];
    		else
    			if(myArray[c] < min) min = myArray[c];
    	}
    	
    	cout << endl << "Min is " << min << endl;
    	cout << "Max is " << max << endl << endl;
    }
    
    void searchArray(int myArray[], const int n)
    {
    	int d, e, f;
    	bool found = false;
    
    	cout << "Enter a value to search for: ";
    	cin >> d;
    
    	{
    		for(e=0; e<=n; e++)
    		{
    			if(myArray[e] == d)
    			{
    				f = d;
    				found = true;
    				cout << "I found " << f << " at position " << e << " of the array." << endl <<endl;
    				break;
    			}
    		}
    	}
    
    	if(!found) cout << d << " was not found in the array." << endl;
    }
    		
    
    int main()
    {
    	#define SIZE 9
    	int myArray[SIZE];
    
    	takeValues(myArray, SIZE);
    	displayValues(myArray, SIZE);
    	findMinMax(myArray, SIZE);
    	searchArray(myArray, SIZE);
    }
    From doing some research on the net I'm guessing the array is going out of bounds somewhere, but I can't figure out where.

    Many thanks.

  2. #2
    Join Date
    May 2010
    Location
    Glasgow, Scotland
    Posts
    9

    Re: Stack around the variable '<array name>' was corrupted

    Sorry, should have added I'm using Visual C++ Express 2010 on Windows XP SP3

  3. #3
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: Stack around the variable '<array name>' was corrupted

    if you declare an array:

    Code:
    int myArray[9];
    Then the only valid indices are : 0,1,2,...,8 (not 9 like you are using).

  4. #4
    Join Date
    May 2010
    Location
    Glasgow, Scotland
    Posts
    9

    Re: Stack around the variable '<array name>' was corrupted

    But I'm using 9 because the assignment asks for an array of 10 numbers?

    Also I removed the variable f and the line f = d; from the searchArray() function as they were serving no purpose, but obviously the error/problem is the same.

  5. #5
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Stack around the variable '<array name>' was corrupted

    Code:
    for(a=0; a<=SIZE; a++)
    That should be

    Code:
    for(a=0; a<SIZE; a++)
    If your array as a size of 10, then the last index is 9.
    Quote Originally Posted by gers1978 View Post
    But why does my first program run perfectly? It also has SIZE set to 9, the only difference is the array is global?
    Why did your first program run and not the second? I have no idea, and quite frankly, I wouldn't bother trying to figure it out. By writing/reading out of bounds, anything can happen. This is the beauty of "undefined behavior".

    Fix this in both the first and second program, and everything should be fine.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  6. #6
    Join Date
    May 2010
    Location
    Glasgow, Scotland
    Posts
    9

    Re: Stack around the variable '<array name>' was corrupted

    Ok, actually I see what you're saying, but why does my first program run perfectly? It also has SIZE set to 9, the only difference is the array is global?

  7. #7
    Join Date
    May 2010
    Location
    Glasgow, Scotland
    Posts
    9

    Re: Stack around the variable '<array name>' was corrupted

    Thanks a lot, I actually fixed it myself with the exact change you suggested. I made SIZE = 10, then used a<SIZE instead of <=SIZE in all cases.

    As I'm new to this I'm very keen to learn WHY things happen as well, but I take your point, it's maybe not worth trying to figure out why prog1 doesn't give a runtime error but prog2 does...

    Thanks again

  8. #8
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Stack around the variable '<array name>' was corrupted

    If you want my guess, global objects and local objects are not created in the same "space" (since locals can only be seen in a closed scope, and constantly built and destroyed. Globals are visible by everyone, and are all constructed/destroyed on program launch/close).

    This could be the reason you get a different behavior.

    But explaining why you get which behavior in which case is anybody's guess.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  9. #9
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Stack around the variable '<array name>' was corrupted

    It's easier for the compiler to add runtime checks for stack corruption than general corruption.

    Of course, both are optional; the compiler is under no obligation to tell you that you did something bad. It may simply crash, or worse, might work fine despite the problem.

  10. #10
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Stack around the variable '<array name>' was corrupted

    And just so you know, these kinds of check are usually much more frequent in debug builds than production builds (for obvious reasons).

    This is why you usually want to code in debug.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured