Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Here's a new error 2

Status
Not open for further replies.

Chemakill

Programmer
Aug 17, 2000
37
CA
I have a simple program. It creates two arrays of random numbers (parallel) and then runs them through bubblesort and minsort functions. The problem is, halfway through the minsort function, it gets a very strange error popup box that states something along the lines of declaring and referencing a function using different techniques... I'm not sure on the exact wording, but my next post will contain the program. Please run it and tell me what you think the problem is.
 
Give a short sample please. John Fill
1c.bmp


ivfmd@mail.md
 
//This program uses the MinSort and BubbleSort algorithms.

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

void Randomizer();
void BubbleSort(short[], short[]);
void MinSort(short[], short[]);

void main()
{
printf(&quot;****\nMain\n****\n\n&quot;);


Randomizer();

}

void Randomizer()
{
printf(&quot;**********\nRandomizer\n**********\n\n&quot;);

short array1[10000], array2[10000];

short y, x = 10000;

for(y=0;y<x;y++)
{
array1[y] = rand();
array2[y] = rand();
}

BubbleSort(array1, array2);

MinSort(array1, array2);
}

void BubbleSort(short bubarray1[],short bubarray2[])
{
printf(&quot;**********\nBubbleSort\n**********\n\n&quot;);

short outer, inner, temp1, temp2, end=10000;

for(outer=0; outer<end; outer++)
{
for(inner=outer+1; inner>0; inner--)
{
if(bubarray1[inner]<bubarray1[inner-1])
{
temp1 = bubarray1[inner];

bubarray1[inner] = bubarray1[inner-1];

bubarray1[inner-1] = temp1;
}

if(bubarray2[inner]<bubarray2[inner-1])
{
temp2 = bubarray2[inner];

bubarray2[inner] = array2[inner-1];

bubarray2[inner-1] = temp2;
}
}
}
}

void MinSort(short minarray1[],short minarray2[])
{
printf(&quot;*******\nMinSort\n*******\n\n&quot;);


int temp_index1=1, temp_index2=1, min1, min2, x=10000, y, z;

for(y=0; y<x; y++)
{
min1 = minarray1[y];
min2 = minarray2[y];

for(z=y; z<x; z++)
{
if(minarray1[z]<min1)
{
temp_index1 = z;
min1 = minarray1[z];
//oddly, it always craps out before printing this statement.
printf(&quot;minarray1 switch\n&quot;);
}

if(minarray2[z]<min1)
{
temp_index2 = z;
min2 = minarray2[z];
}
}

minarray1[temp_index1] = minarray1[y];
minarray1[y] = min1;

minarray2[temp_index2] = minarray2[y];
minarray2[y] = min1;
}

}
 
uhmm... I think the error may have more to do with the program as a whole, and if I send you a code snippet, you'll code the rest and end up with something completely different. It might work. So there it is. Please copy and paste into a compiler. It runs on its own and should give the error shortly after entering Minsort.
 
The problem is in BudbleSort.
//look here
for(inner=outer+1; inner>0; inner--)
// what should be
for(inner=outer; inner>0; inner--) John Fill
1c.bmp


ivfmd@mail.md
 
Okay... tried that and it didn't solve the error. All that actually does is force on more iteration, so it's a little less efficient, but that causes no other problems. Also, the arrays should be randomized again between bubblesort and minsort. I've done that as well, and it solves nothing. I won't repost the whole code, but this
for(y=0;y<x;y++)
{
array1[y] = rand();
array2[y] = rand();
}

BubbleSort(array1, array2);

MinSort(array1, array2);

Should be
for(y=0;y<x;y++)
{
array1[y] = rand();
array2[y] = rand();
}

BubbleSort(array1, array2);

for(y=0;y<x;y++)
{
array1[y] = rand();
array2[y] = rand();
}

MinSort(array1, array2);

. Of course, the strange ESP error still occurs. Does anyone else know what I'm talking about or is this my compiler/computer at work?
 
I think you should be more attentive. There are at least two logical errors. Follow theese bugs in function MinSort:

if(minarray2[z]<min1)/*error in current line*/
{
/**********first bug************************
* here instead of min1 you should put min2 *
********************************************/
....code here
}
}
.....code here
minarray2[y] = min1;/*error in current line*/
/*********second bug************************
* here instead of min1 you should put min2 *
********************************************/
}
} John Fill
1c.bmp


ivfmd@mail.md
 
Okay, been hammering at it for a couple days now. Worked out all the sorting errors, such as the ones mentioned in your last post, however, the error still comes up. Troubleshooting has confirmed that minsort DOES finish sorting, and the program DOES return to FileAccess, and FileAccess finishes, and the program DOES return to main(). I've placed a printf at the end of main JUST before the final } and received output there.

As well, the error is NOT in the sorts, as I have ran the program with their function calls commented out. I tried commenting out the FileAccess function, and got rid of the error. The error is in file access. But where?

As soon as the program tries to end it generates the following error box:
>>
Microsoft Visual C++ Debug Library

Debug Error!

Program: F:\STUDENT\98119\CO40\DEBUG\SORTER.EXE
Module:
File: i386\chkesp.c
Line: 42

The value of ESP was not properly saved after a function call. This is usually the result of calling a function with one calling convention with a function pointer declared with a different calling convention.

(Press Retry to debug the application)

Abort Retry Ignore
<<

If I click Abort, I get the please press any key to continue line in the DOS box, if I click Retry Windows pauses, then gives me the standard windows &quot;program has messed up. Close, Debug, Details&quot;, as does Ignore.
 
If you are so positive about where the error is:
1. comment please the calling of BubleSort function, and the error will disappear.
2. You can also stop looping and not comment calling of bublesort:
instead of
for(inner=outer+1; inner>0; inner--)
{....
you'll write
if(0)for(inner=outer+1; inner>0; inner--)
{....
3. Just correct the bugs from the first post and from the second, and everything will be ok. If you don't believe, try and see.
John Fill
1c.bmp


ivfmd@mail.md
 
Does commenting BubbleSort work on your machine? Doesn't on mine. I narrowed down the problem further. I think it may be a computer specific error, such as memory management problems. It isn't in either of the sorts though, not here anyways. I'll try to get around to testing it on a different machine. As well, I've fixed most of the errors you've found, but I'd rather not post the entire code each time I change something. Anyhow, thanks for your time, and I believe I am finished with this issue.
 
Do you know what is it to comment? Look at this peace of code:
array1[y] = rand();
array2[y] = rand();
}
BubbleSort(array1, array2);
MinSort(array1, array2);
}
Commenting calling of buble sort means to put two slashes:

array1[y] = rand();
array2[y] = rand();
}
//BubbleSort(array1, array2);
//now calling of BubleSort is commented.
//Compiller will ignore this line.
MinSort(array1, array2);
}

Also you can put if(0) instead of a comment:
array1[y] = rand();
array2[y] = rand();
}
if(0)BubbleSort(array1, array2);
//Now compiller will not ignore, but the condition is
//always false and BubleSort will not call.
MinSort(array1, array2);
}

Also is very good you to be able to use debugger. Instead of put printf(...) at the end of main() before return, just debug code. Put the cursor on return and click RunToCursor in the debugger. Is much easier. Also insted of getch() for debugging use StepOver, StepInto, StepOut(or some think like it).

By the way, after I've selected/copy/pasted your code on my computer, I compilled it ang got the same errors you notified here. After some debugging/testing, I found quickly the bugs. If you correct the bugs I notified, you wouldn't have any problems. John Fill
1c.bmp


ivfmd@mail.md
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top