My program Crash when I execute a friend external function

Feb 14, 2015 at 8:32pm
I dont know why, but I have declared a friend external function called hello of class proof and when the function is executed, the program crash...the program gives me the solution but later It crashs...any help?


#include <iostream>
#include <cstring>
using namespace std;

class proof{
public:
//constructores
proof(int a2,int b2) : a(a2), b(b2), pa(&a2),pb(&b2) {}
proof():a(0),b(0){}
proof(int *pa2,int *pb2,int a2,int b2) : a(a2),b(b2),pa(&a2),pb(&b2){}
~proof (){ //destructor, borra los punteros cnd sale del ambito de la clase.
delete pa;
delete pb;
}
//declaracion de funciones
void screen(int *pa2,int *pb2,int a2,int b2);
void screen(int a2,int b2);
void screen();
void recognise(proof &a2);




private:
int a,b;
int *pa,*pb;
friend void hello(proof);

};
//funciones de la clase
void proof::screen(int a2,int b2){

cout << "primer valor"<<pa<<"segundo valor"<<pb ;
}

void proof::screen(){
cout<<"primer valor"<< a << "segundo valor "<< b ;
}

void proof::screen(int *pa2,int *pb2,int a2,int b2){
pa = &a;
pb = &b;

cout << " Donde esta a?"<< pa <<endl <<"Donde esta b?" << pb;
}

void proof::recognise(proof &a2){
if (&a2== this)
cout << "Es el mismo objeto"<<endl;
else
cout<< "Es otro obejto"<<endl;

}
//funciones no pertenecientes a la clase

void hello(proof a2){
cout << "la funcion saludo dice"<< endl << a2.a << endl << a2.b << endl ;
//<< a2.pa << endl << a2.pb << endl;
}

int main(){
int a,b;

proof first;
proof second(11,22);
proof pointer(&a,&b,15,25);
proof third(22,11);

//primero.pantalla(); puedo crear un constructor por defecto con cero, pero al crear objeto no puedo usar su funcion correspondiente

cout<<endl;
second.screen(11,22);
cout<<endl;
pointer.screen(&a,&b,11,22);
cout << endl;
first.recognise(first);
first.recognise(second);

hello(second);

}
Feb 14, 2015 at 10:00pm
1
2
3
4
5
    ~proof ()  //destructor, borra los punteros cnd sale del ambito de la clase.
    {
        delete pa;
        delete pb;
    }
1
2
3
int a,b;
//...
proof pointer(&a,&b,15,25);
You are deleting values which never were allocated.
Last edited on Feb 14, 2015 at 10:00pm
Feb 14, 2015 at 10:12pm
Rule of Three: if you need a custom destructor, copy constructor of assignment operator, you probably need the three.


in the function `hello()' you are passing the parameter by copy,
the copy constructor provided by the compiler will perform a shallow copy of the object, that means that `a2.pa' and `second.pa' point to the same memory address.

When the `hello()' function ends, `a2' is destroyed and the `a2.pa' pointer is deleted.
then `main()' ends and attempts to destroy `second', so you've got a double delete.


> proof pointer(&a,&b,15,25);
`pa' would point to memory that was not allocated with new

> proof first;
`pa' is uninitialized
Last edited on Feb 14, 2015 at 10:16pm
Feb 15, 2015 at 4:34pm
Thank you all for the answers..

I have fixed the trouble, changing my constructor proof():a(0),b(0){}form this to proof():a(0),b(0),pa(0),pb(0){}.

In that way, I assign valoures cero to both pointers, but my programm crashed from a call with an object made with a different constructor...this one
proof(int a2,int b2) : a(a2), b(b2), pa(&a2),pb(&b2) {} which has all the members assigned.

I havent understood so weel your answer ne555, I know It's a trouble of concept that I dont know or I cant still manage well...what you mean is that when a function finishs his task it deletes their temporary object( because it was called by copy not by reference) adn when main finishs It calls the destructor whcich try to delete again the same??
Feb 15, 2015 at 6:57pm
1
2
3
4
5
6
7
void hello(proof a2){
} //here `a2' is destroyed (the destructor is invoked){ delete a2.pa }

int main(){
   proof second(11,22);
   hello(second);
} //here `second' is destroyed{ delete second.pa } 
a2.pa and second.pa point to the same place, so you are trying to delete the same memory twice.
Feb 17, 2015 at 7:30pm
Ok, thank you I think I've have go it!!!

Actually I made that destructor, just to learn how to do it, I know It doesnt make any sense, and hardly I can imagine when I should use it....
Topic archived. No new replies allowed.