-5

The thread here answers the question about the difference between the two: diff-between-malloc-operatornew

What I'm interested to know is: does one use the other? I suspect "operator new" function calls malloc in some form, but I may be way off. Anyone knows the implementation with say gcc?

sepp2k
  • 363,768
  • 54
  • 674
  • 675
Sujay Phadke
  • 2,145
  • 1
  • 22
  • 41
  • 4
    How `operator new` allocates memory is an implementation detail. It could be via `malloc`, or by a syscall to the underlying OS, directly. – StoryTeller - Unslander Monica Jun 06 '17 at 06:13
  • @StoryTeller Yes I know it is implementation specific. But is there some document describing the various implementation(s) for popular compilers/systems? – Sujay Phadke Jun 06 '17 at 06:14
  • @Neroku I don't think that's true. You have to call the constructor yourself. Only "new" (as the keyword/operator) calls the constructor too. – Sujay Phadke Jun 06 '17 at 06:15
  • Concerning gcc, you should've access to the sources of the standard libraries. Thus, you may find out by yourself (although I'm afraid they will be terrible to read...) – Scheff's Cat Jun 06 '17 at 06:16
  • @Scheff exactly. If someone who knows in detail could either tell me, or tell me where to start looking, that'd be great. – Sujay Phadke Jun 06 '17 at 06:17
  • Thinking twice, I'm not quite convinced that implementation of `new` is part of the libraries. Instead, it _could_ be "baked" in the compiler itself. gcc/g++ sources are available as well. (I once had a look into them to check for some C compiler tricks. About sources of gcc, I personally know they are terrible to read...) – Scheff's Cat Jun 06 '17 at 06:22
  • May be, it's worth to add other tags to raise attention of compiler-construction experts. Out of curiosity, I checked the tags overview. There is a [compiler-construction](https://stackoverflow.com/questions/tagged/compiler-construction) tag. In your case [gcc](https://stackoverflow.com/questions/tagged/gcc) could match as well. Digging deeper, you will probably find other tags which improve the chances for any helpful answer. (However, the tedious study of gcc sources may be your "fall-back"...) – Scheff's Cat Jun 06 '17 at 06:28
  • @Neroku no it doesn't. Please refer to this: https://stackoverflow.com/questions/11920486/difference-between-new-operator-and-new-function . I am referring to the function "operator new" not the keyword "new" – Sujay Phadke Jun 06 '17 at 06:29
  • @Abhishek again, you're confusing between "operator new" function vs the "new" operator. That link is not relevant. See the link in my reply to Neroku above. – Sujay Phadke Jun 06 '17 at 06:33
  • @SujayPhadke thanks for the link – JFMR Jun 06 '17 at 06:35
  • @Abhishek please delete your comment – Sujay Phadke Jun 06 '17 at 06:40

1 Answers1

0

It is the normally and customary practice for the default implementation of new to call malloc. However, there is no requirement that it do so. I have seen default new implementations that do not call malloc.

user3344003
  • 20,574
  • 3
  • 26
  • 62