Run time error. poo

hI, I am starting with object oriented programming and i am having a problem that i do not know how to solve it. I am "translating" from java to c++. (assigment). The problem is that it works up to the last line in the TRY block in the main funtion.The error says:

"Thsi application has requested, the runtime to terminate it in an unusual way."

I am using exceptions and I have to have the constructor for the eception (pilallenaexception and pilavaciaexception...)

it is suppoused to be "translated" from this:

"package Utiles;
public class PilaVaciaException extends Exception{
public PilaVaciaException(){
super();
}
}

package Utiles;
public class PilaLlenaException extends Exception {
private int dato;
public PilaLlenaException(int dato){
super();
this.dato = dato;
}
public int getDato() {
return dato;
}
}"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <iostream.h>
#include <conio.h>
#include <exception>

using namespace std;

class pilallenaexception : public exception{
      private:
              int dato;
      public:
             pilallenaexception(int){
                                    
                                    };
             int getdato () {
                 return (dato);
                 }
      };

class pilavaciaexception : public exception{
      private:
              int dato;
      public:
             pilavaciaexception(){
                                    
                                  };
      };

class pila {
      private:
              int capacidad, indice;    // son los datos que pertencen a el objeto de la clase "pila"
              int *datos;              // no se por que se utiliza *
      public:
             pila (int);               //constructor (prototipo)
             pila ();                  //CONSTRUCTOR predeterminado (prototipo)
             ~pila(){                  //destructor //elimina cada componente de la clase
                     delete &capacidad;
                     delete &indice;
                     delete &datos;
                     };        
             void vaciar(){ indice=0;};
             bool estaVacia(){
             return (indice == 0);
                    };
             bool estaLlena(){
             return (indice == capacidad);
                       };
/////////////////////////////////////////////////////////////////////////////////////////////////////////7

              void  mostrar()throw (pilavaciaexception) {
                    if(!estaVacia()){
                                     for(int i=indice-1;i>=0;i--)
                                     cout<<"\nelem = "<<datos[i];
                                     }
                    else throw new pilavaciaexception();
              }
              int desapilar() throw (pilavaciaexception){
                    if(!estaVacia()) return (datos[--indice]);
                    else throw new pilavaciaexception();
              }
              void apilar(int elem) throw (pilallenaexception){
                   if(!estaLlena()) datos[indice++]=elem;
                   else throw new pilallenaexception(elem);
              }
              int cima() throw (pilavaciaexception){
                  if(!estaVacia()) return datos[indice-1];
                  else throw new pilavaciaexception();
              }                                                                                                      

//////////////////////////////////////////////////////////////////////////////////////////////////////////             
             
      };

pila::pila (){                           //constructor (inicializa los valores por default)
           capacidad=10; indice=0;
           datos = new int [capacidad];
           }
pila::pila (int cap){                  //constructor con una capacidad indicada
           capacidad=cap; indice=0;
           datos = new int [capacidad];
           }


int main(int argc, char *argv[])
{
    /////////////////////////////////////////////////////////////////////////////////////////
           int aux;
        pila pila(10);
       try{
         aux=25; cout<<"apilando elem "<<aux<<endl;
            pila.apilar(aux);
             aux=-45; cout<<"apilando elem "<<aux<<endl;
            pila.apilar(aux);
            aux=-12; cout<<"apilando elem "<<aux<<endl;
            pila.apilar(aux);
             aux=67; cout<<"apilando elem "<<aux<<endl;
            pila.apilar(aux);
            aux=1; cout<<"apilando elem "<<aux<<endl;
            pila.apilar(aux);
             aux=0; cout<<"apilando elem "<<aux<<endl;
            pila.apilar(aux);
            aux=-43; cout<<"apilando elem "<<aux<<endl;
            pila.apilar(aux);
             aux=89; cout<<"apilando elem "<<aux<<endl;
            pila.apilar(aux);

            aux = pila.desapilar();
            cout<<"desapilando elem "<<aux<<endl;
            aux = pila.desapilar();
            cout<<"desapilando elem "<<aux<<endl;
            aux = pila.desapilar();
            cout<<"desapilando elem "<<aux<<endl;
            cout<<"Cima: "<<pila.cima();
             aux=564; cout<<"apilando elem "<<aux<<endl;
            pila.apilar(aux);
             aux=-987; cout<<"apilando elem "<<aux<<endl;
            pila.apilar(aux);
            aux=-10; cout<<"apilando elem "<<aux<<endl;
            pila.apilar(aux);
             aux=56; cout<<"apilando elem "<<aux<<endl;
            pila.apilar(aux);
            aux=1111; cout<<"apilando elem "<<aux<<endl;
            pila.apilar(aux);
            if(pila.estaLlena()) cout<<"pila llena"<<endl;
            cout<<"mostrando contenido";
            pila.mostrar();
            aux=666; cout<<"\napilando elem "<<aux;
            pila.apilar(aux);
       }
       catch (pilavaciaexception e){
         cout<<"la pila está vacia";
       }
       catch(pilallenaexception e){
                   cout<<"No pUedo empilar elem : "<<e.getdato()<<" la pila está \"Llena\"";

       };

    
    ////////////////////////////////////////////////////////////////////////////////7
    getch();
}
Lesson 1. Catch by reference, not by value.
http://www.parashift.com/c++-faq-lite/exceptions.html#faq-17.7

Lesson 2. Do not use operator new when throwing exceptions. The syntax will seem strange to you but you will need to learn how to do this correctly in order to avoid memory leaks.
http://www.parashift.com/c++-faq-lite/exceptions.html#faq-17.6

The destructor is almost totally wrong.
1
2
3
4
5
 ~pila(){                  //destructor //elimina cada componente de la clase
                     delete &capacidad;
                     delete &indice;
                     delete &datos;
                     }; 


capacidad and indice were not allocated using operator new therefore you do not delete them. This will cause major run-time problems if you delete this object.

datos was created using operator new but you must not delete using the & symbol. It should be delete datos;

It looks to me like you are going to have to spend some time understanding the use of operator new in C++ because it is totally different than in Java. There is no garbage collection and unlike java, C++ has pointers. Operator new returns a pointer so you have to take special care in properly allocating and deallocating memory. In this case, I would have made datos like this:
1
2
3
4
5
std::vector<int> datos;
pila::pila (int cap)
: capacidad(cap), indice(0), datos(cap)
{
}


See std::vector here:
http://cplusplus.com/reference/stl/vector/

As far as what is causing the exception, you should step into the code with a debugger to see if the last function call is causing it or if the pila object causes it during its destruction. It looks like you are checking for invalid indices in the functions. The destructor is definitely wrong and when your object is destroyed, undefined behavior will result.
Last edited on
hey! you are rigth. new is useless and erasing it solved my problem. Thanks a lot. But i still have a question (hope not to butter) I have been reading about it and i dont undertand what it is used for. If it does not mind you...
actually, re-read my post. I added some new findings. I'm not saying that new is useless. You just have to use new/delete correctly. You may actually need operator new if you truly want a dynamic array. On the other hand, you could use the std::vector as I pointed out. In this case since your array size isn't changing it would be very simple to modify the program to use a std::vector. Just because the program no longer crashes, doesn't mean it is correct. It could still have errors.

Take a look at this. Operator new has its uses but you don't have to use it to build all objects. Since there is no auto garbage collector in C++ you have to be more careful with it.
http://cplusplus.com/doc/tutorial/dynamic/
Ok, thanks.
Topic archived. No new replies allowed.