char* a = new char;
wastes at least 15 bytes.
Is this right ?
How much memory is wasted by internal memory allocation info the operating system keeps ?
Are there any other advantages (than not wasting memory)(like speed) to allocating larger blocks in a single new ?
And finally, is there any difference between new and malloc ?(other than constructors and other C++ thingies)
How much memory is wasted by new?
Whenever I create an object dynamically in VC, the resulting pointer is 16 byte aligned. So, I figure
May 05, 2001 04:27 AM
All I know, is that malloc uses virtualalloc to get a large chunk of data, whenever needed. I''m not sure whether it calls virtualalloc everytime, or just when the old allocation isn''t big enough.
s fo new/malloc, new is just a call to malloc & the constructors.
s fo new/malloc, new is just a call to malloc & the constructors.
16 byte aligned? I thought the default was 8? In any case, if you''re worried about wasted space due to byte alignment, you can always change the compiler settings of use a #pragma directive.
==========================================In a team, you either lead, follow or GET OUT OF THE WAY.
The actual space is dependent on the specific compiler.
here''s how the memory allocation system works:
you request a chunk of memory
The allocation system finds the first empty space in memory large enough to fit your request
the allocation system creates a ''block'' header, a sort of linked list in memory keeping track of all currently allocated pieces of memory.
some compilers have a minimum block size, usually aligned on a boundary of power 2.
so creating a new char will waste whatever memory is allocated for the block and for the header, which will probably end up being many times larger than the actual char.
===============================================
Have I no control, is my soul not mine?
Am I not just man, destiny defined?
Never to be ruled, nor held to heel!
here''s how the memory allocation system works:
you request a chunk of memory
The allocation system finds the first empty space in memory large enough to fit your request
the allocation system creates a ''block'' header, a sort of linked list in memory keeping track of all currently allocated pieces of memory.
some compilers have a minimum block size, usually aligned on a boundary of power 2.
so creating a new char will waste whatever memory is allocated for the block and for the header, which will probably end up being many times larger than the actual char.
===============================================
Have I no control, is my soul not mine?
Am I not just man, destiny defined?
Never to be ruled, nor held to heel!
This is my signature. There are many like it, but this one is mine. My signature is my best friend. It is my life. I must master it as I must master my life. My signature, without me, is useless. Without my signature, I am useless.
Specific implementations may vary, but most compilers are going to set the four bytes prior to the returned address to the size of the allocation. While anything is possible I don''t think many are actually going to chain all the allocations together. It is your responsibility to do that. All it needs to know is how big the block is when you free it which is the reason the size preceeds the area provided to you.
Keys to success: Ability, ambition and opportunity.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement