Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello folks,

i would be heavily interested, to know where a standard/normal "operator new"-function implementation does store the size of the variable. I know from the C++ specification, that the operator new expression expands to a call of the operator new function, of the appropriate class. This call passes the size_t of the memory to allocate.

But where does the function store the size? I mean, the delete and the sizeof operator must get this size from somewhere?!
My first idea was, it would be stored in-front of the data (BSTR like), but i have never seen anything else than the data itself in the memory view window.
For example, if i create an int with value 3, there is only the "0x03" itself at the memory, no size before or behind...


greetings,

Allgaeuer
Posted

 
Share this answer
 
Operator new inside usually calls the API function HeapAlloc. Operator delete calls HeapFree. As you can see, this is all WinAPI inside. Now all you need is to use heap api.
Memory is allocated by blocks, much like a linked list. All you need is to walk these blocks. Try using HeapWalk.
 
Share this answer
 
There are numerous implementations of the new operator from one compiler to the next, plus there are many ways to allocate different types of memory, such as local or global, or in stack or in the heap.

Read about memory management on the platform where your question applies, and about the exact methods of memory allocation that you use.

Beside that, I can only tell you where memory pointer is allocated physically on the x86 platform, regardless of some high-level operators, if you are curious ;) And it goes like this...(no, u think u're ready?) :)

First of all, selector register points at either GDT[^] or LDT[^], depending on its bit 2 status. Then the address register points at either memory itself, in case of flat memory model, or page index + offset, in case of the virtual mode. The bottom line, memory is physically allocated in pages these days, and the swap flag is supported by the x86 processors, so it can trigger an interrupt wherever a task is trying to access a page marked as swapped and the handler loads it up. Yep, that old chestnut. And this is how it works in Windows too ;)

Do you still want to know where your memory size is stored? :) :) :) Memory size within a process is always virtual, not real, and it can be stored anywhere, as long as the supporting API knows how to deal with it. Welcome to the virtual age, my friend ;)

P.S. It's not the size, mate, it's how you use it :) :) :)
 
Share this answer
 
v7
That is totally dependent on the allocator that is used by the default implementation of the global new/delete operators. The default global/new delete operators are totally dependent on your compiler (on its standard library to be accurate). You can even override the default global new/delete operators and provide your own implementation and then you are free to work with your free memory and to optimize the speed of allocation/deallocation. You don't even have to store the size near to the chunk if your allocator library is able to tell the size for a pointer.
Overriding the global new/delete operators is a common technique to introduce some advanced debugging, and to collect statistics about the most commonly allocated chunk sizes to introduce a faster allocator (usually a linked array) just for the those sizes. (This helps avoiding fragmentation too!)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900