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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

trouble with dinamic memory and class 2

Status
Not open for further replies.

rick05

Programmer
Feb 23, 2001
15
MX
Hi guys,

I have a little troblue, you will see, I have the next code:

-------------------
//here all includes...

struct parts
{
int etiqueta;
float X, Y, Z, Q, T;
};

main ....

//I reserve dinamic memory for a bidimensional matrix...

parts **vert;

//la linea siguiente es la buena

vert = new parts*[n_cuadros];

//par es numero de cuadros
int par;

//entrada is a filestream

for (par=0;par<n_cuadros;par++)
{
vert[par] = new parts[n_particulas];

cout << &quot;numero de cuadros: &quot; << par+1 << &quot;\n&quot;;

//i es el numero de particulas....
for (i=0; i<n_particulas; i++)
{
entrada >> vert[par].etiqueta;
entrada >> vert[par].X;
entrada >> vert[par].Y;
entrada >> vert[par].Z;
entrada >> vert[par].Q;
entrada >> vert[par].T;
}

for (int c=0;c<i; c++)
{
cout << vert[par][c].etiqueta<<&quot; &quot;;
cout << vert[par][c].X<<&quot; &quot;;
cout << vert[par][c].Y<<&quot; &quot;;
cout << vert[par][c].Z<<&quot; &quot;;
cout << vert[par][c].Q<<&quot; &quot;;
cout << vert[par][c].T<<&quot; \n&quot;;
}
}
cout << &quot;valor de par: &quot; << par << &quot;\n&quot;;

cout << &quot; valor de la vert[0][0].etiqueta:[&quot; << vert[0][0].etiqueta << &quot;]\n&quot;;
cout << &quot; valor de la vert[n_cuadros-1][5].etiqueta:[&quot; << vert[n_cuadros-1][5].etiqueta << &quot;]\n&quot;;

cout << &quot;valor de n_cuadros :&quot; << n_cuadros << &quot;\n&quot;;

//liberar la memoria reservada:
for(int libera=0;libera<n_cuadros;libera++)
delete [] vert[libera];
delete vert;

------------------

well, this code works alone, but if I try to putting into a class (like a method, such as &quot;read_file&quot;, for example), it brokes!! the &quot;for&quot; loop only works until 5 times, and then broke....

anybody can helpme?

I hope you can helpme please....

rick.


 
entrada >> vert[par]->etiqueta;

vert is of type part** you need to use a pointer again BUT this will require pointer arithmetic i belive. Use
vert[par][n_particulas].etiqueta

Dont know if this is the only problem though

Matt
 

Hi matt and all again...

happens that my program was mistaken, I don´t know how it happens....

the correct code is the next:
---------------------

// all the above-mentioned here is same

vert = new parts*[n_cuadros];

//par es numero de cuadros
int par;
for (par=0;par<n_cuadros;par++)
{

vert[par] = new parts[n_particulas];

cout << &quot;numero de cuadros: &quot; << par+1 << &quot;\n&quot;;

//i es el numero de particulas....
for (i=0; i<n_particulas; i++)
{

/*
this was writen wrong, I had a mistaken
entrada >> vert[par].etiqueta;
entrada >> vert[par].X;
entrada >> vert[par].Y;
entrada >> vert[par].Z;
entrada >> vert[par].Q;
entrada >> vert[par].T;
*/

// the next is correct
entrada >> vert[par].etiqueta;
entrada >> vert[par].X;
entrada >> vert[par].Y;
entrada >> vert[par].Z;
entrada >> vert[par].Q;
entrada >> vert[par].T;
}

for (int c=0;c<i; c++)
{
cout << vert[par][c].etiqueta<<&quot; &quot;;
cout << vert[par][c].X<<&quot; &quot;;
cout << vert[par][c].Y<<&quot; &quot;;
cout << vert[par][c].Z<<&quot; &quot;;
cout << vert[par][c].Q<<&quot; &quot;;
cout << vert[par][c].T<<&quot; \n&quot;;
}
}
cout << &quot;valor de par: &quot; << par << &quot;\n&quot;;

cout << &quot; valor de la vert[0][0].etiqueta:[&quot; << vert[0][0].etiqueta << &quot;]\n&quot;;
cout << &quot; valor de la vert[n_cuadros-1][5].etiqueta:[&quot; << vert[n_cuadros-1][5].etiqueta << &quot;]\n&quot;;

cout << &quot;valor de n_cuadros :&quot; << n_cuadros << &quot;\n&quot;;

//liberar la memoria reservada:
for(int libera=0;libera<n_cuadros;libera++)
delete [] vert[libera];
delete vert;

--------------------------------

however the trouble is the same, it does´t work like a method of a class, if I try it alone, like a program, it works fine, but if I try it into a method it brokes in 5th or 4th iteration....

even I´ve tried with a &quot;while loop&quot; and I got the same...

mmm, also I use a &quot;for&quot; step by step up to 7x8 iterations, I say:

vert[0] = new parts[8];

entrada >> vert[0][0].etiqueta;
entrada >> vert[0][0].X;
entrada >> vert[0][0].Y;
entrada >> vert[0][0].Z;
entrada >> vert[0][0].Q;
entrada >> vert[0][0].T;

entrada >> vert[0][1].etiqueta;
entrada >> vert[0][1].X;
entrada >> vert[0][1].Y;
entrada >> vert[0][1].Z;
entrada >> vert[0][1].Q;
entrada >> vert[0][1].T;

...................
...................

// until:

vert[6] = new parts[8];

entrada >> vert[6][0].etiqueta;
entrada >> vert[6][0].X;
entrada >> vert[6][0].Y;
entrada >> vert[6][0].Z;
entrada >> vert[6][0].Q;
entrada >> vert[6][0].T;

...................
...................


entrada >> vert[6][7].etiqueta;
entrada >> vert[6][7].X;
entrada >> vert[6][7].Y;
entrada >> vert[6][7].Z;
entrada >> vert[6][7].Q;
entrada >> vert[6][7].T;

I mean one by one! and it works, but If I use a loop it brokes!

thx again

I broke mi head with this, helpme please...!!!

rick


 
Sorry it took me so long to respond. I lost internet access to tek-tips on monday and was not in on tuesday. If you could send me a snip of the code where you are having the problem I will see what I can find.

Matt
zyrenthian@home.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top