Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Yes, absolutely. </p> <p>Every block of memory allocated will have a constant overhead of a "header", as well as a small variable part (typically at the end). Exactly how much that is depends on the exact C runtime library used. In the past, I've experimentally found it to be around 32-64 bytes per allocation. The variable part is to cope with alignment - each block of memory will be aligned to some nice even 2^n base-address - typically 8 or 16 bytes. </p> <p>I'm not familiar with how the internal design of <code>std::map</code> or similar works, but I very much doubt they have special optimisations there. </p> <p>You can quite easily test the overhead by:</p> <pre><code>char *a, *b; a = new char; b = new char; ptrdiff_t diff = a - b; cout &lt;&lt; "a=" &lt;&lt; a &lt;&lt; " b=" &lt;&lt; b &lt;&lt; " diff=" &lt;&lt; diff; </code></pre> <p>[Note to the pedants, which is probably most of the regulars here, the above a-b expression invokes undefined behaviour, since subtracting the address of one piece of allocated and the address of another, is undefined behaviour. This is to cope with machines that don't have linear memory addresses, e.g. segmented memory or "different types of data is stored in locations based on their type". The above should definitely work on any x86-based OS that doesn't use a segmented memory model with multiple data segments in for the heap - which means it works for Windows and Linux in 32- and 64-bit mode for sure]. </p> <p>You may want to run it with varying types - just bear in mind that the diff is in "number of the type, so if you make it <code>int *a, *b</code> will be in "four bytes units". You could make a <code>reinterpret_cast&lt;char*&gt;(a) - reinterpret_cast&lt;char *&gt;(b);</code> </p> <p>[diff may be negative, and if you run this in a loop (without deleting <code>a</code> and <code>b</code>), you may find sudden jumps where one large section of memory is exhausted, and the runtime library allocated another large block]</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload