Hello everyone,


Should I delete memory pointed by pointer a if there is bad_alloc when
allocating memory in memory pointed by pointer b? I am not sure whether there
will be memory leak if I do not delete a.

[Code]
try {
a = new int [N];
b = new int [M];
} catch (bad_alloc)
{
// if a success, but b fail, should we try to delete[] a here to avoid
memory leak?

}
[/Code]


thanks in advance,
George

Re: memory leak in the code? by Eberhard

Eberhard
Sat Jan 05 09:17:59 CST 2008

George wrote:

> Should I delete memory pointed by pointer a if there is bad_alloc when
> allocating memory in memory pointed by pointer b? I am not sure whether there
> will be memory leak if I do not delete a.
>
> [Code]
> try {
> a = new int [N];
> b = new int [M];
> } catch (bad_alloc)
> {
> // if a success, but b fail, should we try to delete[] a here to avoid
> memory leak?
>
> }
> [/Code]

That of course depends on whether you want to dispose of 'a' immediately
when allocation of 'b' fails. This is a design question that your
example doesn't illustrate. But of course, at one point you have to
release ("delete") any memory that was allocated through "new".

Your example shows why many people prefer container classes (like
std::vector) over plain dynamically allocated arrays.

Re: memory leak in the code? by Carl

Carl
Sat Jan 05 09:19:05 CST 2008

George wrote:
> Hello everyone,
>
>
> Should I delete memory pointed by pointer a if there is bad_alloc when
> allocating memory in memory pointed by pointer b? I am not sure
> whether there will be memory leak if I do not delete a.
>
> [Code]
> try {
> a = new int [N];
> b = new int [M];
> } catch (bad_alloc)
> {
> // if a success, but b fail, should we try to delete[] a here to
> avoid memory leak?
>
> }
> [/Code]

Never write code like this - it's impossible to know what to do. Instead,
use a smart pointer class (but for this, not std::auto_ptr) that "knows" if
it holds a valid pointer or not and will call delete (or delete[] or
whatever) at the appropriate times.

In the simplest (and least maintainable) solution, you could write:

[Code]
try
{
a = new int [N];
} catch (bad_alloc)
{
// do whatever
}

try
{
b = new int [M];
} catch (bad_alloc)
{
delete[] a;

// do whatever
}

[/Code]

-cd



Re: memory leak in the code? by David

David
Sat Jan 05 09:26:17 CST 2008

George wrote:
> Hello everyone,
>
>
> Should I delete memory pointed by pointer a if there is bad_alloc when
> allocating memory in memory pointed by pointer b? I am not sure whether there
> will be memory leak if I do not delete a.
>
> [Code]
> try {
> a = new int [N];
> b = new int [M];
> } catch (bad_alloc)
> {
> // if a success, but b fail, should we try to delete[] a here to avoid
> memory leak?
>
> }
> [/Code]

George:

Surely we have discussed RAII endlessly in the recent past?

std::vector<int>(N);
std::vector<int>(M);

--
David Wilkinson
Visual C++ MVP

Re: memory leak in the code? by Arnie

Arnie
Sat Jan 05 12:07:18 CST 2008

"George" <George@discussions.microsoft.com> wrote in message
news:4BB7949B-C074-4310-A872-C5CFC96F2809@microsoft.com...
> Hello everyone,
>
>
> Should I delete memory pointed by pointer a if there is bad_alloc when
> allocating memory in memory pointed by pointer b? I am not sure whether
> there
> will be memory leak if I do not delete a.
>
> [Code]
> try {
> a = new int [N];
> b = new int [M];
> } catch (bad_alloc)
> {
> // if a success, but b fail, should we try to delete[] a here to avoid
> memory leak?
>
> }
> [/Code]
>
>
> thanks in advance,
> George
Hi George,

First, if a memoryt allocation fails, the process will probably end. If
that's the case there's no need to worry about memory leaks. All memory
allocated to the process will be freed when the process ends.

If you know EXACTLY the size of an array required there's no real need to
use a container class. However I prefer the container class because when
passed into another function I can ask for it's size() rather than relying
on #defines or const int variables.

In your contrived example, it would be impossible to know which allocation
(a or b) failed. As Carl pointed out, each could be within their own
try/catch block. Of course, you would have to be able to recover from
either allocation failure to be worthwhile.

David - I agree that RAII and std::vector<> are both good things. However,
in your example, one doesn't have anything to do with the other. And
certainly they have nothing to to do with a bad_alloc.

- Arnie



Re: memory leak in the code? by George

George
Sun Jan 06 08:30:01 CST 2008

Thanks cd,


Good pattern.


regards,
George

"Carl Daniel [VC++ MVP]" wrote:

> George wrote:
> > Hello everyone,
> >
> >
> > Should I delete memory pointed by pointer a if there is bad_alloc when
> > allocating memory in memory pointed by pointer b? I am not sure
> > whether there will be memory leak if I do not delete a.
> >
> > [Code]
> > try {
> > a = new int [N];
> > b = new int [M];
> > } catch (bad_alloc)
> > {
> > // if a success, but b fail, should we try to delete[] a here to
> > avoid memory leak?
> >
> > }
> > [/Code]
>
> Never write code like this - it's impossible to know what to do. Instead,
> use a smart pointer class (but for this, not std::auto_ptr) that "knows" if
> it holds a valid pointer or not and will call delete (or delete[] or
> whatever) at the appropriate times.
>
> In the simplest (and least maintainable) solution, you could write:
>
> [Code]
> try
> {
> a = new int [N];
> } catch (bad_alloc)
> {
> // do whatever
> }
>
> try
> {
> b = new int [M];
> } catch (bad_alloc)
> {
> delete[] a;
>
> // do whatever
> }
>
> [/Code]
>
> -cd
>
>
>

Re: memory leak in the code? by George

George
Sun Jan 06 08:31:01 CST 2008

Thanks Eberhard,


I understand and agree that no matter when to delete a if allocation of b
fails, we need to delete a to avoid memory leak.


regards,
George

"Eberhard Schefold" wrote:

> George wrote:
>
> > Should I delete memory pointed by pointer a if there is bad_alloc when
> > allocating memory in memory pointed by pointer b? I am not sure whether there
> > will be memory leak if I do not delete a.
> >
> > [Code]
> > try {
> > a = new int [N];
> > b = new int [M];
> > } catch (bad_alloc)
> > {
> > // if a success, but b fail, should we try to delete[] a here to avoid
> > memory leak?
> >
> > }
> > [/Code]
>
> That of course depends on whether you want to dispose of 'a' immediately
> when allocation of 'b' fails. This is a design question that your
> example doesn't illustrate. But of course, at one point you have to
> release ("delete") any memory that was allocated through "new".
>
> Your example shows why many people prefer container classes (like
> std::vector) over plain dynamically allocated arrays.
>

Re: memory leak in the code? by George

George
Sun Jan 06 08:32:00 CST 2008

Hi David,


RAII is what I prefer. Currently, I am learning code by others. :-)


regards,
George

"David Wilkinson" wrote:

> George wrote:
> > Hello everyone,
> >
> >
> > Should I delete memory pointed by pointer a if there is bad_alloc when
> > allocating memory in memory pointed by pointer b? I am not sure whether there
> > will be memory leak if I do not delete a.
> >
> > [Code]
> > try {
> > a = new int [N];
> > b = new int [M];
> > } catch (bad_alloc)
> > {
> > // if a success, but b fail, should we try to delete[] a here to avoid
> > memory leak?
> >
> > }
> > [/Code]
>
> George:
>
> Surely we have discussed RAII endlessly in the recent past?
>
> std::vector<int>(N);
> std::vector<int>(M);
>
> --
> David Wilkinson
> Visual C++ MVP
>

Re: memory leak in the code? by George

George
Sun Jan 06 08:40:00 CST 2008

Hi Arnie,


Two more comments,

1.

> First, if a memoryt allocation fails, the process will probably end. If
> that's the case there's no need to worry about memory leaks. All memory
> allocated to the process will be freed when the process ends.

Not absolutely. There are other possibilities when bad_alloc happens, like
invalid input or too large size value.

2.

> If you know EXACTLY the size of an array required there's no real need to
> use a container class.

Why if size is known, there is no need to use container class, like vector?
Could you provide more description please?


regards,
George

"Arnie" wrote:

> "George" <George@discussions.microsoft.com> wrote in message
> news:4BB7949B-C074-4310-A872-C5CFC96F2809@microsoft.com...
> > Hello everyone,
> >
> >
> > Should I delete memory pointed by pointer a if there is bad_alloc when
> > allocating memory in memory pointed by pointer b? I am not sure whether
> > there
> > will be memory leak if I do not delete a.
> >
> > [Code]
> > try {
> > a = new int [N];
> > b = new int [M];
> > } catch (bad_alloc)
> > {
> > // if a success, but b fail, should we try to delete[] a here to avoid
> > memory leak?
> >
> > }
> > [/Code]
> >
> >
> > thanks in advance,
> > George
> Hi George,
>
> First, if a memoryt allocation fails, the process will probably end. If
> that's the case there's no need to worry about memory leaks. All memory
> allocated to the process will be freed when the process ends.
>
> If you know EXACTLY the size of an array required there's no real need to
> use a container class. However I prefer the container class because when
> passed into another function I can ask for it's size() rather than relying
> on #defines or const int variables.
>
> In your contrived example, it would be impossible to know which allocation
> (a or b) failed. As Carl pointed out, each could be within their own
> try/catch block. Of course, you would have to be able to recover from
> either allocation failure to be worthwhile.
>
> David - I agree that RAII and std::vector<> are both good things. However,
> in your example, one doesn't have anything to do with the other. And
> certainly they have nothing to to do with a bad_alloc.
>
> - Arnie
>
>
>

Re: memory leak in the code? by Carl

Carl
Sun Jan 06 13:22:24 CST 2008

George wrote:
> Thanks cd,
>
>
> Good pattern.

No, it's really not. It's an example of how to bandage bad code to make it
at least reliable. Using a smart pointer, or std::vector is a much better
pattern.

-cd



Re: memory leak in the code? by George

George
Mon Jan 07 01:46:01 CST 2008

Thanks for your advice, cd!


regards,
George

"Carl Daniel [VC++ MVP]" wrote:

> George wrote:
> > Thanks cd,
> >
> >
> > Good pattern.
>
> No, it's really not. It's an example of how to bandage bad code to make it
> at least reliable. Using a smart pointer, or std::vector is a much better
> pattern.
>
> -cd
>
>
>

Re: memory leak in the code? by Giovanni

Giovanni
Mon Jan 07 13:08:02 CST 2008


"George" <George@discussions.microsoft.com> ha scritto nel messaggio
news:4BB7949B-C074-4310-A872-C5CFC96F2809@microsoft.com...

> Should I delete memory pointed by pointer a if there is bad_alloc when
> allocating memory in memory pointed by pointer b? I am not sure whether
> there
> will be memory leak if I do not delete a.
>
> [Code]
> try {
> a = new int [N];
> b = new int [M];

David W. and others suggested RAII and exception-safe std::vector usage. I
completely agree with them.

I'd just like to add that:

> } catch (bad_alloc)

I think you should catch using *reference*, e.g.:

} catch( std::bad_alloc & )

Giovanni




Re: memory leak in the code? by Duane

Duane
Mon Jan 07 15:27:58 CST 2008


"Giovanni Dicanio" <giovanni.dicanio@invalid.com> wrote in message
news:O1YehCWUIHA.4752@TK2MSFTNGP05.phx.gbl...
>
> "George" <George@discussions.microsoft.com> ha scritto nel messaggio
> news:4BB7949B-C074-4310-A872-C5CFC96F2809@microsoft.com...
>
>> Should I delete memory pointed by pointer a if there is bad_alloc when
>> allocating memory in memory pointed by pointer b? I am not sure whether
>> there
>> will be memory leak if I do not delete a.
>>
>> [Code]
>> try {
>> a = new int [N];
>> b = new int [M];
>
> David W. and others suggested RAII and exception-safe std::vector usage. I
> completely agree with them.
>
> I'd just like to add that:
>
>> } catch (bad_alloc)
>
> I think you should catch using *reference*, e.g.:
>
> } catch( std::bad_alloc & )

I would use a const &.



Re: memory leak in the code? by George

George
Mon Jan 07 23:40:01 CST 2008

Thanks Giovanni,


Cool!


regards,
George

"Giovanni Dicanio" wrote:

>
> "George" <George@discussions.microsoft.com> ha scritto nel messaggio
> news:4BB7949B-C074-4310-A872-C5CFC96F2809@microsoft.com...
>
> > Should I delete memory pointed by pointer a if there is bad_alloc when
> > allocating memory in memory pointed by pointer b? I am not sure whether
> > there
> > will be memory leak if I do not delete a.
> >
> > [Code]
> > try {
> > a = new int [N];
> > b = new int [M];
>
> David W. and others suggested RAII and exception-safe std::vector usage. I
> completely agree with them.
>
> I'd just like to add that:
>
> > } catch (bad_alloc)
>
> I think you should catch using *reference*, e.g.:
>
> } catch( std::bad_alloc & )
>
> Giovanni
>
>
>
>

Re: memory leak in the code? by George

George
Mon Jan 07 23:41:02 CST 2008

Thanks Duane,


Right and agree.

regards,
George

"Duane Hebert" wrote:

>
> "Giovanni Dicanio" <giovanni.dicanio@invalid.com> wrote in message
> news:O1YehCWUIHA.4752@TK2MSFTNGP05.phx.gbl...
> >
> > "George" <George@discussions.microsoft.com> ha scritto nel messaggio
> > news:4BB7949B-C074-4310-A872-C5CFC96F2809@microsoft.com...
> >
> >> Should I delete memory pointed by pointer a if there is bad_alloc when
> >> allocating memory in memory pointed by pointer b? I am not sure whether
> >> there
> >> will be memory leak if I do not delete a.
> >>
> >> [Code]
> >> try {
> >> a = new int [N];
> >> b = new int [M];
> >
> > David W. and others suggested RAII and exception-safe std::vector usage. I
> > completely agree with them.
> >
> > I'd just like to add that:
> >
> >> } catch (bad_alloc)
> >
> > I think you should catch using *reference*, e.g.:
> >
> > } catch( std::bad_alloc & )
>
> I would use a const &.
>
>
>

Re: memory leak in the code? by Duane

Duane
Tue Jan 08 06:26:07 CST 2008


"George" <George@discussions.microsoft.com> wrote in message
news:7BABFDCF-EB0C-44A6-A348-FF676610E2ED@microsoft.com...
> Thanks Duane,
>
>
> Right and agree.

You're welcome. But the best suggestion
here was to avoid these problems completely
by using RAII and/or exception safe vector
design.



Re: memory leak in the code? by George

George
Tue Jan 08 06:41:03 CST 2008

Thanks Duane,


I agree and my question is answered.


regards,
George

"Duane Hebert" wrote:

>
> "George" <George@discussions.microsoft.com> wrote in message
> news:7BABFDCF-EB0C-44A6-A348-FF676610E2ED@microsoft.com...
> > Thanks Duane,
> >
> >
> > Right and agree.
>
> You're welcome. But the best suggestion
> here was to avoid these problems completely
> by using RAII and/or exception safe vector
> design.
>
>
>

Re: memory leak in the code? by Alexander

Alexander
Wed Jan 09 11:55:32 CST 2008

std::vector is not the proper replacement for an array since it
can grow. The correct class would be one of:
boost::scoped_array<>
boost::shared_array<>

http://www.boost.org

--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: agnickolov@mvps.org
MVP VC FAQ: http://vcfaq.mvps.org
=====================================

"Giovanni Dicanio" <giovanni.dicanio@invalid.com> wrote in message
news:O1YehCWUIHA.4752@TK2MSFTNGP05.phx.gbl...
>
> "George" <George@discussions.microsoft.com> ha scritto nel messaggio
> news:4BB7949B-C074-4310-A872-C5CFC96F2809@microsoft.com...
>
>> Should I delete memory pointed by pointer a if there is bad_alloc when
>> allocating memory in memory pointed by pointer b? I am not sure whether
>> there
>> will be memory leak if I do not delete a.
>>
>> [Code]
>> try {
>> a = new int [N];
>> b = new int [M];
>
> David W. and others suggested RAII and exception-safe std::vector usage. I
> completely agree with them.
>
> I'd just like to add that:
>
>> } catch (bad_alloc)
>
> I think you should catch using *reference*, e.g.:
>
> } catch( std::bad_alloc & )
>
> Giovanni
>
>
>



Re: memory leak in the code? by George

George
Wed Jan 09 21:35:01 CST 2008

Hi Alexander,


Why it is not good that it could grow?


regards,
George

"Alexander Nickolov" wrote:

> std::vector is not the proper replacement for an array since it
> can grow. The correct class would be one of:
> boost::scoped_array<>
> boost::shared_array<>
>
> http://www.boost.org
>
> --
> =====================================
> Alexander Nickolov
> Microsoft MVP [VC], MCSD
> email: agnickolov@mvps.org
> MVP VC FAQ: http://vcfaq.mvps.org
> =====================================
>
> "Giovanni Dicanio" <giovanni.dicanio@invalid.com> wrote in message
> news:O1YehCWUIHA.4752@TK2MSFTNGP05.phx.gbl...
> >
> > "George" <George@discussions.microsoft.com> ha scritto nel messaggio
> > news:4BB7949B-C074-4310-A872-C5CFC96F2809@microsoft.com...
> >
> >> Should I delete memory pointed by pointer a if there is bad_alloc when
> >> allocating memory in memory pointed by pointer b? I am not sure whether
> >> there
> >> will be memory leak if I do not delete a.
> >>
> >> [Code]
> >> try {
> >> a = new int [N];
> >> b = new int [M];
> >
> > David W. and others suggested RAII and exception-safe std::vector usage. I
> > completely agree with them.
> >
> > I'd just like to add that:
> >
> >> } catch (bad_alloc)
> >
> > I think you should catch using *reference*, e.g.:
> >
> > } catch( std::bad_alloc & )
> >
> > Giovanni
> >
> >
> >
>
>
>

Re: memory leak in the code? by David

David
Thu Jan 10 04:24:45 CST 2008

George wrote:
> Hi Alexander,
>
>
> Why it is not good that it could grow?

George:

It's just that it's overkill for the purpose.

--
David Wilkinson
Visual C++ MVP

Re: memory leak in the code? by George

George
Thu Jan 10 05:22:00 CST 2008

Thanks for your clarification, David!


regards,
George

"David Wilkinson" wrote:

> George wrote:
> > Hi Alexander,
> >
> >
> > Why it is not good that it could grow?
>
> George:
>
> It's just that it's overkill for the purpose.
>
> --
> David Wilkinson
> Visual C++ MVP
>

Re: memory leak in the code? by Alexander

Alexander
Thu Jan 10 18:26:08 CST 2008

Couldn't have put it better myself...

--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: agnickolov@mvps.org
MVP VC FAQ: http://vcfaq.mvps.org
=====================================

"David Wilkinson" <no-reply@effisols.com> wrote in message
news:udHqbL3UIHA.3908@TK2MSFTNGP04.phx.gbl...
> George wrote:
>> Hi Alexander,
>>
>>
>> Why it is not good that it could grow?
>
> George:
>
> It's just that it's overkill for the purpose.
>
> --
> David Wilkinson
> Visual C++ MVP