Overloaded new

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
shayadAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jasonclarkeCommented:
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
KangaRooCommented:
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
shayadAuthor Commented:
Edited text of question.
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

jasonclarkeCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jasonclarkeCommented:
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
KangaRooCommented:
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
KangaRooCommented:
>> 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
KangaRooCommented:
Unless they've overloaded operator,
The bastards ;)
0
jasonclarkeCommented:
> 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
KangaRooCommented:
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
KangaRooCommented:
Excuse me, it is confusing, when using placement new, operator new isn't called.
0
jasonclarkeCommented:
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
jasonclarkeCommented:
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
jasonclarkeCommented:
Sorry for posting that twice, I think my browser went a bit screwy...
0
KangaRooCommented:
Not clear at all. What Meyers refers to as the new operator is referred to as a new-expression in the draft.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.