?
Solved

Overloaded new

Posted on 1999-11-16
15
Medium Priority
?
286 Views
Last Modified: 2010-05-18
Hi Jason,

Thanks for the answer.
I would like to clarify my question.

In ObjectStore they have not overloaded the operator new .They have overloaded the new operator (the new operator calls the opertaor new and subsequently the constructor).
ObjectStore basically reserves some space in memory and that area is known as Persistent storage. All data/objects allocated within that area gets stored to disk.

Is the persistent new (as ObjectStore calls it) an overloaded new operator or an overloaded operator new.

Another sub-question, Is the placement new an overloaded version of the new operator or an overloaded version of the operator new.

I am sorry for the messy question. It is a wee bit hard to describe the question in words.

Regards
Shayad

 

ObjectStore (an OODB) uses an overloaded new operator to allocate memory.In fact it takes two parameters viz. the db handle (db in which to allocate the object) and apointer to the type of the object which will be allocated.

char* ptr =
new( db, os_ts<char *>::get))

Is this syntax similar to the syntax of placement new. Placement new basically assigns an object to some pre-allocated memory.

Normally we oveload the operator new instead of the new operator. In case of ObjectStore the new operator has been overloaded. I wonder how the overloded new operator works.

Regards
Shyad
0
Comment
Question by:shayad
  • 7
  • 7
15 Comments
 
LVL 9

Expert Comment

by:jasonclarke
ID: 2210782
What are you getting at?  ObjectStore overloads the global operator new to allow allocation of persistant store.

You can overload operator new on a per class basis:

class A {
public:

  ...  
  static void * operator new(size_t size);
  static void operator delete(void *p, size_t size);
  ...
};


// Usage
  A* a = new A; // will call A::new
  delete a; // will call A::delete

You can redefine the global operator new like this:

void * operator new(size_t size)  
{
  ...
}


Placement new looks like you say:

char* buffer = new char[20];
A* a = new (buffer) A;

What exactly do you need to know?
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2210812
I thought you can't overload the new operator. The example code you give above is ill-formed:
===========================================================================================
  5.3.4  New                                                  [expr.new]
        new-expression:
                  ::opt new new-placementopt new-type-id new-initializeropt
                  ::opt new new-placementopt ( type-id ) new-initializeropt
          new-placement:
                  ( expression-list )
          new-type-id:
                  type-specifier-seq new-declaratoropt
          new-declarator:
                  ptr-operator new-declaratoropt
                  direct-new-declarator
          direct-new-declarator:
                  [ expression ]
                  direct-new-declarator [ constant-expression ]
          new-initializer:
                  ( expression-listopt )
===========================================================================================

Looks like a macro may be used to achieve this syntax.
0
 

Author Comment

by:shayad
ID: 2210838
Edited text of question.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
LVL 9

Accepted Solution

by:
jasonclarke earned 600 total points
ID: 2210840
I think its OK, the following code compiles OK with VC++:

void * operator new(size_t s, int p1, int p2)
{
    return 0;
}

void main()
{
    int* a = new (1,1) int;
}

new-placement in your type definition is an expression-list, so maybe this allows this sort of thing?
0
 
LVL 9

Expert Comment

by:jasonclarke
ID: 2210849
Its better if you don't edit the question like this because you lose the track and sense of questions.  Its better to just add comments to the history.

What you ask is confusing,  there is only operator new,  the new operator is just another name for the same thing - which is more properly called operator new (or specifically the global operator new in this case).

Placement new is an overloaded form of the global operator new.
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2210858
Jason, you have Effective C++? Anyway there are two of those, the new operator and operator new. The latter is the one that is commenly overloaded, the former is 'built in' and can't be overloaded. I suspect ObjectStore uses some macro-magic to achieve 'overloading' of the new operator.
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2210899
>> new-placement in your type definition is an expression-list...
type-id is not optional.
Frankly I don't get the example given. How should one interpret:
char* ptr = new( db, os_ts<char *>::get))
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2210902
Unless they've overloaded operator,
The bastards ;)
0
 
LVL 9

Expert Comment

by:jasonclarke
ID: 2211137
> type-id is not optional.


Oh, I mised that bit altogether, I think thats a typo.  The ObjectStore code should still have a type (at least in my version of ObjectStore it does):

char* ptr = new( db, os_ts<char *>::get)) char; // or something.

> the new operator and operator new

are you talking about the difference between 'global' new and 'class' new? You can overload global new (although I reckon you sure better know what you are doing!):

void * operator new(size_t s)
{
    return 0;
}

void main()
{
    int* a = ::new int;
}

0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2211157
Meyers makes a distinction between the (built in) new operator and operator new. The new operator first calls operator new, then a constructor. You can overload the new operator, not operator new (I love this ;) Off course, when using placement new, the new operator isn't called.
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2211162
Excuse me, it is confusing, when using placement new, operator new isn't called.
0
 
LVL 9

Expert Comment

by:jasonclarke
ID: 2211245
OK, I found the bit in Meyers you are talking about (Item no. 8 in More Effective C++), and you are correct - you can overload the memory allocation part of new, but you cannot overload the initialisation part of new (the bit that calls the constructor and casts the pointer).

But calling them operator new and the new operator and expecting the distinction to be clear?
0
 
LVL 9

Expert Comment

by:jasonclarke
ID: 2211247
OK, I found the bit in Meyers you are talking about (Item no. 8 in More Effective C++), and you are correct - you can overload the memory allocation part of new, but you cannot overload the initialisation part of new (the bit that calls the constructor and casts the pointer).

But calling them operator new and the new operator and expecting the distinction to be clear?
0
 
LVL 9

Expert Comment

by:jasonclarke
ID: 2211249
Sorry for posting that twice, I think my browser went a bit screwy...
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2211341
Not clear at all. What Meyers refers to as the new operator is referred to as a new-expression in the draft.
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question