Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Obj* ptr = new Obj(X)

Reply
Thread Tools

Obj* ptr = new Obj(X)

 
 
Phil Endecott
Guest
Posts: n/a
 
      06-03-2005
Dear All,

I'm experimenting with a class that allocates itself using a nonstandard
allocator:

class Obj {
public:

static void* operator new (size_t sz) {
return my_allocate(sz);
}
static void* operator new[] (size_t sz) {
return my_allocate(sz);
}
static void operator delete (void* p, size_t sz) {
my_deallocate(p, sz);
}
static void operator delete[] (void* p, size_t sz) {
my_deallocate(p, sz);
}
... etc ...
};

This works fine for simple cases, but I am not sure how to deal with
this use of new:

Obj* ptr = new Obj(X)

I believe that this should allocate a new Obj and invoke its
copy-constructor to initialise it with a copy of X. I think that, in
order to support this, I need to implement

static void* operator new (size_t sz, const Obj& o)

Is this right? So, what should this function do? Maybe

obj* p = my_allocate(sz);
*p = o;
return p;

But this seems all wrong. Surely the purpose of operator new is to
allocate the memory, with construction happening later. And what about
subclasses of Obj?

I think I am missing something basic here - can someone help me out?

Cheers, --Phil.
 
Reply With Quote
 
 
 
 
John Carson
Guest
Posts: n/a
 
      06-03-2005
"Phil Endecott" <(E-Mail Removed)> wrote in message
news:zU_ne.2410$(E-Mail Removed)
> Dear All,
>
> I'm experimenting with a class that allocates itself using a
> nonstandard allocator:


[snip]
>
> This works fine for simple cases, but I am not sure how to deal with
> this use of new:
>
> Obj* ptr = new Obj(X)
>
> I believe that this should allocate a new Obj and invoke its
> copy-constructor to initialise it with a copy of X. I think that, in
> order to support this, I need to implement
>
> static void* operator new (size_t sz, const Obj& o)
>
> Is this right?


No.

> So, what should this function do? Maybe
>
> obj* p = my_allocate(sz);
> *p = o;
> return p;
>
> But this seems all wrong. Surely the purpose of operator new is to
> allocate the memory, with construction happening later. And what about
> subclasses of Obj?
>
> I think I am missing something basic here - can someone help me out?


Any user-defined operator new is responsible solely for memory allocation.
Constructor calls will be handled by the compiler, just as if you were using
the standard operator new.

--
John Carson

 
Reply With Quote
 
 
 
 
Phil Endecott
Guest
Posts: n/a
 
      06-03-2005
John Carson wrote:
> Phil Endecott wrote
>> I am not sure how to deal with
>> this use of new:
>>
>> Obj* ptr = new Obj(X)
>>
>> I think that, in
>> order to support this, I need to implement
>>
>> static void* operator new (size_t sz, const Obj& o)
>>
>> Is this right?


> No.
> Any user-defined operator new is responsible solely for memory allocation.
> Constructor calls will be handled by the compiler, just as if you were
> using the standard operator new.


Thanks John. That makes sense.

I found myself thinking the wrong way because of an error message:

/usr/include/c++/3.3/bits/stl_construct.h:78: error: no matching
function for
call to `shm_vec_char:perator new(unsigned int, void*&)'
.../apachemod/shm_alloc.h:278: error: candidates are: static void*
Shm::Obj:perator new(unsigned int)

I am trying to store these things in a std::vector, and there is more
going on than I have explained. I'll post again if I can come up with a
sufficiently simple example.

--Phil.
 
Reply With Quote
 
Mark P
Guest
Posts: n/a
 
      06-03-2005
Phil Endecott wrote:
> John Carson wrote:
>
>> Phil Endecott wrote
>>
>>> I am not sure how to deal with
>>> this use of new:
>>>
>>> Obj* ptr = new Obj(X)
>>>
>>> I think that, in
>>> order to support this, I need to implement
>>>
>>> static void* operator new (size_t sz, const Obj& o)
>>>
>>> Is this right?

>
>
>> No.
>> Any user-defined operator new is responsible solely for memory
>> allocation.
>> Constructor calls will be handled by the compiler, just as if you were
>> using the standard operator new.

>
>
> Thanks John. That makes sense.
>
> I found myself thinking the wrong way because of an error message:
>
> /usr/include/c++/3.3/bits/stl_construct.h:78: error: no matching
> function for
> call to `shm_vec_char:perator new(unsigned int, void*&)'
> ../apachemod/shm_alloc.h:278: error: candidates are: static void*
> Shm::Obj:perator new(unsigned int)
>
> I am trying to store these things in a std::vector, and there is more
> going on than I have explained. I'll post again if I can come up with a
> sufficiently simple example.
>
> --Phil.


I believe it's more complicated if you want to store your objects in an
STL class.

If you just store pointers nothing really special happens, but since STL
classes own their objects and allocate memory for them via their own
allocator objects, it's unlikely that your operator new will ever be
called.

Instead I think STL classes will invoke placement new after allocating
the memory themselves. This is in fact what your error message
indicates-- notice the function it's looking for looks like: operator
new(unsigned int, void*). The unsigned int is size_t on your system,
and the void* is the location for the new object.

By defining your own operator new you hide the global placement new.
Try adding:

void* operator new(size_t size, void* loc) {return :perator
new(size,loc);}

to your class definition, which will restore access to global placement new.

Hope that helps,
Mark
 
Reply With Quote
 
Phil Endecott
Guest
Posts: n/a
 
      06-03-2005
Mark P wrote:
> By defining your own operator new you hide the global placement new. Try
> adding:
>
> void* operator new(size_t size, void* loc) {return :perator
> new(size,loc);}


Thanks! I didn't know it would be hidden.
I presume I also need a version for new[] :

void* operator new[] (size_t size, void* loc) {
return :perator new[](size,loc);
}

--Phil.
 
Reply With Quote
 
Mark P
Guest
Posts: n/a
 
      06-03-2005
Phil Endecott wrote:
> Mark P wrote:
>
>> By defining your own operator new you hide the global placement new.
>> Try adding:
>>
>> void* operator new(size_t size, void* loc) {return :perator
>> new(size,loc);}

>
>
> Thanks! I didn't know it would be hidden.
> I presume I also need a version for new[] :
>
> void* operator new[] (size_t size, void* loc) {
> return :perator new[](size,loc);
> }
>
> --Phil.


I'm not an expert but I imagine it can't hurt. Not sure how often
placement new[] is actually used though. I don't think I've ever
encountered an STL allocator which attempted to use this.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
difference between *ptr++ and ++*ptr ? Jason C Programming 19 05-19-2005 04:50 PM
is (!ptr) or (ptr) valid way to check for NULL or NOT NULL? G Fernandes C Programming 9 02-27-2005 03:07 AM
const ptr to const ptr ? franco ziade C Programming 3 02-17-2005 04:30 AM
How to convert a double **ptr in a double const ** const ptr? Heiko Vogel C Programming 3 09-14-2004 10:23 AM
what's the difference between delete ptr and ptr=0 -dont they accomplish the same Sid C++ 5 07-29-2004 03:42 AM



Advertisments