Friday, July 31, 2009

Why does the program crash when it enters a function?

Hi, I am writing a C++ program. I have found out that an unhandled exception message will pop out whenever the code enters a function.





I have used breakpoints and cout statements to determine the point of the crash.





The function has 4 parameters. 1 int variable, 1 int array and 2 double arrays. "Tuples" and "Features" are constants representing values of 1600 and 46 respectively.





int rank(int num,int data[Tuples][Features],


double dist[Tuples],double rank[Tuples])


{ ..........


...........}





The program crashes after the first opening bracket. '{'. None of the statements inside the function are executed.





I don't know what is going on. How come the program will crash without even executing a statement?

Why does the program crash when it enters a function?
Yes, you should give the text of the exception. However, I wonder if rank() is a method of an object. If so, it could be that you forgot to instantiate the object before calling rank(). This isn't always bad (and there are static methods, of course), but depending on the rest of your code, it might explain things.
Reply:You should not be passing the arrays into the function. I suspect there is a memory error.





You can still access your memory using data[][], but your function should look like this int rank(int num,int *data,


double *dist,double *rank)





Keep in mind that the person that calls your function will have to create the memory.
Reply:Your function prototype is the problem.


I suspect you are passing values of int and double and expecting arrays for the last three parameters.





Convetr the prototype to pass and expect arrays by having int* and double*
Reply:An unhandled exception means that something can go wrong and you are not handling it.





If you get the details of the unhandled exception to learn what could go wrong that you aren't taking care of then you'll have an idea of how to take care of the problem.
Reply:I think the problem is the same name of the function and one array ie 'rank'.


Try changing it.


No comments:

Post a Comment