CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 21

Thread: Tasteless mimic of operator new: Call ctor

Hybrid View

  1. #1
    Join Date
    Jun 2002
    Location
    Germany
    Posts
    1,557

    Tasteless mimic of operator new: Call ctor

    Hello Gurus,

    I'm working on a few generic code sequences which should in some ways mimic the functionality of global operator new. I believe the code might be considered in bad taste. However, I would still be curious if there are syntaxes available for this.

    I want to mimic new in so far as it takes (or allocates) a chunk of memory and calls a ctor on it.

    My codes are below. The upper ctor call works for VS2010 but not for GCC 4.5.0. The template works for neither.

    • Can anyone help out with the syntaxes of these codes?
    • Is it even possible to call ctors directly on a chunk of memory in these ways?

    Thanks. Sincerely, Chris.

    Code:
    #include <iostream>
    
    typedef unsigned char UINT8;
    
    struct Wrapper
    {
      unsigned x;
      Wrapper(const unsigned X = 1u) : x(X) { }
    };
    
    namespace
    {
      UINT8 pool[64u];
    }
    
    template<typename T> T* alloc(void)
    {
      ((T*) pool)->T::T();
    }
    
    int main()
    {
      // Can not call constructor directly?
      // Is there a syntax for this?
      ((Wrapper*) pool)->Wrapper::Wrapper();
      const Wrapper* p1 = (Wrapper*) pool;
      std::cout << p1->x << std::endl;
    
      // Does not seem to work.
      // Wrapper does not have member function T.
      // Any syntax for this?
      const Wrapper* p2 = alloc<Wrapper>();
      std::cout << p2->x << std::endl;
    }
    You're gonna go blind staring into that box all day.

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Tasteless mimic of operator new: Call ctor

    Use placement new.

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Tasteless mimic of operator new: Call ctor

    I believe placement-new is what you're looking for.

    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Jun 2002
    Location
    Germany
    Posts
    1,557

    Re: Tasteless mimic of operator new: Call ctor

    Yes, thank you.

    [Edited]: Disregard previous text.
    Last edited by dude_1967; November 24th, 2010 at 02:46 PM. Reason: Post should be disregarded
    You're gonna go blind staring into that box all day.

  5. #5
    Join Date
    Jun 2002
    Location
    Germany
    Posts
    1,557

    Re: Tasteless mimic of operator new: Call ctor

    Oh... Silly me! You guys mean it's already there.

    I got it.

    Please disregard my last post.

    Thanks so much.

    Sincerely, Chris.
    You're gonna go blind staring into that box all day.

  6. #6
    Join Date
    Jun 2002
    Location
    Germany
    Posts
    1,557

    Re: Tasteless mimic of operator new: Call ctor

    Quote Originally Posted by dude_1967 View Post
    I got it.
    So just one more thing...

    Can I coerce placement new to return 0 if my custom buffer is full?

    If so, how?

    Sincerely, Chris.
    You're gonna go blind staring into that box all day.

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Tasteless mimic of operator new: Call ctor

    Quote Originally Posted by dude_1967 View Post
    So just one more thing...

    Can I coerce placement new to return 0 if my custom buffer is full?

    If so, how?

    Sincerely, Chris.
    You wouldn't even try to use placement new if you didn't have memory to use it on. So the decision of whether or not the buffer is full occurs before placement new is called.

  8. #8
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Tasteless mimic of operator new: Call ctor

    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)