Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   why does it crash? (http://www.velocityreviews.com/forums/t284806-why-does-it-crash.html)

Gernot Frisch 08-04-2004 12:02 PM

why does it crash?
 
The following code crashes, since the delete[] in ~ds is called on a
wrong thingy... Can you help me out? Why does the temporary object get
destructed before the assignment occours???

Output of that code:
152433<bang>


#define MAX_STRLEN 512
class ds;

class cs // const string handler
{
public:
cs(const char* c) {printf("1");dat = c;}
operator ds() const;
const char* dat;
};

class ds // dynamic string handler
{
public:
ds(){printf("2");dat=new char[MAX_STRLEN]; dat[0]='\0';}
~ds() {printf("3");if(dat) delete[]dat; dat=NULL;}
ds& operator=(const cs& a) {printf("4");strcpy(dat, a.dat); return
*this;}
char* dat;
};

cs::operator ds()const {printf("5");ds a;a = *this;return a;}


int main(int argc, char* argv[])
{
ds a = cs("B");
}


--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

________________________________________
Looking for a good game? Do it yourself!
GLBasic - you can do
www.GLBasic.com



John Harrison 08-04-2004 12:10 PM

Re: why does it crash?
 

"Gernot Frisch" <Me@Privacy.net> wrote in message
news:2nc1gqFulgj8U1@uni-berlin.de...
> The following code crashes, since the delete[] in ~ds is called on a
> wrong thingy... Can you help me out? Why does the temporary object get
> destructed before the assignment occours???
>
> Output of that code:
> 152433<bang>
>
>
> #define MAX_STRLEN 512
> class ds;
>
> class cs // const string handler
> {
> public:
> cs(const char* c) {printf("1");dat = c;}
> operator ds() const;
> const char* dat;
> };
>
> class ds // dynamic string handler
> {
> public:
> ds(){printf("2");dat=new char[MAX_STRLEN]; dat[0]='\0';}
> ~ds() {printf("3");if(dat) delete[]dat; dat=NULL;}
> ds& operator=(const cs& a) {printf("4");strcpy(dat, a.dat); return
> *this;}
> char* dat;
> };
>
> cs::operator ds()const {printf("5");ds a;a = *this;return a;}
>
>
> int main(int argc, char* argv[])
> {
> ds a = cs("B");
> }
>


Lack of a copy constructor in class ds. Assignment operator is missing too.

john



Gernot Frisch 08-04-2004 12:48 PM

Re: why does it crash?
 

"John Harrison" <john_andronicus@hotmail.com> schrieb im Newsbeitrag
news:2nc1nvFvcuupU1@uni-berlin.de...
>
> "Gernot Frisch" <Me@Privacy.net> wrote in message
> news:2nc1gqFulgj8U1@uni-berlin.de...
> > The following code crashes, since the delete[] in ~ds is called on

a
> > wrong thingy... Can you help me out? Why does the temporary object

get
> > destructed before the assignment occours???
> >
> > Output of that code:
> > 152433<bang>
> >
> >
> > #define MAX_STRLEN 512
> > class ds;
> >
> > class cs // const string handler
> > {
> > public:
> > cs(const char* c) {printf("1");dat = c;}
> > operator ds() const;
> > const char* dat;
> > };
> >
> > class ds // dynamic string handler
> > {
> > public:
> > ds(){printf("2");dat=new char[MAX_STRLEN]; dat[0]='\0';}
> > ~ds() {printf("3");if(dat) delete[]dat; dat=NULL;}
> > ds& operator=(const cs& a) {printf("4");strcpy(dat, a.dat);

return
> > *this;}
> > char* dat;
> > };
> >
> > cs::operator ds()const {printf("5");ds a;a = *this;return a;}
> >
> >
> > int main(int argc, char* argv[])
> > {
> > ds a = cs("B");
> > }
> >

>
> Lack of a copy constructor in class ds. Assignment operator is

missing too.
>
> john


doesn't "ds& operator=(const cs& a)" work? I'm copying from a cs,
don't I?
<blink, blink>
Somehow I don't get it.



John Harrison 08-04-2004 01:00 PM

Re: why does it crash?
 

"Gernot Frisch" <Me@Privacy.net> wrote in message
news:2nc463FusvbpU1@uni-berlin.de...
>
> "John Harrison" <john_andronicus@hotmail.com> schrieb im Newsbeitrag
> news:2nc1nvFvcuupU1@uni-berlin.de...
> >
> > "Gernot Frisch" <Me@Privacy.net> wrote in message
> > news:2nc1gqFulgj8U1@uni-berlin.de...
> > > The following code crashes, since the delete[] in ~ds is called on

> a
> > > wrong thingy... Can you help me out? Why does the temporary object

> get
> > > destructed before the assignment occours???
> > >
> > > Output of that code:
> > > 152433<bang>
> > >
> > >
> > > #define MAX_STRLEN 512
> > > class ds;
> > >
> > > class cs // const string handler
> > > {
> > > public:
> > > cs(const char* c) {printf("1");dat = c;}
> > > operator ds() const;
> > > const char* dat;
> > > };
> > >
> > > class ds // dynamic string handler
> > > {
> > > public:
> > > ds(){printf("2");dat=new char[MAX_STRLEN]; dat[0]='\0';}
> > > ~ds() {printf("3");if(dat) delete[]dat; dat=NULL;}
> > > ds& operator=(const cs& a) {printf("4");strcpy(dat, a.dat);

> return
> > > *this;}
> > > char* dat;
> > > };
> > >
> > > cs::operator ds()const {printf("5");ds a;a = *this;return a;}
> > >
> > >
> > > int main(int argc, char* argv[])
> > > {
> > > ds a = cs("B");
> > > }
> > >

> >
> > Lack of a copy constructor in class ds. Assignment operator is

> missing too.
> >
> > john

>
> doesn't "ds& operator=(const cs& a)" work? I'm copying from a cs,
> don't I?
> <blink, blink>
> Somehow I don't get it.
>


You are also copying when you 'return a;' For that you need to have a ds
copy constructor defined, as the compiler generated one is crashing your
program.

The straightforward method is this

class ds // dynamic string handler
{
public:
ds(){printf("2");dat=new char[MAX_STRLEN]; dat[0]='\0';}
ds(const ds& rhs){printf("6");dat=new char[MAX_STRLEN]; strcpy(dat,
rhs.dat);}

but maybe a auto_ptr style copy constructor would be better in this case

class ds // dynamic string handler
{
public:
ds(){printf("2");dat=new char[MAX_STRLEN]; dat[0]='\0';}
ds(ds& rhs){printf("6");dat= rhs.dat; rhs.dat = 0;}

That would be more efficient (less memory allocation), and would work for
the code you've quoted, but I've not been following exactly what you are
trying to do, so I'm not sure.

john



Karl Heinz Buchegger 08-04-2004 01:17 PM

Re: why does it crash?
 
Gernot Frisch wrote:
>
> >
> > Lack of a copy constructor in class ds. Assignment operator is

> missing too.
> >
> > john

>
> doesn't "ds& operator=(const cs& a)" work? I'm copying from a cs,
> don't I?
> <blink, blink>
> Somehow I don't get it.


Gernot, If unsure you can try the following:
Declare a private copy constructor and an assignement operator in
each class, but *don't' implement them


For your class ds, we know that both classes would need both
operations, since there is dynamic memory management going
on.

So modify class ds as follows:

class ds // dynamic string handler
{
public:
ds(){printf("2");dat=new char[MAX_STRLEN]; dat[0]='\0';}
~ds() {printf("3");if(dat) delete[]dat; dat=NULL;}
ds& operator=(const cs& a) {printf("4");strcpy(dat, a.dat); return
*this;}
char* dat;

private:
ds( const cs& Arg ); // not implemented by intention
ds operator=( const ds& Arg ); // not implemented by intention
};

and try to compile again. If somewhere a either a copy constructor
or an assignment operator is needed, the above class will either

* not compile

The copy constructor was used outside the class, but the
compiler cannot allow this, since it is private

* not link

One of the member functions of class ds uses a copy constructor
somewhere. Since it is a member function it has access to all
private functions and thus there is no problem for the compiler.

But since you didn't implement the copy constructor, the linker
will moan for a missing function

So in any case:
Declaring those functions private and not implementing them leaves
you with a diagnosable error if one of them is used somewhere. And
from the way ds is written, we know that they *must not* be used
somewhere.

--
Karl Heinz Buchegger
kbuchegg@gascad.at

Gernot Frisch 08-04-2004 01:45 PM

Re: why does it crash?
 
> private:
> ds( const cs& Arg ); // not implemented by intention
> ds operator=( const ds& Arg ); // not implemented by intention
> };



Very good idea!
It was the copy constructor that was missing. I fixed it and it works
good now, thank you all.
-Gernot




All times are GMT. The time now is 04:18 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.