Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question about operator new()?

Status
Not open for further replies.

cpjust

Programmer
Sep 23, 2003
2,132
US
I've been playing around with operator new() (as a class member, not global), and I haven't been able to find a lot of useful examples other than the definitions.

I wrote a class with a operator new() function like this:

<code>
void* operator new( size_t size )
{
CDeletable* pTemp = ::new CDeletable;
return pTemp;
};
</code>

I used "::new" to call the global new rather than the same function I'm in. (I'm assuming that's how to call the global new?) Anyways, in the debugger this code does work, however, the constructor is called twice. I'm wondering why that is?

If I change the function like this:

<code>
void* operator new( size_t size )
{
void* pTemp = malloc( size );
return pTemp;
};
</code>

Then the constructor is only called once. I'd really rather avoid using malloc() in a C++ program though. Can I use the first method, or something like it, which only calls the constructor once?

Thanks,
Chris
 
Well, what do you want? The global (predefined) ::new operator must call CDeletable constructor (see it's specification)! Let's go:
Code:
CDeletable* p = new CDeletable();
Now you have:
1. CDeletable::new started to allocate memory for the object
2. Then it calls global ::new operator (your code).
3. Now ::new allocate memory and must call class ctor
4. Then your redefined new() must call class ctor again (the 2nd logically redundant call)
Well, it's a wrong design - that's all...
Use ::new char[sizeof(CDeletable)] or what else (malloc;) to avoid redundant CDeletable ctor in a class new(0 op...
 
In the future, don't use reserved words / keywords or standard function names for variable or function names. It will save a good deal of headache.
 
sethmcdoogle, are you referring to my code example?
I'm not sure which reserved words you're referring to?

ArkM, thanks for the explaination. I thought something like that may have been happening. I didn't think of doing "::new char[sizeof( CDeletable )]" though -- that worked.

I also overloaded the array version's of new and delete and noticed that if I do this:

Code:
CDeletable* pTemp = new CDeletable;

I can delete it with both:

Code:
delete pTemp;

or

Code:
delete [] pTemp;

So why not always use "delete []"? Is it doing something else that I'm not seeing?
 
About delete[] and delete. See the sample (let's compile and run):
Code:
class Elem
{
public:
  Elem() { cout << "Elem ctor" << endl; }
  ~Elem(){ cout << "Elem dtor" << endl; }
};

int main(int argc, char* argv[])
{
  Elem* arr = new Elem[3];
  delete [] arr;
  cout << "Now let's die..." << endl;
  arr = new Elem[3];
  delete arr;
  return 0;
}
Array delete[] operator calls all array elements destructors. Alas, the simple delete raises an exception (in debug mode; in release mode you will get more troubles;)...
In the real world delete w/o brackets for simple types (w/o dtor) is similar to array delete. But it's not conforming with C++ Standard.
Moral: use delete with brackets for all pointers allocated with array new op. Avoid ambiguous design etc...

I hope you are aware of senseless simple substitution of ::new in user defined new ops...
True useful user defined new ops must use more effective (than ::new) allocation mechanics...

Good luck!
 
Thanks, but I am aware that delete [] calls all of the destructors for each array element. I was actually referring to using delete [] on single (non-array) types.

Using your example above, if you change both "new Elem[3]" to "new Elem" and run it, it will run and call the destructor with no errors.

Is there any problems with using delete [] on non-array pointers? Maybe performance issues?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top