operator new overloading and template functions

This is a discussion on operator new overloading and template functions within the C++ Programming forums, part of the General Programming Boards category; I tried a few things. The new[] operator (or deallocation of arrays) seems to be broken: Code: #include <iostream> struct ...

  1. #31
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I tried a few things. The new[] operator (or deallocation of arrays) seems to be broken:
    Code:
    #include <iostream>
    struct Test
    {
        Test() { std::cout << "Test()\n"; }
        ~Test() { std::cout << "~Test()\n"; }
    };
    
    int main()
    {
        nuiMemoryPool pool(1024);
        try {
            Test* t = new (pool, Wrapper<Test>()) Test[2];
        }
        catch (std::exception& e) {
            std::cout << e.what() << '\n';
        }
    
    };
    Outputs:
    Test()
    Test()
    ~Test()
    ~Test()
    ~Test()
    ~Test()
    ~Test()
    ~Test()
    It also seems that if you are out of memory you should throw an exception and not return NULL, otherwise the program just crashes.

    The nuiMemoryPoolGuard is either broken or I don't get its usage:
    Code:
    int main()
    {
        nuiMemoryPool pool(1024);
        Test* t = new (pool, Wrapper<Test>()) Test;
        {
            nuiMemoryPoolGuard guard(pool);
            Test* u = new (pool, Wrapper<Test>()) Test;
        }
    };
    This leaves u undestructed.

    And Clear offers other misuse:
    Code:
    int main()
    {
        nuiMemoryPool pool(1024);
        Test* t = new (pool, Wrapper<Test>()) Test;
        {
            nuiMemoryPoolGuard guard(pool);
            Test* u = new (pool, Wrapper<Test>()) Test;
            pool.Clear();
        }
    };
    This runs destructor three times.

    * I typedeffed uint8 as unsigned char.
    Last edited by anon; 05-26-2008 at 09:09 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  2. #32
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    A memory pool is not a bulk unloader or some sort of garbage collector, so don't use it like that.
    Well, the Apache HTTPD uses them very effectively as such.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #33
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,262
    Well, the Apache HTTPD uses them very effectively as such.
    I doubt it. They certainly aren't using them more effectively than they could use a real bulk unloader or garbage collector.

    Edit: I don't doubt that they may be using a garbage collector effectively, a bulk unloader effectively, or memory pools effectively. I do doubt that they are using a memory pool as a bulk unloader or a garbage collector effectively.

    Soma
    Last edited by phantomotap; 05-26-2008 at 09:40 AM.

  4. #34
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Check out the APR Pools. That's what I'm talking about.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #35
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,262
    Check out the APR Pools. That's what I'm talking about.
    Nearest I can tell they do not provide a hook for garbage collection. It does have a bulk unload mechanism, but it almost certainly is efficient.

    Soma

  6. #36
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    It's simply a linearly allocated memory area that can be only freed all at once. It does not call object cleanup functions.

    I'm not sure what you mean by a hook for garbage collection, or exactly what you consider a bulk unload mechanism.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #37
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,262
    Then it seems a bit odd that you should say that Apache uses memory pools effectively as such.

    *shrug*

    It doesn't matter. Designing a memory pool to partially emulate a garbage collector or a bulk unloader isn't effective because it must waste time and space to provide this behavior. A bulk unloader or a garbage collector could be associated with any memory or any available hook. They can be designed as separate components making each component stronger. They can then be used together by associating a chunk of memory obtained from a memory pool with either component.

    Soma

    Edit: And a bulk unloader is what you get when you don't have RAII to do cleanup.

  8. #38
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Then it seems a bit odd that you should say that Apache uses memory pools effectively as such.
    I know what I mean by those terms. The cognitive dissonance didn't set in until you replied.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #39
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,262
    I know what I mean by those terms. The cognitive dissonance didn't set in until you replied.
    Then, with that context, I have to ask: why would you find it necessary give the kill function of a memory pool a special name?

    Soma

  10. #40
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Now my confusion is complete. Where did I do that?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #41
    Lode Runner
    Join Date
    May 2004
    Posts
    53
    Thanks for trying it out, anon!

    Quote Originally Posted by anon View Post
    I tried a few things. The new[] operator (or deallocation of arrays) seems to be broken
    Thanks for pointing that out. The sizeof(Test) is 1, yet new (pool, Wrapper,Test>()) Test[n] calls the new operator with size n+4, due to 4-byte alignment. In this case, for a 2-Test array, it asks for 6 bytes, i.e. the size for 6 instances of Test.
    I haven't found a solution to this yet, I had only tested it with simple types which don't require alignment.
    If anyone cares to help me out, I'm still trying to work around this one.

    Quote Originally Posted by anon View Post
    It also seems that if you are out of memory you should throw an exception and not return NULL, otherwise the program just crashes.
    done

    Quote Originally Posted by anon View Post
    The nuiMemoryPoolGuard is either broken or I don't get its usage:
    It was broken, now fixed.

    Quote Originally Posted by anon View Post
    And Clear offers other misuse:
    Code:
    int main()
    {
        nuiMemoryPool pool(1024);
        Test* t = new (pool, Wrapper<Test>()) Test;
        {
            nuiMemoryPoolGuard guard(pool);
            Test* u = new (pool, Wrapper<Test>()) Test;
            pool.Clear();
        }
    };
    This runs destructor three times.
    Now that the guard is fixed, it runs destructor twice, then, on destruction of the guard, it tries to deallocate at the wrong place and fails an assertion. If the assertion is muted, nothing happens on destruction of the guard.

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. template overloading question
    By plutino in forum C++ Programming
    Replies: 14
    Last Post: 02-27-2009, 01:10 AM
  2. template plus ambiguous overloading!!
    By mynickmynick in forum C++ Programming
    Replies: 4
    Last Post: 08-27-2008, 03:26 AM
  3. Template overloading?
    By cpjust in forum C++ Programming
    Replies: 5
    Last Post: 02-20-2008, 02:21 PM
  4. Replies: 7
    Last Post: 11-10-2007, 04:17 AM
  5. Replies: 2
    Last Post: 01-04-2003, 02:35 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21