## Recommended Posts

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!)

##### Share on other sites
rip-off    10979
Quote:
 Original post by dbzprogrammerJust 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?

##### Share on other sites
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?

##### Share on other sites
(The overloaded new operator is inside a class by the way!)

##### Share on other sites
rip-off    10979
Quote:
 Original post by dbzprogrammerHold's a memory object. It's pretty much a template...T* datathing.

i would recommend against this kind of usage.

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

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

##### Share on other sites
Quote:
Original post by rip-off
Quote:
 Original post by dbzprogrammerHold's a memory object. It's pretty much a template...T* datathing.

i would recommend against this kind of usage.

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

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.

##### Share on other sites
rip-off    10979
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

##### Share on other sites
Enigma    1410
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).

Enigma

##### Share on other sites
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).

##### Share on other sites
Zahlman    1682
Quote:
 Original post by dbzprogrammerIt'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.