Problem with std::bad_alloc

I made function to extract the pilygons and similar stuf, but when I execute it it pus me bad alloc... the coments are in spanish and don't know kow to make it cleaner so if have any problem to read just post the question.

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
#include <iostream>
#include <fstream>
class Vertice3D
{
private:
	float x,y,z;
public:
	Vertice3D(){};
	Vertice3D(float a, float b, float c)
	{
		x = a;
		y = b;
		z = c;
	}
	void set_X(float a){x = a;}
	void set_Y(float b){y = b;}
	void set_Z(float c){z = c;}
};

class Arista
{
private:
	Vertice3D *v1;
	Vertice3D *v2;
public:

	Arista(){};
	Arista(Vertice3D ver1, Vertice3D ver2)
	{
		v1 = new Vertice3D;
		v2 = new Vertice3D;
		*v1 = ver1;
		*v2 = ver2;
	}
	
	void setVertice(Vertice3D* ver1, Vertice3D* ver2)
	{
		v1 = ver1;
		v2 = ver2;
	}
};

class Poligono
{
private:
	Arista* vec_Arista;
public:
	Poligono(){};
	Poligono(Arista* vector,int n)
	{
		vec_Arista = new Arista[n];
		for(int i=0;i<n;i++)
		{
			vec_Arista[n] = vector[i];
		}
	}
};

void guardarValores3D(char* nombre)
{
	int num_vertices=-1, num_poligonos=-1,numAristas=-1,tempPunto1=-1,tempPunto2=-1,tempPuntoPrimero=-1;
	float tempX=-1.0,tempY=-1.0,tempZ=-1.0;
	Vertice3D temp1, temp2;
	
	std::ifstream entrada;
	entrada.open(nombre);
	
	entrada >> num_vertices >> num_poligonos;
	Vertice3D* todosLosVertices = new Vertice3D[num_vertices];
	
	for(int cont=0;cont<num_vertices;cont++) // Agafem els vertexs
	{
		entrada >> tempX >> tempY >> tempZ;
		todosLosVertices[cont].set_X(tempX);
		todosLosVertices[cont].set_Y(tempY);
		todosLosVertices[cont].set_Z(tempZ);
	}
	
	//Ara fatem els poligons coneixen, primer, el numero de aristas i passsant-li el vector tal cual
	Poligono* todosLosPoligonos = new Poligono[num_poligonos]; //Aqui tinfrm tots els poligons junts
	
	for(int cont=0;cont<num_poligonos;cont++) 
	{
		entrada >> numAristas; //Primer busquem quantes aristas tindra el poligon per poder crear el vector
		Arista* vectorTemp = new Arista[numAristas]; // Aristas del poligon
		for(int cont2 = 0; cont2 < numAristas; cont2++)
		{
			entrada >> tempPunto1 >> tempPunto2;
			if(cont2 == 0) // Per el primer parell guardem el primer vertex
			{
				tempPuntoPrimero = tempPunto1;
			}
			if(cont2 == numAristas-1) // Hem de lincar el ultim amb el primer
			{
				temp1 = todosLosVertices[tempPunto1];
				temp2 = todosLosVertices[tempPuntoPrimero];
			}
			else // Cas normal
			{
				temp1 = todosLosVertices[tempPunto1];
				temp2 = todosLosVertices[tempPunto2];
			}
			Arista arista(temp1,temp2);
			vectorTemp[cont2] = arista;
		} // Ara ja timc fet le vector de aristas i se la n
		Poligono poligono(vectorTemp,numAristas);
		todosLosPoligonos[cont] = poligono;
	}
	
}

int main(int argc,char **argv)
{
	argv++;
  	char * imatgeEntrada = *argv++;
	std::cout << "Com a minim començo" << std::endl;
	std::cout << "Imatge entrada:" << imatgeEntrada << std::endl;
	guardarValores3D(imatgeEntrada);
	std::cout << "En teoria he acabat" << std::endl;
	return 0;
}



And the output is:


Com a minim començo
Imatge entrada:terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc
Abortado



I tryed all dont know how to prove where is the error...
closed account (zb0S216C)
Try throwing a bad_alloc exception and controlling it that way[1]. If you don't control the bad_alloc exception, you program will terminate by default. However, controlling the exception will allow the program to continue, unless you explicitly told the program to terminate.

References:
[1]http://msdn.microsoft.com/en-us/library/6dekhbbc(v=vs.80).aspx


Wazzak
Last edited on
Memory leaks all over the place.
You better use std::vector instead of raw pointers.

1
2
3
	entrada.open(nombre); //check if the file is open
	entrada >> num_vertices >> num_poligonos;
	Vertice3D* todosLosVertices = new Vertice3D[num_vertices]; //check the value of num_vertices 


the coments are in spanish
Liar.
closed account (zb0S216C)
ne555 wrote:
Liar.

Read line 79. Although, not all the comments are in Spanish.

Wazzak
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Arista
{
private:
	Vertice3D *v1;
	Vertice3D *v2;
public:

	Arista(){};
	Arista(Vertice3D ver1, Vertice3D ver2)
	{
		v1 = new Vertice3D;
		v2 = new Vertice3D;
		*v1 = ver1;
		*v2 = ver2;
	}
	
	void setVertice(Vertice3D* ver1, Vertice3D* ver2)
	{
		v1 = ver1;
		v2 = ver2;
	}
};


Just from a quick look, you don't delete the objects pointed by *v1 and *v2 in your destructor. So you have a really nice memory leak. Remember for every new you need somewhere at some point a delete. Otherwise you are introducing memory leaks which depending on the lifetime of your program, OS etc may or may not throw a bad_alloc exception.
Topic archived. No new replies allowed.