Undefined reference to 'operator delete(void*)'

前端 未结 4 2026
长发绾君心
长发绾君心 2021-02-12 22:09

I\'m new to C++ programming, but have been working in C and Java for a long time. I\'m trying to do an interface-like hierarchy in some serial protocol I\'m working on, and kee

4条回答
  •  [愿得一人]
    2021-02-12 22:28

    If you are not linking against the standard library for some reason (as may well be the case in an embedded scenario), you have to provide your own operators new and delete. In the simplest case, you could simply wrap malloc, or allocate memory from your own favourite source:

    void * operator new(std::size_t n)
    {
      void * const p = std::malloc(n);
      // handle p == 0
      return p;
    }
    
    void operator delete(void * p) // or delete(void *, std::size_t)
    {
      std::free(p);
    }
    

    You should never have to do this if you are compiling for an ordinary, hosted platform, so if you do need to do this, you better be familiar with the intricacies of memory management on your platform.

提交回复
热议问题