why does it crash?

Discussion in 'C++' started by Gernot Frisch, Aug 4, 2004.

  1. 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::eek:perator 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
     
    Gernot Frisch, Aug 4, 2004
    #1
    1. Advertising

  2. "Gernot Frisch" <> wrote in message
    news:...
    > 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::eek:perator 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
     
    John Harrison, Aug 4, 2004
    #2
    1. Advertising

  3. "John Harrison" <> schrieb im Newsbeitrag
    news:...
    >
    > "Gernot Frisch" <> wrote in message
    > news:...
    > > 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::eek:perator 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.
     
    Gernot Frisch, Aug 4, 2004
    #3
  4. "Gernot Frisch" <> wrote in message
    news:...
    >
    > "John Harrison" <> schrieb im Newsbeitrag
    > news:...
    > >
    > > "Gernot Frisch" <> wrote in message
    > > news:...
    > > > 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::eek:perator 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
     
    John Harrison, Aug 4, 2004
    #4
  5. 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
     
    Karl Heinz Buchegger, Aug 4, 2004
    #5
  6. > 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
     
    Gernot Frisch, Aug 4, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alf P. Steinbach

    Re: Why does this crash?

    Alf P. Steinbach, Jul 18, 2003, in forum: C++
    Replies:
    5
    Views:
    373
  2. Developwebsites

    why does it crash?

    Developwebsites, Oct 10, 2003, in forum: C++
    Replies:
    6
    Views:
    356
    Jerry Coffin
    Oct 11, 2003
  3. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    912
    Mark Rae
    Dec 21, 2006
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,004
    Smokey Grindel
    Dec 2, 2006
  5. Phi!
    Replies:
    1
    Views:
    183
Loading...

Share This Page