Proper Way to catch exception thrown by new keyword

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Fao, Sean

    Proper Way to catch exception thrown by new keyword

    I'm reading The C++ Programming Language by Bjarne Stroustrup and I
    was unclear as to the proper way to catch an exception thrown by the
    new keyword. I was wondering if someobody here could let me know if I
    have written the following code correctly.

    <CODE_SNIP>
    #include <iostream>
    #include <new>

    using std::cerr;
    using std::bad_alloc;

    int main()
    {
    char *p;

    try
    {
    for(; ;)
    p = new char[100000];
    }

    catch(bad_alloc )
    {
    cerr << "Error allocating memory...\n";
    return 1;
    }

    delete p;

    return 0;
    }

    </CODE_SNIP>

    I thank you in advance for any pointers you can provide.

    Sean
  • John Harrison

    #2
    Re: Proper Way to catch exception thrown by new keyword


    "Fao, Sean" <enceladus311@y ahoo.com> wrote in message
    news:98236b24.0 307050612.2912a 9d3@posting.goo gle.com...[color=blue]
    > I'm reading The C++ Programming Language by Bjarne Stroustrup and I
    > was unclear as to the proper way to catch an exception thrown by the
    > new keyword. I was wondering if someobody here could let me know if I
    > have written the following code correctly.[/color]

    Well that depends on what you want it to do.
    [color=blue]
    >
    > <CODE_SNIP>
    > #include <iostream>
    > #include <new>
    >
    > using std::cerr;
    > using std::bad_alloc;
    >
    > int main()
    > {
    > char *p;
    >
    > try
    > {
    > for(; ;)
    > p = new char[100000];[/color]

    Are you trying to loop until you run out of memory?

    If two allocations succeed then this loop will leak memory. Because the
    second allocation will overwrite the first and you will never be able to
    free that memory.
    [color=blue]
    > }
    >
    > catch(bad_alloc )[/color]

    This isn't wrong but usually you catch exceptions using a const reference,
    to avoid copying the exception object unnecessarily.

    catch (const bad_alloc&)
    [color=blue]
    > {
    > cerr << "Error allocating memory...\n";
    > return 1;
    > }
    >
    > delete p;
    >
    > return 0;
    > }
    >
    > </CODE_SNIP>
    >
    > I thank you in advance for any pointers you can provide.
    >
    > Sean[/color]

    Looks OK.

    john


    Comment

    • Unforgiven

      #3
      Re: Proper Way to catch exception thrown by new keyword

      Fao, Sean wrote:[color=blue]
      > delete p;[/color]

      In addition to what join said, this line will always be reached, even when
      an exception occurs. You may want to add a check (initialise p to NULL and
      check for that for instance, or place the delete still inside the try block)
      whether you really have any memory to delete.

      Also you are allocating with new[], do you should free with delete[]. It
      doesn't make any difference here because char is a basic type, but it's good
      practice to do it anyway: it adds clarity and gets you in the habit so you
      don't forget it when it does make a difference.

      --
      Unforgiven

      "Earth. It exists only in a corner of my memory."
      Lord Dornkirk
      The Vision of Escaflowne

      Comment

      • John Harrison

        #4
        Re: Proper Way to catch exception thrown by new keyword


        "Unforgiven " <jaapd3000@hotm ail.com> wrote in message
        news:be6v2j$1pg gb$1@ID-136341.news.dfn cis.de...[color=blue]
        > Fao, Sean wrote:[color=green]
        > > delete p;[/color]
        >
        > In addition to what join said, this line will always be reached, even when
        > an exception occurs. You may want to add a check (initialise p to NULL and
        > check for that for instance, or place the delete still inside the try[/color]
        block)[color=blue]
        > whether you really have any memory to delete.[/color]

        Actually there's a return in the catch block, so the delete is not reached
        when an exception occurs.
        [color=blue]
        >
        > Also you are allocating with new[], do you should free with delete[]. It
        > doesn't make any difference here because char is a basic type, but it's[/color]
        good[color=blue]
        > practice to do it anyway: it adds clarity and gets you in the habit so you
        > don't forget it when it does make a difference.[/color]

        Good point.

        john


        Comment

        • Unforgiven

          #5
          Re: Proper Way to catch exception thrown by new keyword

          John Harrison wrote:[color=blue]
          > "Unforgiven " <jaapd3000@hotm ail.com> wrote in message
          > news:be6v2j$1pg gb$1@ID-136341.news.dfn cis.de...[color=green]
          >> Fao, Sean wrote:[color=darkred]
          >>> delete p;[/color]
          >>
          >> In addition to what join said, this line will always be reached,
          >> even when an exception occurs. You may want to add a check
          >> (initialise p to NULL and check for that for instance, or place the
          >> delete still inside the try block) whether you really have any
          >> memory to delete.[/color]
          >
          > Actually there's a return in the catch block, so the delete is not
          > reached when an exception occurs.[/color]

          Oops, didn't see that ^_^

          --
          Unforgiven

          "Earth. It exists only in a corner of my memory."
          Lord Dornkirk
          The Vision of Escaflowne

          Comment

          • Fao, Sean

            #6
            Re: Proper Way to catch exception thrown by new keyword

            "Fao, Sean" <enceladus311@y ahoo.com> wrote in message
            news:98236b24.0 307050612.2912a 9d3@posting.goo gle.com...[color=blue]
            > I'm reading The C++ Programming Language by Bjarne Stroustrup and I
            > was unclear as to the proper way to catch an exception thrown by the
            > new keyword. I was wondering if someobody here could let me know if I
            > have written the following code correctly.[/color]

            I appreciate your comments, I have rewritten the code using your
            recomendations.

            Thank you much...

            Sean

            Comment

            Working...