C/C++ - Why is the heap so big when I'm allocating space for a single int? -


i'm using gdb see effects of low level code. right i'm doing following:

int* pointer = (int*)calloc(1, sizeof(int)); 

yet when examine memory using info proc mappings in gdb, see following after presume .text section (since objfile shows name of binary i'm debugging):

... start addr    end addr    size     offset    objfile 0x602000      0x623000    0x21000  0x0       [heap] 

how come heap big when did allocating space single int?

the weirdest thing is, when i'm doing calloc(1000, sizeof(int)) size of heap remains same.

ps: i'm running ubuntu 14.04 on x86_64 machine. i'm compiling source using g++ (yes, know shouldn't use calloc in c++, test).

how come heap big when did allocating space single int?

i did simple test on linux. when 1 calls calloc glibc calls @ point sbrk() memory os:

(gdb) bt #0  0x0000003a1d8e0a0a in brk () /lib64/libc.so.6 #1  0x0000003a1d8e0ad7 in sbrk () /lib64/libc.so.6 #2  0x0000003a1d87da49 in __default_morecore () /lib64/libc.so.6 #3  0x0000003a1d87a0aa in _int_malloc () /lib64/libc.so.6 #4  0x0000003a1d87a991 in malloc () /lib64/libc.so.6 #5  0x0000003a1d87a89a in calloc () /lib64/libc.so.6 #6  0x000000000040053a in main () @ main.c:6 

but glibc not ask os 4 bytes have asked. glibc calculates own size. how done in glibc:

  /* request enough space nb + pad + overhead */   size = nb + mp_.top_pad + minsize; 

mp_.top_pad default 128*1024 bytes main reason why when ask 4 bytes system allocates 0x21000 bytes.

you can adjust mp_.top_pad call mallopt. mallopt's doc:

m_top_pad  parameter defines amount of padding employ when calling sbrk(2) modify program break.  (the measurement unit parameter bytes.)  parameter has effect in following circumstances:  *  when program break increased, m_top_pad bytes  added sbrk(2) request.  in either case, amount of padding rounded system page boundary. 

so changed progam , added mallopt:

#include <stdlib.h> #include <malloc.h> int main() {   mallopt(m_top_pad, 1);   int* pointer = (int*)calloc(1, sizeof(int));   return 0; } 

i set 1 byte padding , according doc must be rounded system page boundary.

so gdb tells me program:

      start addr           end addr       size     offset objfile         0x601000           0x602000     0x1000        0x0 [heap] 

so heap 4096 bytes. size of page:

(gdb) !getconf page_size 4096 

useful links:


Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -