hi i compile this code but it works but i have as result the error message "Memory is insufficient! "

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



using namespace std; 

int i ,j , l , k ,x,y ;
float b;
const int maxElements = 15000;
float **Bt , **Btc ;
float **M , **MC;

string  **T , **TC;


int main()

{
	 using namespace PLib ; 
	 NurbsSurface<float,3> surf ;


ifstream file("visage.txt");

Bt = new float* [maxElements];
T = new string*[maxElements];
string word;
i=0;
 while( file >> word) 

 {
	 word.size();
	 T[i]= new string[word.size()];
	 *(T[i])= word;

		i=i+1;

 }
  
 for (j=0;j<i;j++)
  { Bt[j] = new float [100];
 *Bt[j] = atof((T[j])->c_str()); 
	
  }
 for (j=0;j<i;++j)
	  delete[] T[j];
  delete[] T;

   l=0;
   x=i/3;
   M = new float* [x];
   for(  j=0 ; j < x ; j++ )
   {M[j] = new float [3];}
   for (j=0;j<x;j++)
	{
		M[j][0]=*Bt[l];
		M[j][1]=*Bt[l+1];
		M[j][2]=*Bt[l+2];
		l=l+3;

	}
   for (j=0;j<i;++j)
	  delete[] Bt[j];
  delete[] Bt;
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];

		}
	for (j=0;j<x;++j)
	  delete[] M[j];
  delete[] M;
Btc = new float* [maxElements];
TC = new string*[maxElements];
std::ifstream fileuv("a1.txt");
std::string worduv;
i=0;
while( fileuv >> worduv ) 
 {
	 worduv.size();
	 TC[i]= new string[worduv.size()];
	 *(TC[i])= worduv;

		i=i+1; 
 }
for (j=0;j<i;j++)
  { Btc[j] = new float [100];
 *Btc[j] = atof((TC[j])->c_str()); 
	
  }
for (j=0;j<i;++j)
	  delete[] TC[j];
  delete[] TC;

   l=0;
   y=i/2;
    
   MC = new float* [y];
   
   for(  j=0 ; j < y ; j++ )
   {MC[j] = new float [2*sizeof(float)];}
        for (j=0;j<y;j++)
		{
		MC[j][0]=*Btc[l];
		MC[j][1]=*Btc[l+1];

		l=l+2;
		}

   for (j=0;j<i;++j)
	  delete[] Btc[j];
  delete[] Btc;
  Vector<float> uk(8000) ,vk(8000);
k=0;
	for (j=0;j<y;j++)
	{
		vk[j]=MC[j][1];
		uk[j]=MC[j][0];
		k=k+1;
         
	}
	 for (j=0;j<y;++j)
	  delete[] MC[j];
  delete[] MC;

surf.U=uk;
surf.V=vk;
surf.knotU()[1];
  try {
    surf.leastSquares(Pts, 3, 3, 7120, 7120);
  } catch(std::bad_alloc& e) {
    std::cout << "Memory is insufficient!" << std::endl;
    return 1;
  };
 return 0;
}

but i think it's not a memory problem how can solve this problem

Recommended Answers

All 5 Replies

Did you calculate the amount of memory that program takes? 15000*100*sizeof(float) is the number of bytes need for just one of those arrays.

but i deallocate it after use it so the memory become free no ?

hi replace

Btc[j] = new float [100];

with

Btc[j] = new float [1];

and it doesn't work could any one know the problem

Step 1 is learn how to indent code (or at least post indented code on a forum). Your smart IDE editor might be able to make sense of it, but on a forum, it looks like dog food.

Here is your code, courtesy of indent -kr -nut -ts2 -i2 foo.cpp I haven't checked it still compiles, but it looks a lot better.

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <math.h>
#include <nurbsS.h>
#include <nurbsSub.h>
using namespace std;

int i, j, l, k, x, y;
float b;
const int maxElements = 15000;
float **Bt, **Btc;
float **M, **MC;
string **T, **TC;

int main()
{
  using namespace PLib;
  NurbsSurface < float, 3 > surf;

  ifstream file("visage.txt");

  Bt = new float *[maxElements];
  T = new string *[maxElements];
  string word;
  i = 0;
  while (file >> word)
  {
    word.size();
    T[i] = new string[word.size()];
    *(T[i]) = word;
    i = i + 1;
  }

  for (j = 0; j < i; j++) {
    Bt[j] = new float[100];
    *Bt[j] = atof((T[j])->c_str());
  }

  for (j = 0; j < i; ++j)
    delete[]T[j];
  delete[]T;

  l = 0;
  x = i / 3;
  M = new float *[x];
  for (j = 0; j < x; j++) {
    M[j] = new float[3];
  }

  for (j = 0; j < x; j++) {
    M[j][0] = *Bt[l];
    M[j][1] = *Bt[l + 1];
    M[j][2] = *Bt[l + 2];
    l = l + 3;
  }

  for (j = 0; j < i; ++j)
    delete[]Bt[j];
  delete[]Bt;

  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];

  }
  for (j = 0; j < x; ++j)
    delete[]M[j];
  delete[]M;

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

  for (j = 0; j < i; j++) {
    Btc[j] = new float[100];
    *Btc[j] = atof((TC[j])->c_str());
  }

  for (j = 0; j < i; ++j)
    delete[]TC[j];
  delete[]TC;

  l = 0;
  y = i / 2;

  MC = new float *[y];

  for (j = 0; j < y; j++) {
    MC[j] = new float[2 * sizeof(float)];
  }
  for (j = 0; j < y; j++) {
    MC[j][0] = *Btc[l];
    MC[j][1] = *Btc[l + 1];
    l = l + 2;
  }

  for (j = 0; j < i; ++j)
    delete[]Btc[j];
  delete[]Btc;

  Vector < float >uk(8000), vk(8000);
  k = 0;
  for (j = 0; j < y; j++) {
    vk[j] = MC[j][1];
    uk[j] = MC[j][0];
    k = k + 1;

  }
  for (j = 0; j < y; ++j)
    delete[]MC[j];
  delete[]MC;

  surf.U = uk;
  surf.V = vk;
  surf.knotU()[1];
  try {
    surf.leastSquares(Pts, 3, 3, 7120, 7120);
  }
  catch(std::bad_alloc & e) {
    std::cout << "Memory is insufficient!" << std::endl;
    return 1;
  };
  return 0;
}

Now look at these 3 lines, and tell me what they're doing.

word.size();  // does nothing
    T[i] = new string[word.size()];  // an array of strings, based on word length (?)
    *(T[i]) = word; // same as T[i][0] = word;

In other words, you allocate a whole bunch of strings which you never use.

Bt[j] = new float[100];
    *Bt[j] = atof((T[j])->c_str());  // same as Bt[j][0]

Here again, 99 out of 100 floats go unused.
No wonder you run out of memory.

Here are the first two steps, done somewhat differently.

$ cat foo.cpp
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;

const int maxElements = 15000;

int main()
{
  ifstream file("visage.txt");

  float *Bt = new float [maxElements];
  string *T = new string [maxElements];
  string word;
  int i = 0;
  while (i < maxElements && file >> word)
  {
    T[i] = word;
    i = i + 1;
  }

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

  delete [] T;
  delete [] Bt;
  return 0;
}
$ g++ foo.cpp
$ valgrind ./a.out
==2124== Memcheck, a memory error detector
==2124== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
==2124== Using Valgrind-3.6.0.SVN-Debian and LibVEX; rerun with -h for copyright info
==2124== Command: ./a.out
==2124== 
==2124== 
==2124== HEAP SUMMARY:
==2124==     in use at exit: 0 bytes in 0 blocks
==2124==   total heap usage: 43 allocs, 43 frees, 190,059 bytes allocated
==2124== 
==2124== All heap blocks were freed -- no leaks are possible
==2124== 
==2124== For counts of detected and suppressed errors, rerun with: -v
==2124== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 4 from 4)

Sorry for my bad writing code but I am beginner in c + + and i try to learn it step by step
for this 3 lines

word.size();  // does nothing
    T[i] = new string[word.size()];  // an array of strings, based on word length (?)
    *(T[i]) = word; // same as T[i][0] = word;

i try to fill the table T with word
and with this

Bt[j] = new float[100];
    *Bt[j] = atof((T[j])->c_str());  // same as Bt[j][0]

i replace 100 with 1
but i have the same problem

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.