Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > Re: composition...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 3 of 12 Topic 9509 of 9828
Post > Topic >>

Re: composition and bad_alloc

by Geert-Jan Giezeman <geert@[EMAIL PROTECTED] > Apr 17, 2008 at 03:58 AM

mpho wrote:
> Hi all,
> 
> This seems straightforward but gives this runtime error: terminate
> called after throwing an instance of 'std::bad_alloc' , what() :
> std9bad_alloc . I have:

There are several errors in your code which make that it does not
compile. There is also another error: you use array new in the
constructor, but do not use array delete in the destructor. Below is a
corrected version of your code. No runtime error occurs when I run it.
That said, I would use std::vector or tr1::array, rather than making
something like this myself.

#include <cassert> // added
typedef double T;  // added

class X {
      int sz;
      T *ptr;

     // added copy constructor and assignment operator, because
     // default ones don't make sense.
      X(X const &);
      X& operator=(X const &);
   public:
     X(int s, T *p) : sz(s), ptr(new T[s])
     {
         assert(ptr != 0);
         // for (int i = 0, i < sz; i++)
         for (int i = 0; i < sz; i++) // comma should be semicolon
             ptr[i] = p[i];
     }

     //~X() {  delete ptr;  ptr = 0;  }
     ~X() {  delete [] ptr;  ptr = 0;  } // array delete needed
};

class Y {
     X xobj;  // semicolon added
   public:
     Y(int s, T *p) : xobj(s, p) {  }
                 ~Y() { }
};

int main(){
     T ar[10] = {0.3};
     Y yobj(10, ar);
}

-- 
      [ See http://www.gotw.ca/resources/clcm.htm
for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
 




 12 Posts in Topic:
composition and bad_alloc
mpho <tjabane@[EMAIL P  2008-04-16 15:43:28 
Re: composition and bad_alloc
Michael Aaron Safyan <  2008-04-17 03:48:15 
Re: composition and bad_alloc
Geert-Jan Giezeman <ge  2008-04-17 03:58:39 
Re: composition and bad_alloc
"Antoon" <th  2008-04-17 03:57:37 
Re: composition and bad_alloc
acehreli@[EMAIL PROTECTED  2008-04-17 03:52:21 
Re: composition and bad_alloc
=?ISO-8859-1?Q?Daniel_Kr=  2008-04-17 03:56:57 
Re: composition and bad_alloc
Gerhard Menzl <clcppm-  2008-04-17 03:59:03 
Re: composition and bad_alloc
Mathias Gaunard <loufo  2008-04-17 03:56:11 
Re: composition and bad_alloc
Chris Uzdavinis <cuzda  2008-04-17 04:13:13 
Re: composition and bad_alloc
mpho <tjabane@[EMAIL P  2008-04-18 06:13:19 
Re: composition and bad_alloc
acehreli@[EMAIL PROTECTED  2008-04-18 15:01:27 
Re: composition and bad_alloc
=?ISO-8859-1?Q?Daniel_Kr=  2008-04-18 15:01:06 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Thu Jul 24 15:42:53 CDT 2008.