Re: memory leak in the code?

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



You're in the wrong group. You already know about and use the C++ group, so
stop multiposting.

And yes there would be a memory leak if the allocation for b fails. Use a
smart pointer.

<George George> wrote in message
news:20081510531george4academic@xxxxxxxxxxxx
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

EggHeadCafe - .NET Developer Portal of Choice
http://www.eggheadcafe.com


.



Relevant Pages

  • Re: realloc in function
    ... My note about memory leak is also very important for proper use of realloc. ... > Keep in mind that assigning realloc result to the same pointer may lead ... > Why would you read Microsoft news from anywhere other then the MS news ... > " MVPs earn their status by being nominated by peers and Microsoft ...
    (microsoft.public.vc.language)
  • Re: CList Concept
    ... pointer instead of a list of object. ... called automatically by the list class destructor. ... CObject myObject = new CObject; ... if I do it this way, will I get memory leak? ...
    (microsoft.public.windowsce.embedded.vc)
  • Re: question on malloc(0)
    ... the pointer returned by malloc, it's a memory leak regardless ... After the malloccall, ptr presumably ... points to some small allocated chunk of memory. ...
    (comp.lang.c)
  • Re: memory leak
    ... you should 'delete' object BEFORE zeroing a pointer. ... > void CleanUp() ... > Calling EXAMPLE'S 1 CleanUp method gets rid of the memory leak. ... >> and will thus guarantee a memory leak. ...
    (microsoft.public.vc.language)
  • Re: Parsing options in the same way they are passed to main
    ... > Jeff Rodriguez wrote: ... > This is also a guaranteed memory leak if realloc fails. ... > assign it to the destination pointer if it is non-null. ...
    (comp.lang.c)