I was trying to write a program .
But when i run it i have this error message

this application has requested the runtime to terminate it in an unusual way

Recommended Answers

All 10 Replies

Ok... please post the relevant code. Have you tried to figure out the exact point where the program crashes?

this is my code

#include <iostream>
#include <fstream>
#include <string>
#include <nurbsS.h>
#include <nurbsSub.h>
#include <math.h>

using namespace std; 

int i ,j , l , k ,x , nU , nV;
float b;
float Bt[10000] , Btc[10000];
float M[10000][3];
float Mc[10000][2];
string T[10000];
string TC[100000];

int main()
{
  using namespace PLib ; 

 std::ifstream file("visage.txt");
 std::string word;
i=0;
 while( file >> word ) 
 {
T[i]=word;
//std::cout << T[i] << '\n';
i=i+1;

 }
// cout<<"la valeur de i"<<i<<endl;
 /* for (j=0;j<i;j++)
 {
	 if(T[j].compare(T[j].length()-1,1,",")==0)

		T[j].erase(T[j].length()-1);

	cout<<"fin   "<<T[j]<<endl;
 
 }
 */
 
  /*Bt[0]= atof(T[0].c_str());
  cout<<"convert"<<Bt[0]<<endl;*/

   for (j=0;j<i;j++)
  {
	Bt[j] = atof(T[j].c_str()); 
	//cout<<"convert  "<<j<<"   "<<Bt[j]<<endl;

  }
   l=0;
   k=0;
  x=i/3;
  cout<<"i=   "<<i<<endl;
  cout<<"x=   "<<x<<endl;
for (j=0;j<x;j++)
{
M[j][k]=Bt[l];
M[j][k+1]=Bt[l+1];
M[j][k+2]=Bt[l+1];
//cout<<"matrice  "<<M[j][k+1]<<endl;
k=0;
l=l+3;

}
//cout<<"hello"<<endl;
Matrix<Point_nD<float,3>>  Pts(x,3) ;
for (j=0;j<x;++j)
{
//cout<<"hello"<<endl;
Pts(j,0)=M[j][0];
Pts(j,1)=M[j][1];
Pts(j,2)=M[j][2];

/*Pts(j,0).x()=M[j][0];
Pts(j,0).y()=M[j][1];
Pts(j,0).z()=M[j][2];*/
//cout<<"hello"<<endl;
}
cout<<"point   "<<"j  ="<<j<<Pts(0,0)<<endl;

std::ifstream fileuv("a1.txt");
 std::string worduv;
i=0;
 while( fileuv >> worduv ) 
 {
TC[i]=worduv;
//std::cout << TC[i] << '\n';
i=i+1;

 }
  for (j=0;j<i;++j)
  {
	//cout<<"hello"<<'\n';
	  Btc[j] = atof(TC[j].c_str()); 

	//cout<<"convert  "<<j<<"   "<<Btc[j]<<endl;

  }
   l=0;
   k=0;
  x=i/2;
  for (j=0;j<x;j++)
{
Mc[j][0]=Btc[l];
Mc[j][1]=Btc[l+1];

//cout<<"matrice  "<<Mc[j][0]<<endl;
//k=0;
l=l+2;
}
//cout<<"matrice  "<<Mc[x-1][0]<<endl;


Vector<float> uk[8000] ,vk[8000];

//cout<<"size  "<<uk.n()<<endl;


cout<<"x    "<<x<<endl;
for (j=0;j<x;j++)
{
	//cout<<j<<endl;

	//for(k=0;k<j+1;k++)
	//{
			vk[j]=Mc[j][1];
			uk[j]=Mc[j][0];
          //  cout<<Mc[j][1]<<endl;


	//	//}

}

cout<<uk[100]<<endl;

//cout<<vk<<endl;
//nU=uk.size();
//cout<<"aaaaaaa"<<nU<<endl;

NurbsSurface<float,3> surf ;

 surf.leastSquares(Pts ,3,3,7120,7120) ; 
 /*cout<<"hello"<<endl;
cout<<surf.degreeU()<<endl;
surf.ctrlPnts*/

return 0;
}

the error start when i write the function surf

You are using a large amount of stack-based memory (static arrays) and I wouldn't be surprised if the leastSquares method did as well, given the sizes you provide (7120). The "abnormal termination" failure is usually due to stack-overflow, meaning that your program runs out of stack-memory to store the local variables of the functions, in which case, all the OS can do is terminate abruptly. Either find a way to avoid using so much memory (by breaking up the problem, or by using dynamically allocated memory instead), or increase the stack-size for the program (check your compiler's documentation to know how to specify a stack-size that is greater than the default size).

Well it would help if you could post code with a decent approach to indentation

It also helps to remove all the commented out code you accumulated, at least for the purposes of posting it on a forum.

Something like this.

#include <iostream>
#include <fstream>
#include <string>
#include <nurbsS.h>
#include <nurbsSub.h>
#include <math.h>

using namespace std;

int i, j, l, k, x, nU, nV;
float b;
float Bt[10000], Btc[10000];
float M[10000][3];
float Mc[10000][2];
string T[10000];
string TC[100000];

int main()
{
  using namespace PLib;

  std::ifstream file("visage.txt");
  std::string word;
  i = 0;
  while (file >> word) {
    T[i] = word;
    i = i + 1;
  }

  for (j = 0; j < i; j++) {
    Bt[j] = atof(T[j].c_str());
  }

  l = 0;
  k = 0;
  x = i / 3;
  cout << "i=   " << i << endl;
  cout << "x=   " << x << endl;
  for (j = 0; j < x; j++) {
    M[j][k] = Bt[l];
    M[j][k + 1] = Bt[l + 1];
    M[j][k + 2] = Bt[l + 1];
    k = 0;
    l = l + 3;
  }

  Matrix < Point_nD < float, 3 > > Pts(x, 3);
  for (j = 0; j < x; ++j) {
    Pts(j, 0) = M[j][0];
    Pts(j, 1) = M[j][1];
    Pts(j, 2) = M[j][2];
  }
  cout << "point   " << "j  =" << j << Pts(0, 0) << endl;

  std::ifstream fileuv("a1.txt");
  std::string worduv;
  i = 0;
  while (fileuv >> worduv) {
    TC[i] = worduv;
    i = i + 1;
  }

  for (j = 0; j < i; ++j) {
    Btc[j] = atof(TC[j].c_str());
  }

  l = 0;
  k = 0;
  x = i / 2;
  for (j = 0; j < x; j++) {
    Mc[j][0] = Btc[l];
    Mc[j][1] = Btc[l + 1];
    l = l + 2;
  }

  Vector < float >uk[8000], vk[8000];

  cout << "x    " << x << endl;
  for (j = 0; j < x; j++) {
    vk[j] = Mc[j][1];
    uk[j] = Mc[j][0];
  }

  cout << uk[100] << endl;

  NurbsSurface < float, 3 > surf;

  surf.leastSquares(Pts, 3, 3, 7120, 7120);

  return 0;
}

> You are using a large amount of stack-based memory (static arrays)
The only conspicuously large thing on the stack would be Vector < float >uk[8000], vk[8000]; The rest are global variables.
All the single letter variables should be local to main, and some could do with better (more descriptive) names as well.

The first sign of trouble is going to be

while (file >> word) {
    T[i] = word;
    i = i + 1;
  }

which should really guard against reading long files, with say

while (i < 10000 && file >> word) {
    T[i] = word;
    i = i + 1;
  }

And please declare a constant for all those 10000 values, like say

const int maxElements = 10000;
float Bt[maxElements], Btc[maxElements];
float M[maxElements][3];
float Mc[maxElements][2];
string T[maxElements];
string TC[maxElements];

Though to be honest, changing all these arrays to std::vector would be a good idea as well.

Agreed, we aren't in the stone ages of C programming--use vectors. The "app terminated in unusual way" may also be an uncaught exception, from my experiences.

the error message starts when I insert the function surf.leastsquares which is a function of the bookstore nurbs++
I am beginner in c + + so I wanna know how can i fix this problem: this function has two local attributes which I must also define how can i define
this code represent the function

void NurbsSurface<T,N>::leastSquares(const Matrix< Point_nD<T,N> >& Q, int pU, int pV, int nU, int nV){
  Vector<T> vk,uk ;

  resize(nU,nV,pU,pV) ;
  cout<<"hello"<<endl;

  surfMeshParams(Q,uk,vk) ;

  Vector< HPoint_nD<T,N> > Pts(Q.rows()) ;
  NurbsCurve<T,N> R ;
  
  int i,j ;

  Matrix< HPoint_nD<T,N> > P2 ;

  P2.resize(nU,Q.cols()) ;

  for(j=0;j<Q.cols();j++){
    for(i=0;i<Q.rows();i++)
      Pts[i] = Q(i,j) ;
    R.leastSquaresH(Pts,pU,nU,uk);
    for(i=0;i<P.rows();i++)
      P2(i,j) = R.ctrlPnts(i) ;
    if(j==0)
      U = R.knot() ;
  }

With the parameters that you gave, this function will require more than 4 GB of contiguous memory to run. My guess is that you run out of memory. To test if this is the case, you should try this in your main function, instead of the original call to leastSquares:

try {
    surf.leastSquares(Pts, 3, 3, 7120, 7120);
  } catch(std::bad_alloc& e) {
    std::cout << "Memory is insufficient!" << std::endl;
    return 1;
  };

I try this

try {
    surf.leastSquares(Pts, 3, 3, 7120, 7120);
  } catch(std::bad_alloc& e) {
    std::cout << "Memory is insufficient!" << std::endl;
    return 1;
  };

it returns me the error message "Memory is insufficient!" how can i fix it ?

>>"Memory is insufficient!" how can i fix it ?

Either get a computer with more memory (RAM) or figure out a way to solve your problem with smaller data-sets, like sub-matrices. You could split the problem into smaller matrices and solve the leastSquare problem for each smaller matrix. There is no easy solution to this.

hi !
how can i replace static table with a dynamic pointer ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.