04d_dynmem

04d_dynmem - Data Structures CSCI 102 CS102 C Pointers...

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

View Full Document Right Arrow Icon
1 Data Structures - CSCI 102 Copyright © William C. Cheng CS102 Dynamic Objects Bill Cheng http://merlot.usc.edu/cs102-s11
Background image of page 1

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

View Full DocumentRight Arrow Icon
Polymorphism (Ch 13) Virtual functions Abstract classes Interfaces C Structs (Ch 10) Topics to cover 2 Data Structures - CSCI 102 Copyright © William C. Cheng C++ Object-Oriented Programming C++ Classes (Ch 11) Constructors Destructors Member functions Exception Handling (Ch 15) Namespaces (Ch 8) Operator Overloading (Ch 14) Class Composition & Inheritance (Ch 12)
Background image of page 2
When you create variables normally, they live on the stack and their lifetime is tied to their scope (the { }) int foo() { int x = 5 ; return x + 1 ; } The variable x is a stack variable Memory for x is created on the stack when foo() is called Memory for x is freed from the stack when foo() returns C++ manages memory for stack variables for you. They are automatically created and destroyed. stack allocation is mostly handled by the compiler 3 Data Structures - CSCI 102 Copyright © William C. Cheng Stack Variables
Background image of page 3

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

View Full DocumentRight Arrow Icon
Sometimes stack variables can be really inconvenient 4 Data Structures - CSCI 102 Copyright © William C. Cheng Stack Variables int * bar() { int x = 5 ; return &x; } This is a really bad idea because x lives on the stack and it will be destroyed when bar() returns We’ll be returning a pointer to unusable memory What if we want the pointer to x to stick around long after bar() has been called? For example, bar() reads a bunch of stuff from a file to be processed later What if we don’t want C++ to manage memory for us? Use data structures to manage data ourselves
Background image of page 4
By default all variables go on the stack You MUST clean up after yourself! The heap is large pool of free memory in your C++ application that is used for dynamic variable allocation 5 Data Structures - CSCI 102 Copyright © William C. Cheng Heap Variables and the new Operator You must explicitly allocate variables on the heap The heap is not automatically memory managed for you like the stack is The C++ operator new can be used to dynamically create new variables on the heap When you use the new operator, you’re telling C++ that it should NOT manage memory for you
Background image of page 5

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

View Full DocumentRight Arrow Icon
Since you’re dynamically creating variables on the fly, their location is not known in advance The new operator returns a pointer to the new memory it has allocated 6 Data Structures - CSCI 102 Copyright © William C. Cheng The new Operator Create a pointer to a new integer on the heap : int * x = new int ; *x = 55 ; Or all in one statement: int * x = new int ( 55 );
Background image of page 6
When you use the new operator, you’re telling C++ that you will manage your own memory 7 Data Structures - CSCI 102 Copyright © William C. Cheng The delete Operator If you use new to allocate memory like this: int * x = new int ( 55 ); Then later you must use delete to clean up the previously allocated memory like this: delete x;
Background image of page 7

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

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/17/2011 for the course CSI 102 taught by Professor Billcheng during the Spring '11 term at USC.

Page1 / 28

04d_dynmem - Data Structures CSCI 102 CS102 C Pointers...

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

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