Thread View: gnu.g++
3 messages
3 total messages
Started by User Jdsmith
Thu, 15 Feb 1996 22:23
Overloaded new operator in C++
Author: User Jdsmith
Date: Thu, 15 Feb 1996 22:23
Date: Thu, 15 Feb 1996 22:23
32 lines
1459 bytes
1459 bytes
I am experiencing a problem with overloading the new operator when allocating an array of classes. I do the following and my overloaded new operator is called as expected: ptr = new ClassThatOverloadsNew; But when I do this: ptr = new ClassThatOverloadsNew[5]; The default new operator is called. What needs to be done to get an overloaded new operator to be called when allocating an array of data types? Does C++ even allow this to be done? I have checked several C++ manuals and none explicitely state that this can be done (and none say that it cannot be done!). I assumed that the overloaded new operator would be called with the number of bytes (the first parameter) being set to: sizeof(class) * arrayCount But this is not the case. Does anybody know how to solve this problem? I'm sure it is supported and is simple to do, I just have not found the right documentation on how to do it. My assumptions have been wrong so far, so I need some help. Thanks in advance, John Smith Systems Engineer, Electronic Data Systems
Re: Overloaded new operator in C++
Author: Enno Sandner
Date: Fri, 16 Feb 1996 09:39
Date: Fri, 16 Feb 1996 09:39
71 lines
2598 bytes
2598 bytes
User Jdsmith wrote:
>
> I am experiencing a problem with overloading the
> new operator when allocating an array of classes.
>
> I do the following and my overloaded new operator is
> called as expected:
>
> ptr = new ClassThatOverloadsNew;
>
> But when I do this:
>
> ptr = new ClassThatOverloadsNew[5];
>
> The default new operator is called.
>
> What needs to be done to get an overloaded new operator to be called
> when allocating an array of data types? Does C++ even allow this
> to be done? I have checked several C++ manuals and none explicitely
> state that this can be done (and none say that it cannot be done!).
> I assumed that the overloaded new operator would be called with the
> number of bytes (the first parameter) being set to:
>
> sizeof(class) * arrayCount
>
> But this is not the case. Does anybody know how to solve this problem?
> I'm sure it is supported and is simple to do, I just have not found the
> right documentation on how to do it. My assumptions have been wrong so
> far, so I need some help.
>
You have to provide array new/delete operators.
For instance, the subsequent code shows how to overload the appropriate
allocation functions for a class 'A':
>>>>
#include <iostream.h>
#include <new.h>
struct A {
void* operator new (size_t t) {
cout << "new\n";
return :: operator new(t);
}
void* operator new[] (size_t t) {
cout << "new[]\n";
return :: operator new[](t);
}
void operator delete(void* p) {
cout << "delete\n";
:: operator delete(p);
}
void operator delete[](void* p) {
cout << "delete[]\n";
:: operator delete(p);
}
};
int main()
{
delete new A;
delete[] new A[5];
return 0;
}
<<<<
When an array of objects of type 'A' is created 'A:: operator new[](size_t)' is called.
'A:: operator delete[](size_t)' is used to release the allocated memory when the array
is destructed.
Enno
Re: Overloaded new operator in C++
Author: Lars Toeffner-Clausen
Date: Mon, 19 Feb 1996 08:17
Date: Mon, 19 Feb 1996 08:17
25 lines
1514 bytes
1514 bytes
In article <4g0boe$c37@maverick.tad.eds.com> venkat@nemesis.pln.rboc.eds.com (User Jdsmith) writes: > I am experiencing a problem with overloading the > new operator when allocating an array of classes. You need two functions - which can also be defined on a per-class basis: void *operator new(size_t size); void *operator new[](size_t size); > I assumed that the overloaded new operator would be called with the > number of bytes (the first parameter) being set to: > > sizeof(class) * arrayCount This is true - also for the new[] operator. -- "We need a plan to diverge from", Fesser -- .---------------------------------------------------------. | Lars Toeffner-Clausen e-mail: lastec@dmi.min.dk | | Oersted Science Data Center Phone: +45 3915 7497 | | Lyngbyvej 100 Fax: +45 3915 7460 | | DK - 2100 Koebenhavn Oe | `---------------------------------------------------------'
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads