Overloading new[]

Started by
8 comments, last by Zahlman 18 years, 4 months ago
Just a quick question or 2. I have a class called SMemoryO that I'm overloading the new[] operator in. So it'll look something like: SmartPointer newPointer = new SMemoryO[20]; Is there a way to fetch '20' as a parameter? Also, in the definition I have: inline void* operator new[](size_t size) Could I change that to? inline SMemoryO* operator new[](size_t size) (The overloaded new operator is inside a class by the way!)
We should do this the Microsoft way: "WAHOOOO!!! IT COMPILES! SHIP IT!"
Advertisement
Quote:Original post by dbzprogrammer
Just a quick question or 2. I have a class called SMemoryO that I'm overloading the new[] operator in.

So it'll look something like:

SmartPointer newPointer = new SMemoryO[20];

Is there a way to fetch '20' as a parameter?

Also, in the definition I have:

inline void* operator new[](size_t size)

Could I change that to?

inline SMemoryO* operator new[](size_t size)


the 20 is the parameter in size. it may be more than 20, but it will be at least 20

i dont think you could overload it that way, since you only truly overload a function when the signature, it ignores the return types.

if you do this - i *think* that the compiler will become confused.
( the original void* operator new[]( size_t ) will still exist. it cannot tell which function to call )

what does class SMemoryO do?
Hold's a memory object. It's pretty much a
template<Type>...T* data

thing.

Also, how can size_t size be the size? In the normal new operator it determines the actual size in bytes of that object. So saying:

char* i = new char;

Would pass to the new function a parameter value of 1 because that's the size... Right?
We should do this the Microsoft way: "WAHOOOO!!! IT COMPILES! SHIP IT!"
(The overloaded new operator is inside a class by the way!)
We should do this the Microsoft way: "WAHOOOO!!! IT COMPILES! SHIP IT!"
Quote:Original post by dbzprogrammer
Hold's a memory object. It's pretty much a
template<Type>...T* data

thing.


i would recommend against this kind of usage.

i think it would be better to provide a static member function to create them.

overloading new is probably not the way to go.

it really depends on what your doing. is it supposed to be some kindof auto-pointer?
Quote:Original post by rip-off
Quote:Original post by dbzprogrammer
Hold's a memory object. It's pretty much a
template<Type>...T* data

thing.


i would recommend against this kind of usage.

i think it would be better to provide a static member function to create them.

overloading new is probably not the way to go.

it really depends on what your doing. is it supposed to be some kindof auto-pointer?


It's super complicated. Yes, I've already debated how to go about this. I'm doing this so my fellow coders don't acciedently make objects without references to them. This creates object deletion because of my garbage collector.
We should do this the Microsoft way: "WAHOOOO!!! IT COMPILES! SHIP IT!"
Quote:Original post by dbzprogrammer
(The overloaded new operator is inside a class by the way!)


in that case why not make a static member function that does the same thing. it would be more meaningful for those trying to read the code. most people expect new to do one thing only. most of the time if you want to overload new it is because you want to give it arguments
If I'm understanding correctly what you're trying to do then don't do it! operator new[] is only supposed to allocate memory. Nothing more, nothing less. In particular you cannot assume that objects of that exact class will be stored in the memory and you cannot assume that only objects will be stored in that memory. operator new[] is called by new[]. new[] first calls operator new[] to allocate memory. The amount of memory requested may include some overhead for bookkeeping. new[] then constructs objects into the array and returns a pointer to the first element. Since operator new[] is inherited it could also be an array of derived objects which is being allocated. For example the following code snippet:
#include <iostream>class Base{	public:		static void * operator new[](std::size_t size)		{			std::cout << "request for " << size << " bytes\n";			return ::operator new(size);		}		static void operator delete[](void * memory, std::size_t size)		{			::operator delete(memory);		}	private:		int i;};class Derived	:	public Base{	private:		int j;};int main(){	Derived * da = new Derived[4];	delete[] da;}

prints either request for 32 bytes or request for 36 bytes depending on which compiler I compile it under (the latter includes 4 bytes of bookkeeping information).

Follow rip-off's advice and go with the static constructor.

Enigma
Ok, thank you! I know now what I'm doing. I will continue what I'm trying to do. New is allocating memory, but not making multiples of that object, it's allocating the memory object inside of SMemoryO (T* data).
We should do this the Microsoft way: "WAHOOOO!!! IT COMPILES! SHIP IT!"
Quote:Original post by dbzprogrammer
It's super complicated. Yes, I've already debated how to go about this. I'm doing this so my fellow coders don't acciedently make objects without references to them. This creates object deletion because of my garbage collector.


Educate your fellow programmers instead. Get them to use proper tools and to avoid doing manual memory management as much as possible. And get them to fix their bugs.

If you really, really need "garbage collection", look into a pre-existing GC for C++, such as Boehm's (google it). If you only need reference counting, consider the smart pointers provided by the Boost library.

This topic is closed to new replies.

Advertisement