4

I just see some declarations in libstdc++-v3, but can't find the definitions. Does the new and delete just encapsulate malloc and free ? Where can I find the definitions of new and delete ? And why can't I find the bits/c++allocator.h include in bits/allocator.h ? So many strange things in gcc.

Zakilo
  • 77
  • 1
  • 7
  • I have heard the `malloc` and `free` are implement with `ptmalloc` in `glibc`, I want to know does `new` and `delete` use `ptmalloc` as well or some other strategy ? – Zakilo Jul 05 '15 at 10:13
  • 4
    https://gcc.gnu.org/viewcvs/gcc/trunk/libstdc%2B%2B-v3/libsupc%2B%2B/new_op.cc?view=markup – Marc Glisse Jul 05 '15 at 11:09
  • 1
    c++allocator.h can be any of the files in https://gcc.gnu.org/viewcvs/gcc/trunk/libstdc%2B%2B-v3/config/allocator/ , that makes it easier to pick a different default when configuring gcc. – Marc Glisse Jul 05 '15 at 11:12
  • Download and read the [source](https://github.com/gcc-mirror/gcc/tree/master/libstdc%2B%2B-v3). – n. m. could be an AI Oct 18 '18 at 15:44

3 Answers3

3

Although C++ Specification states that developers should avoid using legacy old malloc/free way to allocate memory, C++ language itself can do nothing with memory allocation. new/delete act like wrapper to malloc/free with C++ type-safe features and throw expections when malloc return NULL. That why “new” never return NULL but expectations. They do the job for you.

C++ use C routines like glibc, libc for system-level operation. In Linux, the only way to “actually” allocate memory is to call brk() and mmap(). All language include Java and Python call malloc() or lower-level api to allocate memory and call free() when recycle "garbage".

To sum up, the answer for your question is "new/delete call malloc/free and handle expections".

2

Does the new and delete just encapsulate malloc and free ?

Check out this StackOverflow answer.

Where can I find the definitions of new and delete ?

Find the description of new and delete here.

I hope someone else can help you on your third question.

Community
  • 1
  • 1
Mi_Onim
  • 412
  • 2
  • 12
  • 1
    I add a comment for complement just now. Actually, I want to konw how gnu c++ implement `new` and `delete` exactly in its source code. Sorry for not express my question clearly. – Zakilo Jul 05 '15 at 10:20
0

They are handlers that can be redefined. The "malloc" and "free" are parts of that structure. They can be redefined to improve memory managements.

overloading new/delete

Community
  • 1
  • 1
user1436187
  • 3,252
  • 3
  • 26
  • 59