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 5 of 12 Topic 9509 of 9830
Post > Topic >>

Re: composition and bad_alloc

by acehreli@[EMAIL PROTECTED] Apr 17, 2008 at 03:52 AM

The following is not the code that's giving you trouble. It can't be
compiled. I tried compiling your code for five minutes, then gave up.

On Apr 16, 2:43 pm, mpho <tjab...@[EMAIL PROTECTED]
> wrote:
> This seems straightforward but gives this runtime error: terminate
> called after throwing an instance of 'std::bad_alloc' , what() :
> std9bad_alloc .

std::bad_alloc is thrown when new cannot allocate the requested amount
of memory.

> I have:
>
> class X {
>      int sz;
>      T *ptr;
>
>   public:
>             X(int s, T *p) : sz(s), ptr(new T[s])
>             {
>               assert(ptr != NULL);

That assertion will always succeed, because new never returns NULL,
unless you explicitly request that behavior with 'nothrow' version of
new:

ptr(new (std::nothrow) T[s])

>                for (int i = 0, i < sz; i++)

Replace that comma with a semicolon.

>                   ptr[i] = p[i];
>             }
>
>             //other members
>
>             ~X() {  delete ptr;  ptr = NULL;  }

That's undefined behavior: you must use delete[] when you use new[].

Additionally, setting ptr to NULL is completely unnecessary, right?
The object is about to die. Who will ever reference ptr?

Surely, you did not give out the address of or a reference to it to
your callers...

>  };
>
>  class Y {
>               X xobj

A semicolon missing.

>
>     public:
>                Y(int s, T *p) : xobj(s, p) {  } //OK?

That's OK.

>                 //other members
>                 ~Y() { } //OK?

That's OK too.

>  };
>
> then
>
> int main(){
>
>  ar[10] = {ten values};
>  Y yobj(10, ar); //problem here(?)

No, there is no problem there. The problem is in the code that you did
not show, including the definition of T.

Lastly, I recommend that you become familiar with the standard
containers like std::vector. You will notice that your resource
owner****p headaches will vanish. :)

Ali

-- 
      [ 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 Fri Jul 25 15:30:56 CDT 2008.