***.exe has encountered a problem and needs to close.

balata.h

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
class balata {

private:

	char *name;
	int width;
	int length;
	double price;

public:

	static int counter;

balata(const char*, const int,const int,const double);//constructor
~balata();//destructor
balata(){};//default constructor

balata (const balata &); // copy constructor

int calc_bal(int,int);

void fill_array(balata*,int,balata*[]);

void minimal_price (balata*[],int,int,int);

void show();



};



balata.cpp

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
#include<iostream.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<assert.h>
#include "balata.h"

int balata::counter=0;

balata::balata (const char *nam,const int wid,const int len,const double prize){ //constructor


	cout<<"construct"<<counter<<endl;

	++counter;

	name=new char[strlen(nam)+1];
	assert(name!=0);
	strcpy(name,nam);
	width=wid;
	length=len;
	price=prize;}

balata::~balata()
{
	//cout<<"destruct the tile"<<counter<<endl;

	--counter;

cout<<"destruct the tile"<<counter<<endl;

	delete[]name;}

int balata::calc_bal(int Wid,int Len)
{

	int widka,lenka;

	if (Wid%width!=0)

		widka=(Wid/width)+1;
	else
        widka=Wid/width;

	if (Len%length!=0)

		lenka=(Len/length)+1;
	else
		lenka=(Len/length);

return widka*lenka;

}


void balata::show(){
    cout<<"-------------------------------------------------------------------"<<endl;
    cout<<"the data of current tile is:"<<endl;
	cout<<"name:"<<name<<";"<<"width:"<<width<<";"<<"length:"<<length<<";"<<"price:"<<price<<endl;
	cout<<"-------------------------------------------------------------------"<<endl;
}



void balata::fill_array(balata *tile,int tile_type,balata *tiles_array[]){ //filling array of tiles


  (tiles_array[tile_type])=tile;
cout<<"The tiles are:"<<endl;
  for (int y=0;y<=tile_type;y++){
      (tiles_array[y])->show();}
        
}


void balata::minimal_price(balata *arrayTiles[],int ArrayNumb,int des_wid,int des_len){

double *quant_array=new double [ArrayNumb];

int min_numb=0;

double min_Geld=(arrayTiles[0]->calc_bal(des_wid,des_len))*(arrayTiles[0]->price);

for (int y=0;y<ArrayNumb;y++){

quant_array[y]=(arrayTiles[y]->calc_bal(des_wid,des_len))*(arrayTiles[y]->price);//filling price*quantity array

}


for (int r=1;r<ArrayNumb;r++){
    
	if(quant_array[r]<min_Geld){min_Geld=quant_array[r];min_numb++;}}

    //delete[]quant_array;

cout<<"the most favorable tile is:"<<endl; 
	arrayTiles[min_numb]->show();


cout<<"the number of those tiles that you need is:"<<arrayTiles[min_numb]->calc_bal(des_wid,des_len)<<endl;
	cout<<"and you need to pay:"<<min_Geld<<endl;


	delete[]quant_array;
}



balata_imp.cpp

[codee]#include<iostream.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
#include "balata.h"

void main()

{

char Name[30];
int Wid,Len;
double Priz;
int size;
int Wida,Lena;

int count=0;

balata Tilka;

//balata *Tilka=new balata();


//balata *tile=new balata();

balata **objArray;



cout<<"enter number of how many types of tiles do you have:"<<endl;
cin>>size;

//double *table=new double[size];

objArray=new balata *[size];

for (int j=0;j<size;j++){

cout<<"enter name,width,length,price for tile"<<endl;
cin>>Name>>Wid>>Len>>Priz;

balata *tile=new balata(Name,Wid,Len,Priz);


Tilka.fill_array(tile,j,objArray);

//tile->fill_array(tile,j,objArray);

//Tilka->fill_array(tile,j,objArray);

count++;

if (count==size) {


cout<<"enter desired square as (width*length)"<<endl;
cin>>Wida>>Lena;


Tilka.minimal_price(objArray,size,Wida,Lena);

//tile->minimal_price(objArray,size,Wida,Lena);


//for (int dctor=0;dctor<size-1;dctor++){
//delete objArray [dctor]; cout<<"after delete objArray [dctor]"<<endl;}//delete tile;cout<<"after delete tile"<<endl;}
//!!!!!!!!!!!!!!!!! delete objArray[0]; //=delete tile;

//for (int dctor=0;dctor<size-1;dctor++){
//delete objArray [dctor]; cout<<"after delete objArray [dctor]"<<endl;}//delete tile;cout<<"after delete tile"<<endl;}

//for (int dctor=0;dctor<size-1;dctor++){
//delete tile;cout<<"after delete tile"<<endl;}




//delete []objArray;

//delete Tilka;
// delete []objArray; --nifiga
//
//
//for (int dctor=0;dctor<size;dctor++){
//delete tile;delete objArray [dctor];}

//delete tile;


}//end for if (count==size))

cout<<"after loop if (count==size)"<<endl;

}//end for (int j=0;j<size;j++)

for (int dctor=0;dctor<size-1;dctor++){
delete objArray [dctor];}



//for(int term=0;term<size-1;term++){

//objArray[term]=0;}

delete []objArray;

cout<<"after loop end for (int j=0;j<size;j++)"<<endl;

//for (int dctor=0;dctor<size-1;dctor++){
//delete objArray [dctor]; cout<<"after delete objArray [dctor]"<<endl;}//delete tile;cout<<"after delete tile"<<endl;}

//delete objArray[0];

//delete []objArray;

}//end of prog



[/code]



Here is that program.

I don't understand why I get this mistake.
balata.exe has encountered a problem and needs to close.

I know it's something wrong with dctor and wrong memory allocation.

what do you say? this program works fine without dctor but I want to work in the right way. (with dctors and ctors as well)

here is the print out:
http://i37.tinypic.com/6900pg.jpg


after that I push the button "don't send" it gives me that print out:


http://i34.tinypic.com/bhq4n.jpg


I guess the problem might be as I emphasized in bold style. The dctor should
destroy all the created objects during the program (for (int dctor=0;dctor<size-1;dctor++){
delete objArray [dctor];}
and after that I destroy the array of pointers **balata **objArray by the delete []objArray;.

So what the problem here? As for me it should be Ok..

Last edited on
Man, I dont get it?! Now it works fine..
I put just instead these line Tilka.fill_array(tile,j,objArray) this one
tile->fill_array(tile,j,objArray)

and correspondingly Tilka.minimal_price(objArray,size,Wida,Lena); this tile->minimal_price(objArray,size,Wida,Lena);


Why is that?

Why when I make an object balata Tilka and call by him function makes a problem?

so now the balata_imp.cpp looks like this:

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
#include<iostream.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
#include "balata.h"

void main()

{

char Name[30];
int Wid,Len;
double Priz;
int size;
int Wida,Lena;

int count=0;

//balata Tilka;

balata **objArray;

cout<<"enter number of how many types of tiles do you have:"<<endl;
cin>>size;

//double *table=new double[size];

objArray=new balata *[size];

for (int j=0;j<size;j++){

cout<<"enter name,width,length,price for tile"<<endl;
cin>>Name>>Wid>>Len>>Priz;

balata *tile=new balata(Name,Wid,Len,Priz);


//Tilka.fill_array(tile,j,objArray);

tile->fill_array(tile,j,objArray);

//Tilka->fill_array(tile,j,objArray);

count++;

if (count==size) {
	
	
cout<<"enter desired square as (width*length)"<<endl;
cin>>Wida>>Lena;


//Tilka.minimal_price(objArray,size,Wida,Lena);
tile->minimal_price(objArray,size,Wida,Lena);




}//end for if (count==size))

cout<<"after loop if (count==size)"<<endl;

}//end for (int j=0;j<size;j++)

for (int dctor=0;dctor<size;dctor++){  //!!!!!!!!!!!!!!!!! delete objArray[0]=delete tile;
delete objArray [dctor];}

delete []objArray;

cout<<"after loop end for (int j=0;j<size;j++)"<<endl;

}//end of prog
Very nice job on the indenting. Seriously, your boss is going to love you.
How about posting balata.h? It's pretty clear that's where the problem is.
it doesnt change. (balata.h)
Huh?
It's posted at the very top.
Hmm... Somehow I missed it...

You should learn to use the debugger. You would have spotted the error in five seconds.
You were constructing Tilka without assigning anything to Tilka.name (balata Tilka;). When main() returned and Tilka was popped and destructed, its destructor would try to delete Tilka.name. Since it was uninitialized, this caused a segmentation fault.
This is the alkaline to the acid that are pointers: balata(){};//default constructor
Mix them if you like explosions.

You should always initialize your pointers to zero or NULL (whichever you prefer. They have the same effect) and check that they are pointing somewhere before deleting them. And unless you're sure they can no longer be used (as is the case for members of an object that is being deleted), reassign them to zero, NULL, or a valid address after deleting them.
Last edited on
Topic archived. No new replies allowed.