C++Guns – RoboBlog

10.12.2010

C/C++ eine static Variable liegt im Heap

Filed under: Allgemein — Tags: , — Thomas @ 16:12

Eine statische Variable liegt im Heap, statt auf dem Stack. Das erklärt auch, warum das Programm meines Chefs funktionierte, nachdem er vorsorgshalber alle 200 Variablen im Datapool als static deklarierte. Der Stack ist nur einige MB groß (Wie groß genau? Infos suchen!). Fordert man ein entsprechend großes Array an, stürzt das Programm ab.

Gefunden im Buch "Multicore Programmierung" von Intel. Seite 154 ganz Oben.
ISBN 978-3939084-70-9

Im Regelfall sind Variablen, die auf dem Stack deklariert wurden, privat; aber das C/C++ Schlüsselwirt static sorgt dafür, dass die Variable auf dem globalen Heap abgelegt werden und so für alle OpenMP-Schleifen sichbar sind

Noch mehr Infos hab ich hier gefundn http://stackoverflow.com/questions/408670/stack-static-and-heap-in-c

Summary of what static, heap, and stack memory are:

  • A static variable is basically a global variable, even if you cannot access it globally. Usually there is an address for it that is in the executable itself. There is only one copy for the entire program. No matter how many times you go into a function call (or class) (and in how many threads!) the variable is referring to the same memory location.
  • The heap is a bunch of memory that can be used dynamically. If you want 4kb for an object then the dynamic allocator will look through its list of free space in the heap, pick out a 4kb chunk, and give it to you. Generally, the dynamic memory allocator (malloc, new, et c.) starts at the end of memory and works backwards.
  • Explaining how a stack grows and shrinks is a bit outside the scope of this answer, but suffice to say you always add and remove from the end only. Stacks usually start high and grow down to lower addresses. You run out of memory when the stack meets the dynamic allocator somewhere in the middle (but refer to physical versus virtual memory and fragmentation). Multiple threads will require multiple stacks (the process generally reserves a minimum size for the stack).
  • When you would want to use each one:

  • Statics/globals are useful for memory that you know you will always need and you know that you don't ever want to deallocate. (By the way, embedded environments may be thought of as having only static memory... the stack and heap are part of a known address space shared by a third memory type: the program code. Programs will often do dynamic allocation out of their static memory when they need things like linked lists. But regardless, the static memory itself (the buffer) is not itself "allocated", but rather other objects are allocated out of the memory held by the buffer for this purpose. You can do this in non-embedded as well, and console games will frequently eschew the built in dynamic memory mechanisms in favor of tightly controlling the allocation process by using buffers of preset sizes for all allocations.)
  • Stack variables are useful for when you know that as long as the function is in scope (on the stack somewhere), you will want the variables to remain. Stacks are nice for variables that you need for the code where they are located, but which isn't needed outside that code. They are also really nice for when you are accessing a resource, like a file, and want the resource to automatically go away when you leave that code.
  • Heap allocations (dynamically allocated memory) is useful when you want to be more flexible than the above. Frequently, a function gets called to respond to an event (the user clicks the "create box" button). The proper response may require allocating a new object (a new Box object) that should stick around long after the function is exited, so it can't be on the stack. But you don't know how many boxes you would want at the start of the program, so it can't be a static.
  • No Comments

    No comments yet.

    RSS feed for comments on this post.

    Sorry, the comment form is closed at this time.

    Powered by WordPress