Thread: Error on close.

  1. #1
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718

    Exclamation Error on close.

    Does anyone know why I get the following error when my program finishes (using VC++6)?

    Debug Error!

    Program: <path to my exe>
    Module:
    File: i386\chkesp.c
    Line: 42

    The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.
    Here's my code:

    Code:
    #include <iostream.h>
    #include <conio.h>
    #include <cstring>
    
    #define PlainText "This is a test..."
    
    void SortArray(long *InArray);
    void GetCharCounts(char *TheString);
    void SpitOutSorted(int UniqueChars);
    
    struct SortedChars
    {
    	char Character;
    	long CharCount;
    };
    
    SortedChars FinalArray[255];
    unsigned __int8 TotalChars;
    
    void SortArray(long *InArray)
    {
    	bool Sorted = false;
    	int StartIndex = -1;
    	int Highest = 0;
    	cout<<"Sorting character list...";
    	while (!Sorted)
    	{
    		Highest = StartIndex;
    		for (int CurIndex = StartIndex + 1; CurIndex <= 255; CurIndex++)
    		{
    			if((InArray[Highest] > InArray[CurIndex] || InArray[Highest] <= 0) && InArray[CurIndex] > 0)
    			{
    				Highest = CurIndex;
    			}
    			if(InArray[Highest] == 1)
    			{
    				break;
    			}
    		}
    		if(Highest == StartIndex)
    		{
    			Sorted = true;
    			break;
    		}
    		FinalArray[StartIndex + 1].CharCount = InArray[Highest];
    		FinalArray[StartIndex + 1].Character = Highest;
    		InArray[Highest] = 0;
    		StartIndex++;
    	}
    	cout<<(char)9<<"Done"<<endl;
    }
    
    void GetCharCounts(char *TheString)
    {
    	long CharCounts[255];
    	int UniqueChars = 0;
    	cout<<"Counting unique characters...";
    	for(int i = 0; i <= 255; i++)
    	{
    		CharCounts[i] = 0;
    	}
    	for(unsigned int CurChar = 0; CurChar < strlen(TheString) ; CurChar++)
    	{
    		if (CharCounts[TheString[CurChar]] == 0)
    		{
    			UniqueChars++;
    		}
    		CharCounts[TheString[CurChar]]++;
    	}	
    	cout<<(char)9<<UniqueChars<<" unique characters found."<<endl;
    	SortArray(CharCounts);	
    }
    
    void SpitOutSorted(int UniqueChars)
    {
    	for (int i = 0; i < UniqueChars; i++)
    	{
    		cout<<FinalArray[i].Character<<(char)9<<(int)FinalArray[i].Character<<(char)9<<FinalArray[i].CharCount<<endl;
    	}
    }
    
    int main()
    {		
    	cout<<"The string which will be encoded is:"<<endl<<PlainText<<endl;
    	GetCharCounts(PlainText);	
    	getch();	
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    Strange problem, certainly.

    I've compiled, and run, your code on VC++6, Borland C++Builder4 and Borland C++Builder 5.

    I received the same error message that you did (after the fact, so to speak) on VC++6...after what appears to be a successful run.

    However, I did not get any such error running on Borland. I even added some code, following the function call in main() to 'GetCharCounts(PlainText)', and found that VC++6 never processes my code while Borland does, in both cases.

    With no hard assumptions being made, I would have to believe that this is specific to the compiler and not an error in your code.

    Last edited by skipper; 07-03-2002 at 05:44 PM.
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Figured it out. In the GetCharCounts function, when it loops through the CharCounts array and set each element to 0, it seemed to be looking for the index 256 in the array, even though it is only supposed to go up to 255. Once I resized the array to 'long CharCounts[256];', it worked fine.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Close an HTTPListenerResponse.OutputStreram
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 04-23-2008, 11:00 PM
  2. User input to close window
    By NoFearXD in forum Windows Programming
    Replies: 2
    Last Post: 05-09-2007, 07:38 PM
  3. open() and close()
    By SoFarAway in forum C Programming
    Replies: 3
    Last Post: 04-08-2005, 01:16 PM
  4. XWindows- Close window event?
    By Exile in forum Linux Programming
    Replies: 8
    Last Post: 01-09-2005, 10:39 PM
  5. Ghost in the CD Drive
    By Natase in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 10-12-2001, 05:38 PM