std containers and allocators.

Discussion in 'C++' started by Andy Venikov, Jul 11, 2005.

  1. Andy Venikov

    Andy Venikov Guest

    Is there a way to change standard containers' behavior
    to expect a 0 when allocation fails (same way you can
    change operator new's behavior to return null by passing
    std::nothrow) ?

    In my environment I can't use exceptions. I can't use
    standard allocators either (embedded, you get the picture).
    I recompiled STLPort with _no_exceptions, but when my
    allocator failed (returned 0) the whole thing crashed.
    My guess is that recompiling with "no exceptions" did
    not change the behavior and it still references 0.
    Is there a standard way to do it?

    Thank you,
    Andy.
     
    Andy Venikov, Jul 11, 2005
    #1
    1. Advertising

  2. In article <>,
    "Andy Venikov" <> wrote:

    > Is there a way to change standard containers' behavior
    > to expect a 0 when allocation fails (same way you can
    > change operator new's behavior to return null by passing
    > std::nothrow) ?
    >
    > In my environment I can't use exceptions. I can't use
    > standard allocators either (embedded, you get the picture).
    > I recompiled STLPort with _no_exceptions, but when my
    > allocator failed (returned 0) the whole thing crashed.
    > My guess is that recompiling with "no exceptions" did
    > not change the behavior and it still references 0.
    > Is there a standard way to do it?


    No.

    What would you like the container to do when its allocator returns 0?

    -Howard
     
    Howard Hinnant, Jul 11, 2005
    #2
    1. Advertising

  3. Andy Venikov

    Andy Venikov Guest

    Well, for insert(), the return could point to map::end.
    For instance.
     
    Andy Venikov, Jul 11, 2005
    #3
  4. In article <>,
    "Andy Venikov" <> wrote:

    > Well, for insert(), the return could point to map::end.
    > For instance.


    <nod> And push_back? Constructors?

    I've done a lot of work on the Metrowerks/Freescale implementation of
    the std::lib to make it work well in embedded environments (reducing
    heap and code size requirements). And it does have a no-exceptions
    mode. The reason I'm asking what your expectations are is to find out
    if I could do something better than what I'm currently doing.

    On allocation failure when exceptions are turned off, our std::allocator
    will call a user-replaceable error function. However the containers are
    not prepared for this function to return. The expectation is that in
    this mode, running out of heap is a bug to be fixed before release, as
    opposed to an unexpected run time event that can be handled (handled
    short of rebooting the program). If the program needs to view this
    event as the latter, I can think of no better way to do so than
    exceptions. However I am open to suggestions.

    -Howard
     
    Howard Hinnant, Jul 11, 2005
    #4
  5. Andy Venikov

    Andy Venikov Guest

    > <nod> And push_back? Constructors?
    Don't forget operator [] for maps, that one may be
    the trickiest :)

    Yeah, I'm aware of the problem. I was just dumb struck
    hitting it in the real life. It looks like the STL design is really
    not compatible (or hardly compatible) with exceptions-free
    environments.

    One way to go around the problem I'm still contemplating, is to
    design the custom allocator in such a way that it always keeps
    extra memory enough for several elements after the official
    memory has bee exhausted, so that the container itself doesn't
    try to reference bad pointers, but the allocator will flag this event.
    The flag will have to be querried by the user of the container.
    Of course there's a lot of problems with this approach.
    First, the allocator now relies on the container's user to querry the
    allocator status and to free newly added element in case of a failure.
    Second, it requires the container's user to know something about the
    custom allocator and how to querry for the status. Third, it doesn't
    solve the case when the container does bulk allocation.
    But still, it's a way.

    Thanks,
    Andy.
     
    Andy Venikov, Jul 13, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Anton
    Replies:
    1
    Views:
    369
    Peter van Merkerk
    Aug 6, 2003
  2. Ares Lagae

    Containers & Allocators

    Ares Lagae, Feb 15, 2005, in forum: C++
    Replies:
    6
    Views:
    401
    Ares Lagae
    Feb 21, 2005
  3. Jef Driesen
    Replies:
    1
    Views:
    290
  4. Replies:
    7
    Views:
    555
    Pete Becker
    Jan 25, 2008
  5. Sebastian Mach
    Replies:
    5
    Views:
    314
Loading...

Share This Page