C++_memory_management - Overview 4 major memory segments...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
CSE 332: C++ Memory Management Overview 4 major memory segments Global: variables outside stack, heap Code (a.k.a. text): the compiled program Heap: dynamically allocated variables Stack: parameters, automatic and temporary variables Key differences from Java Destructors of automatic variables called when stack frame where declared pops No garbage collection: program must explicitly free dynamic memory Heap and stack use varies dynamically Code and global use is fixed Code segment is “read-only” stack heap code global
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CSE 332: C++ Memory Management Illustrating C++ Memory int g_default_value = 1; int main (int argc, char **argv) { Foo *f = new Foo; f->setValue(g_default_value); delete f; return 0; } void Foo::setValue(int v) { this->m_value = v; } stack heap code global g_default_value Foo *f Foo *this int v m_value crt0 main Foo: setValue
Background image of page 2
CSE 332: C++ Memory Management Illustrating C++ Memory stack code global g_default_value Foo *f Foo *this int v heap int g_default_value = 1; int main (int argc, char **argv) { Foo *f = new Foo; f->setValue(g_default_value); delete f; return 0; } void Foo::setValue(int v) { this->m_value = v; } m_value
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CSE 332: C++ Memory Management Memory, Lifetimes, and Scopes Temporary variables Are scoped to an expression, e.g., a = b + 3 * c; Automatic (stack) variables Are scoped to the duration of the function in which they are declared Dynamically allocated variables Are scoped from explicit creation ( new ) to explicit destruction ( delete ) Global variables Are scoped to the entire lifetime of the program Includes static class and namespace members May still have initialization ordering issues (Singleton Pattern) Member variables
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 15

C++_memory_management - Overview 4 major memory segments...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online