Thread: How do I overload the new operator to accept a std::string parameter?

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    144

    How do I overload the new operator to accept a std::string parameter?

    Hello. I'm a C++ newbie.

    I'm trying to do something very brave and beyond my capability.

    The code should explain what I'm trying to do. How would I overload the "new" operator so as to allow a string to be passed in and placed in the allocated memory?

    Code:
    #include <cstddef>
    #include <cstdlib>
    #include <exception>
    #include <iostream>
    #include <new>
    #include <string>
    
    void * operator new (std::size_t size, std::string debug_name)
    {
        if (size + sizeof(std::string) < size)
        {
            std::bad_alloc exception;
            throw exception;  /* size overflow */
        }
    
        void * p = std::malloc(size + sizeof(std::string));
    
        std::string *pstr = (std::string *) p;
    
        *pstr = debug_name;
    
        return p + sizeof(std::string);
    }
    
    class invalid_ptr : public std::exception {};
    
    void operator delete(void *ptr)
    {
        if (ptr == NULL || ptr == nullptr)
            return;
    
        void *ptr2 = ptr - sizeof(std::string);
    
        if (ptr2 > ptr)
        {
            invalid_ptr exception;
            throw exception;
        }
    
        std::free(ptr2);
    }
    
    int main()
    {
        int *pi = new int;
        *pi = 42;
    
        std::cout << *pi << std::endl;
    
        delete(pi);
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    I'm trying to do something very brave and beyond my capability.
    That's not how I would describe that code.

    What does your main() have to do with the rest of the code?

    Why are you trying to dynamically allocate a std::string in the first place? What do you think you will gain since the string "data" is already dynamic?

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    The two parts you are missing is how to construct and destroy the string.

    To construct the string, you need to use placement new, wherein you pass the address of the future location of the string a a parameter to new.

    To destroy the string, you simply need to call the destructor on the string explicitly.

    Another thing, you need to be doing pointer arithmetic here with "unsigned char *" or "std::byte *", not void pointers, because "ptr - sizeof(std::string)" does not have defined semantics; you want to go back sizeof(string) bytes, not size of string number of void objects (which don't exist).
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help with overload operator[]
    By effa in forum C++ Programming
    Replies: 8
    Last Post: 10-09-2009, 06:30 AM
  2. Operator overload
    By Gordon in forum C++ Programming
    Replies: 3
    Last Post: 03-11-2008, 01:20 PM
  3. overload same Operator twice
    By noobcpp in forum C++ Programming
    Replies: 4
    Last Post: 06-30-2007, 10:24 AM
  4. Replies: 1
    Last Post: 05-18-2003, 05:10 PM
  5. overload operator<
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 12-12-2001, 03:54 PM

Tags for this Thread