c++ - where does an automatic object live in( with a demo ) -


i want research in memory management of c++ , it's implementations g++, vc++.

the first question automatic object(local object) live in?(built-in type, user-defined type, stl...)

i think built-in type stored in stack done in compiling step. , what's fact user-defined type? see somewhere before stl data type in heap memory. wrote tiny function, compiled g++, disassembled using objdump, see compiler did.

#include <string>  void autovar(){     std::string s; } 

and result of disassembling follows:

00000000 <__z7autovarv>:    0:   55                      push   %ebp //push old frame pointer     1:   89 e5                   mov    %esp,%ebp //ebp point old    3:   83 ec 28                sub    $0x28,%esp//allocate stack space    6:   8d 45 f4                lea    -0xc(%ebp),%eax//param or something??    9:   89 04 24                mov    %eax,(%esp)    c:   e8 00 00 00 00          call   11 <__z7autovarv+0x11>   11:   8d 45 f4                lea    -0xc(%ebp),%eax   14:   89 04 24                mov    %eax,(%esp)   17:   e8 00 00 00 00          call   1c <__z7autovarv+0x1c>   1c:   c9                      leave   1d:   c3                      ret   1e:   90                      nop   1f:   90                      nop 

so can understand first 3 lines, need understand rest

thanks attention!

standard disclaimer: implementation could work entirely differently, on x86 or similar, act described below.

when define object auto storage duration, object itself going allocated on stack. so, let's consider simplified version of vector:

template <class t, class allocator = std::allocator<t> > class vector {     t *data;     size_t currently_used;     size_t allocated; public:     // ... }; 

so, when allocate vector, object itself (the storage data pointer, currently_used , allocated counters) gets allocated on stack.

assuming typical 32-bit machine pointers , size_ts 32-bits each, that'll mean 12 bytes of space on stack. simpler types (e.g., int or long) , conceivably vector, we'd expect see locals allocated in registers in quite few cases. compiler chooses allocate in registers based on (guesses at) used most. on machine sparc or itanium has lots of registers, can expect most local/auto variables in registers. on x86 have enough fewer registers stack usage pretty common (though x86-64 doubles available registers, helps quite bit).

the vector uses allocator object storage elsewhere (typically, not free store) store data care (i.e., elements store in vector).

looking @ specific details of code included: looks me of rest of code invoking constructor , destructor std::string object. unfortunately, you've used horrible at&t syntax, makes unreadable.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -