Class Notes (809,120)
Canada (493,535)
CMPT 225 (60)
John Edgar (28)

CMPT 225 Week 1 Lecture 2

3 Pages
Unlock Document

Simon Fraser University
Computing Science
CMPT 225
John Edgar

How do you actually implement a stack? Template class: class you write, can use to work with any time, very useful for data structures. Most of what we write in this course will be templated. Main Memory/RAM: Breakdown of main memory: static, automatic, dynamic. Static: used for global vars (can access from anywhere in program, they are bad, unless constants), when execute program w/ series of global vars, know exactly how much memory needed to store. Must be reserved space through lifetime of program. Doesn't change. Hence the name. Automatic: Used for variables declared in functions (at least most of them). Call Stack is mechanism for managing automatic memory. Have function, starts of declaring things, those variables stored in automatic memory. Management fairly straightforward. Call stack starts at some address in MM, hit first var (x), reserve 4 bytes of space. Next var (y), reserve 4 bytes of space immediately after first (x). main () { int x; int y; f1(x); int z = f2(); f1(); } void f1(int a) { double d; } int f2( ) { int arr[10]; ... return 13; } Hit f1(), reserve space for a -> next 4 bytes after y. Then next bytes for d. Finished executing f1; no longer need those Hit f2(), start allocating space there to where f1() had previously started. In C and C++, arrays are stored in automatic memory on the call stack. If you are used to Java, this sounds wrong. Reserves space for those 10 ints in automatic memory (adjacent). Returns 13, goes to z; no longer need the space for f2(), so z goes where f2() started. Just rewrite over stuff after we finish calling them. Cannot use automatic memory for variable which change size. Changing values is fine. Dynamic: used for variables that change size, lots of other things. Fundamental mechanism is the same in Java and C++. C++ version: Circle* c = new Circle(12);
More Less

Related notes for CMPT 225

Log In


Don't have an account?

Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.