C++ Some Stack questions -
let me start saying have read this tutorial , have read this question. questions are:
how big can stack ? processor/architecture/compiler dependent ?
is there way know how memory available function/class stack , how being used in order avoid overflows ?
using modern compilers (say gcc 4.5) on modern computer (say 6 gb ram), need worry stack overflows or thing of past ?
is actual stack memory physically on ram or on cpu cache(s) ?
how faster stack memory access , read compared heap access , read ? realize times pc specific, ratio enough.
i've read not advisable allocate big vars/objects on stack. how big ? this question here given answer of 1mb thread in win32. how thread in linux amd64 ?
i apologize if questions have been asked , answered already, link welcome !
- yes, limit on stack size varies, if care you're probably doing wrong.
- generally no can't information how memory available program. if obtain such information, stale before use it.
- if share access data across threads, yes need serialize access unless they're strictly read-only.
- you can pass address of stack-allocated object thread, in case (again) have serialize unless access strictly read-only.
- you can overflow stack on modern machine lots of memory. stack limited small fraction of overall memory (e.g., 4 mb).
- the stack allocated system memory, used enough @ least top page or 2 typically in cache @ given time.
- being part of stack vs. heap makes no direct difference access speed -- 2 typically reside in identical memory chips, , @ different addresses in same memory chip. main difference stack contiguous , heavily used, top few pages in cache. heap-based memory typically fragmented, there's greater chance of needing data that's not in cache.
- little has changed respect maximum size of object should allocate on stack. if stack can larger, there's little reason allocate huge objects there.
- the primary way avoid memory leaks in c++ raii (aka sbrm, stack-based resource management).
- smart pointers large subject in themselves, , boost provides several kinds. in experience, collections make bigger difference, basic idea largely same either way: relieve programmer of keeping track of every circumstance when particular object can used or should freed.
Comments
Post a Comment