简体   繁体   中英

is it possible to get memory block size allocated by 'new'?

Hello i need to record my heap, but now i am just thinking to overload 'new' operator with my function.

I need to summary the real count bytes of memory that was increased after malloc() or Heap*() or other windows mem* functions

But for now i need to analyze current heap implementation. Is it possible to get blocks size like allocated by HeapAlloc() function - HeapSize()?

.

I can see that you did not search the documentation.

HeapSize() exists.


Edit On reflection, perhaps you were asking for an alternative to HeapSize() that you can use when you performed allocation yourself with new .

The answer is no. The standard allocation routines don't have anything to grab information about the memory block, because:

  • That's highly implementation-dependent, and
  • You already know the block size (because you specified it in the first place), so what would be the point of the bloat?

In fact HeapSize() is the implementation-dependent function for Windows that does this, but you can only use it when you performed a HeapAlloc() .

There's [also non-standard] _msize that can be used with malloc and friends, but new may not use malloc .

Therefore I suggest that you just track the sizes yourself in your allocator.

_msize

According to the documentation it works for calloc,malloc and realloc .

However, at least under Visual Studio, using the default allocator, it works also for new .

It is not a good idea to use it, however, it may do the work for your analysis.

One more thing:

External tools such as VMMap may help for such analysis.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM