Amadeus Amadeus - 1 month ago 7
C++ Question

Memory leak somewhere. Any ideas? Very much appreciated

I'm having a memory leak somewhere in this code. I'm not super knowledgeable so I can't know what exactly is it.

I have to ask the user how many times of what figure they want (rectangle, triangle, poligon, etc).

After I go back to the menu and I chose a new figure the app crashes.

Also for some reason the app already takes it as I already have 1 figure chosen, so if I choose to do 2 triangles, for example, it will do 3. If I choose 4, it will do 5.

#include <iostream>
#include "Figura.h"
#include "Rectangulo.h"
#include "Triangulo.h"
#include "PoligonoR.h"
#include "Rombo.h"
#include <exception>

using namespace std;

const int TAMANO = 5;

int main()
{
cout << "\t\tHERENCIA Y POLIMORFIMO." << endl;
float x = 0, y = 0;
int *arreglo = 0;
int opc = 0;
int ter = 0;
arreglo = new int[TAMANO];


do {
try {
cout << endl << "1) Capturar Rectangulo. " << endl <<
"2) Capturar Triangulo. " << endl <<
"3) Capturar Poligono Regular. " << endl <<
"4) Capturar Rombo. " << endl;
cin >> opc;
cout << endl;
if ( opc < 0 ) { throw "El numero tiene que ser positivo!.\n"; }

} catch (const char *e) { cout << "Error" << endl << e; }

switch(opc) {

case 1: for (int i = 0; i < 1; i++) {
try {
cout << "Cuantas figuras a capturar?\n " << endl;
cin >> arreglo[i];
for (int j = 0; j < arreglo[i];j++) {

//!Imprimir Rectangulo
cout << "\nArea y Perimetro de " << arreglo[i] << " Cuadrado(s). " << endl;
cout << "Base: ";
cin >> x;
cout << "Altura: ";
cin >> y;
Rectangulo Obj(x,y);
cout << "\nEl area de Rectangulo(s) es: " << Obj.Area(x,y) << endl;
cout << "El perimetro de Rectangulo(s) es: " << Obj.Perimetro(x,y) << endl;
} if ( i < 0 || x < 0 || y < 0 ) { throw "El numero tiene que ser positivo!.\n"; }
} catch (const char *e) { cout << "Error" << endl << e; }
}//!Fin del for anidado
break;


case 2: for (int i = 0; i < 1; i++) {
try {
cout << "Cuantas figuras a capturar?\n " << endl;
cin >> arreglo[i];
for (int j = 0; j < arreglo[i];j++) {

//!Imprimir Triangulo
cout << "\nArea y Perimetro de " << arreglo[i] << " Triangulo(s). " << endl;
cout << "Base: ";
cin >> x;
cout << "Altura: ";
cin >> y;
Triangulo Obj2(x,y);
cout << "\nEl area de " << arreglo[i] << " Triangulo(s) es: " << Obj2.Area(x,y) << endl;
cout << "El perimetro de " << arreglo[i] << " Triangulo(s) es: " << Obj2.Perimetro(x,y) << endl;
} if ( i < 0 || x < 0 || y < 0 ) { throw "El numero tiene que ser positivo!.\n"; }
} catch (const char *e) { cout << "Error" << endl << e; }
}//!Fin del for anidado
break;


case 3: for (int i = 0; i < 1; i++) {
try{
cout << "Cuantas figuras a capturar?\n " << endl;
cin >> arreglo[i];
for (int j = 0; j < arreglo[i];j++) {

//!Imprimir Poligono
cout << "\nArea y Perimetro de " << arreglo[i] << " Regular (6 lados)." << endl;
cout << "Lado: ";
cin >> x;
cout << "Apotema: ";
cin >> y;
PoligonoR Obj3(x,y);
cout << "\nEl Area de " << arreglo[i] << " Poligono Regular(es) es: " << Obj3.Area(x,y) << endl;
cout << "El perimetro " << arreglo[i] << " Poligono Regular(es) es: " << Obj3.Perimetro(x, y) << endl;
} if ( i < 0 || x < 0 || y < 0 ) { throw "El numero tiene que ser positivo!.\n"; }
} catch (const char *e) { cout << "Error" << endl << e; }
}//!Fin del for anidado
break;


case 4: for (int i = 0; i < 1; i++) {
try {
cout << "Cuantas figuras a capturar?\n " << endl;
cin >> arreglo[i];
for (int j = 0; j < arreglo[i];j++) {
//!Imprimir Rombo
cout << "\nArea y Perimetro de " << arreglo[i] << " Rombo:" << endl;
cout << "\nDiagonal mayor: ";
cin >> x;
cout << "Diagonal menor: ";
cin >> y;
Rombo Obj4(x,y);
cout << "\nEl Area del Rombo es: " << Obj4.Area(x,y) << endl;
cout << "El perimetro del Rombo es: " << Obj4.Perimetro(x, y) << endl;
} if ( i < 0 || x < 0 || y < 0 ) { throw "El numero tiene que ser positivo!.\n"; }
} catch (const char *e) { cout << "Error" << endl << e; }
}//!Fin del for anidado
break;

default:
break;

}//!fin del switch

delete [] arreglo;

cout << "\nDesea continuar? (1/2) (1 = Si, 2 = no). ";
cin >> ter;

}while(ter == 1);

}

Answer

This is not a memory leak. A memory leak happens when you have a new/new[] without a delete/delete[]

The issue here is the array is declared outside the do..while loop but you are deleting it inside the loop. That meas the next time through the loop you are writing to deleted memory which is undefined behavior. It also is a double delete and that is also undefined behavior. You either need to move the array declaration into the loop so you recreate it every time or move the delete outside the loop so you only delete once.

Comments