0

In the output new called is printed when the statement Test *m = new Test(); is executed. But we are not passing any argument to the user-defined new function.

Can someone explain what's going on here?

#include<stdlib.h>
#include<iostream>

using namespace std;

class Test {
public:
    void* operator new(size_t size);
    void operator delete(void*);
    Test() { cout<<"\n Constructor called"; }
    ~Test() { cout<<"\n Destructor called"; }
};

void* Test::operator new(size_t size)
{
    cout<<"\n new called";
    void *storage = malloc(size);
    return storage;
}

void Test::operator delete(void *p )
{
    cout<<"\n delete called";
    free(p);
}

int main()
{
    Test *m = new Test();
    delete m;
    return 0;
}
UpAndAdam
  • 4,515
  • 3
  • 28
  • 46
abkds
  • 1,764
  • 7
  • 27
  • 43
  • 5
    Magic? (of the sort that passes `sizeof(Test)`) – Hasturkun Aug 29 '13 at 18:06
  • The magic of compilers.. – Justin Meiners Aug 29 '13 at 18:08
  • 5
    Same way you don't give `operator++(int)` an `int`. – chris Aug 29 '13 at 18:08
  • 2
    So the "problem" here is simply failing to understand the very `operator new` that *you're* overriding? – WhozCraig Aug 29 '13 at 18:11
  • 1
    Beside a terrible title, this isn't a bad question. Saying "Magic" and "The same way x happens" doesn't help. I expect better. – Grammin Aug 29 '13 at 18:12
  • That's how overloading the `new` operator works. The `size` argument is passed to `operator new` by the compiler. – Alex Aug 29 '13 at 18:17
  • @Grammin: I wasn't answering the question, really (although I did attempt to qualify the magic). An answer would likely be along the lines of, "the compiler calls `new` with the size of the allocated object by default". Which I probably won't post as I'm actively looking for a duplicate question, assuming one exists. – Hasturkun Aug 29 '13 at 18:18
  • @UpAndAdam: No. That doesn't appear to answer this question, which appears to be about where the size argument came from. (unfortunately `new` questions are hard to find) – Hasturkun Aug 29 '13 at 18:24
  • @Hasturkun good point sorry about that! :-) thought someone would actually accept only an answer that actually explained it! Yours looks good though! and suprisingly similar to my downvoted answer.. – UpAndAdam Aug 29 '13 at 18:26
  • @UpAndAdam: Well, it looks like a misleading title, that question was about how an apparently non-static method is being found. The accepted answer fits that perfectly. – Hasturkun Aug 29 '13 at 18:28

1 Answers1

0

This is also roughly what Kerrick goes over in size_t parameter new operator

Implicitly there is a call to Test::new(sizeof(Test)) to obtain the memory for keyword new to provide to the constructor to use.

So while you don't realize it you actually are calling it. You can use a debugger and stack trace to figure this out or the documents regarding overriding the new operator and how constructors work.

The relationship between Operator New and the New Keyword

Don't be confused by the fact that there is both a new keyword and an operator new. When you write: MyClass *x = new MyClass; there are actually two things that happen--memory allocation and object construction; the new keyword is responsible for both. One step in the process is to call operator new in order to allocate memory; the other step is to actually invoke the constructor. Operator new lets you change the memory allocation method, but does not have any responsibility for calling the constructor. That's the job of the new keyword.

http://www.cprogramming.com/tutorial/operator_new.html

Community
  • 1
  • 1
UpAndAdam
  • 4,515
  • 3
  • 28
  • 46
  • Dude, why'd everyone downvote this one so much? – ash Aug 29 '13 at 18:31
  • @ash That's what I'd like to know! Even in the edit history I only made it more complete never changed the correctness... – UpAndAdam Aug 29 '13 at 18:32
  • You might want to reorder the contents of your answer somewhat. (also, "Yes you are" is a bit odd) – Hasturkun Aug 29 '13 at 18:36
  • @ash There was an error in the previous version - it used to say an anonymous `Test()` object was being created. In reality, no object has been created, as `operator new` has to allocate the memory for the object, which is then created. At least that's how I (and the downvoters) must have read it. I upvoted it as soon as you fixed that, though. – Alex Aug 29 '13 at 18:38
  • @Alex Fair enough I can see how that would be misunderstood, but the point was still the same.. but its usually good to comment :-) – UpAndAdam Aug 29 '13 at 18:41
  • @Hasturkun Thanks for the pointer, reordered it and reworded again. Yes you are was better than you can't not call it I thought. Probably could have benefitted from a smiley. – UpAndAdam Aug 29 '13 at 18:42