0

I am implementing a custom memory allocation process and am globally overriding new, new[] and delete, delete[]. The memory allocation takes the size of the object and an optional 'nObjects' parameter where its the amount of objects to be created (defaults to 1).

new works fine, but with new[] the size_t value just returns the total value of nObjects*sizeOfObject. So i have no way of knowing the number of objects that are actually being allocated, just the size.

I looked in the assembly and the number of objects is definitely there, being passed around, but my question is: Is there a safe way to access it within the scope of the global new[] override?

Thanks.

EDIT:

Btw, I'm creating the custom allocation so that any other class that is created with it does not need to do anything extra to account for memory allocation. eg. I can just create class whatever{} and allocate it, I don't need to derive it from anything or put anything extra in its constructor/destructor.

The reason i'm doing this is too account for some external uses of new that i have no control over other than in the global new override.

Luke Bourne
  • 295
  • 2
  • 11
  • 3
    Note: A new expression invokes two calls: 1) memory allocation with operator new. 2) construction of the object(s). You should not care about the second in your custom operators new. –  Sep 09 '16 at 11:50
  • I'm doubtful otherwise [this](http://stackoverflow.com/questions/3868962/how-do-you-get-the-size-of-array-that-is-passed-into-the-function) would be less of an issue. – NathanOliver Sep 09 '16 at 11:51
  • ... see also http://en.cppreference.com/w/cpp/language/new –  Sep 09 '16 at 11:55
  • @Dieter Lücking Hey, My memory allocation doesn't deal with the construction, it just uses the nObjects number to keep track of the total number of objects to be allocated. – Luke Bourne Sep 09 '16 at 11:56
  • @LukeBourne You may consider a trackable base class and count there (construction/destruction) –  Sep 09 '16 at 12:03
  • If you need to know the number of objects, you can use the in-class versions -- like option 16 here: http://en.cppreference.com/w/cpp/memory/new/operator_new From my reading of that page, all the calls to new/new[] are given the number of bytes, not the number of objects. "The standard library implementation allocates count bytes from free store." – xaxxon Sep 09 '16 at 12:03
  • Of course that would require overriding it per-class you wanted to track, so that kind of stinks. Also "The memory allocation takes the size of the object and an optional 'nObjects' parameter" -- Where are you seeing that? – xaxxon Sep 09 '16 at 12:10
  • i've added an edit to the original post, as i've not included something thats probably relevent. and @xaxxon, i mean my custom allocator, at the moment i have a macro: Allocate(size_t sizeofobject,size_t nObjects) which works exactly how i want to but i can't embed it inside new[] unless i can get an nObjects number from new[] – Luke Bourne Sep 09 '16 at 12:13
  • @LukeBourne The only options with custom arguments are the in-class versions. For example, lines 19 and 20: http://en.cppreference.com/w/cpp/memory/new/operator_new – xaxxon Sep 09 '16 at 12:15
  • @xaxxon, yeah its looking that way :/ thanks for the help guys. My allocation still works, it just messes up my tracking with i might have to rethink. – Luke Bourne Sep 09 '16 at 12:18
  • new/delete are for bytes. if you want to track objects, track it at the class level. As someone else suggested, just adding a counting base class could suffice. – xaxxon Sep 09 '16 at 12:30

1 Answers1

1

Is there a safe way to access it within the scope of the global new[] override?

No such way.

new operators are just memory allocations functions, they only get the number of bytes they need to allocate (just like malloc).

This is new expression that accepts the number of objects, calls operator new (which you can overload) and then invokes the constructor. new expression cannot be overloaded.

Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271