The Stack, Free Store, and Global Namespace

Similar documents
Linked Lists. What is a Linked List?

Post Experiment Interview Questions

MITOCW watch?v=0jljzrnhwoi

Eventually, you'll be returned to the AVD Manager. From there, you'll see your new device.

MITOCW ocw f99-lec07_300k

Formal Methods of Software Design, Eric Hehner, segment 24 page 1 out of 5

In our first lecture on sets and set theory, we introduced a bunch of new symbols and terminology.

In today s video I'm going show you how you can set up your own online business using marketing and affiliate marketing.

Instructor: Craig Duckett. Lecture 03: Tuesday, April 3, 2018 SQL Sorting, Aggregates and Joining Tables

The following content is provided under a Creative Commons license. Your support

The following content is provided under a Creative Commons license. Your support

Skill 1: Multiplying Polynomials

Formal Methods of Software Design, Eric Hehner, segment 1 page 1 out of 5

How to Improve Your Campaign Conversion Rates

Blitz2D Newbies: Definitive Guide to Types by MutteringGoblin

MITOCW MIT6_172_F10_lec18_300k-mp4

Version Copyright Feel free to distribute this guide at no charge...

Hi everyone. I hope everyone had a good Fourth of July. Today we're going to be covering graph search. Now, whenever we bring up graph algorithms, we

MITOCW watch?v=zm5mw5nkzjg

CS103 Spring 2018 Mathematical Vocabulary

PROFESSOR: Last time, we took a look at an explicit control evaluator for Lisp, and that bridged the gap between

mid=81#15143

BEGINNER PHP Table of Contents

MITOCW ocw apr k

Robert Ragan s TOP 3

How to Install Ubuntu on VirtualBox

MITOCW watch?v=rvrkt-jxvko

So on the survey, someone mentioned they wanted to work on heaps, and someone else mentioned they wanted to work on balanced binary search trees.

MITOCW ocw f99-lec12_300k

Introduction to Game Programming Lesson 4 Lecture Notes

The Definitive Guide to Fractal Awesomeness with J-WildFire!

CS93SI Handout 04 Spring 2006 Apr Review Answers

Clickteam Fusion 2.5 Creating a Debug System - Guide

Chapter 1 Getting Started

MITOCW watch?v=hverxup4cfg

Chris' Makefile Tutorial

THE PRAGMATIC INTRO TO REACT. Clayton Anderson thebhwgroup.com WEB AND MOBILE APP DEVELOPMENT AUSTIN, TX

MITOCW watch?v=4dj1oguwtem

The following content is provided under a Creative Commons license. Your support

1 Getting used to Python

MITOCW MIT6_01SC_rec2_300k.mp4

Designing a Database -- Understanding Relational Design

Mathematical Logic Part One

PROFESSOR: So far in this course we've been talking a lot about data abstraction. And remember the idea is that

Lesson 1. Importing and Organizing Footage using Premiere Pro CS3- CS5

BCSWomen Android programming (with AppInventor) Family fun day World record attempt

Initial Alpha Test Documentation and Tutorial

Hello, and welcome to another episode of. Getting the Most Out of IBM U2. This is Kenny Brunel, and

Out for Shopping-Understanding Linear Data Structures English

Lesson 3 Transcript: Part 1 of 2 - Tools & Scripting

MITOCW watch?v=kz7jjltq9r4

Android Programming Family Fun Day using AppInventor

Functions, Pointers, and the Basics of C++ Classes

The following content is provided under a Creative Commons license. Your support

CSE143 Notes for Monday, 4/25/11

Instructor: Craig Duckett. Lecture 04: Thursday, April 5, Relationships

Java Programming Constructs Java Programming 2 Lesson 1

Adobe Photoshop How to Use the Marquee Selection Tools

MITOCW watch?v=se4p7ivcune

Azon Master Class. By Ryan Stevenson Guidebook #11 Squidoo Marketing

Sequential Containers Cont'd

I'm Andy Glover and this is the Java Technical Series of. the developerworks podcasts. My guest is Brian Jakovich. He is the

Well, Hal just told us how you build robust systems. The key idea was-- I'm sure that many of

MITOCW watch?v=9h6muyzjms0

WYBCS Android Programming (with AppInventor) Family fun day

static CS106L Spring 2009 Handout #21 May 12, 2009 Introduction

KMyMoney Transaction Matcher

PROFESSOR: Well, now that we've given you some power to make independent local state and to model objects,

Sequential Containers Cont'd

Note: Please use the actual date you accessed this material in your citation.

CA31-1K DIS. Pointers. TA: You Lu

Well, I hope you appreciate that we have inducted you into some real magic, the magic of

MITOCW watch?v=flgjisf3l78

MITOCW watch?v=w_-sx4vr53m

PROFESSOR: Well, yesterday we learned a bit about symbolic manipulation, and we wrote a rather stylized

An attribute used in HTML that is used for web browsers screen reading devices to indicate the presence and description of an image Module 4

P1_L3 Operating Systems Security Page 1

3. Now go to Edit => Effects => Brightness/Contrast... and notice that you can modify the colors you created above.

Azon Master Class. By Ryan Stevenson Guidebook #5 WordPress Usage

MITOCW watch?v=sdw8_0rdzuw

Sending s With Sendmail - Part 2

Instructor (Mehran Sahami):

An Introduction to Stored Procedures in MySQL 5 by Federico Leven6 Apr 2011

Introduction to Scratch

Intro. Scheme Basics. scm> 5 5. scm>

MITOCW watch?v=tkwnms5irbu

Part 1 Simple Arithmetic

Fortunately, you only need to know 10% of what's in the main page to get 90% of the benefit. This page will show you that 10%.


Who am I? I m a python developer who has been working on OpenStack since I currently work for Aptira, who do OpenStack, SDN, and orchestration

Slide 1 CS 170 Java Programming 1 Testing Karel

Printable View of: Week 13: Miscelaneous cool features. Returns from standard functions. returns from standard functions: scanf(), fopen()

Instructor (Mehran Sahami):

Lesson 3 Transcript: Part 2 of 2 Tools & Scripting

Programming Languages. Streams Wrapup, Memoization, Type Systems, and Some Monty Python

Hi everyone. Starting this week I'm going to make a couple tweaks to how section is run. The first thing is that I'm going to go over all the slides

CaseComplete Roadmap

NetLogo Lab 3 Tutorial: Finding Peaks

MITOCW watch?v=r6-lqbquci0

MITOCW watch?v=yarwp7tntl4

Transcription:

Pointers This tutorial is my attempt at clarifying pointers for anyone still confused about them. Pointers are notoriously hard to grasp, so I thought I'd take a shot at explaining them. The more information out there on pointers, the better. I'll be giving code demonstrations thoughout this tutorial and will also include a downloadable program that you can use to test different aspects of pointers. I'm not sure how much can be gained by copying out the code here line for line, but I do encourage you to play around with the code. First, let's briefly talk about how memory is stored in a computer. You can think of memory as sequential rows of storage spaces, each with its own address. When you declare variables, your program stores the information in one of those spaces (or a group of spaces if it needs the extra room) and remembers where it is. If you have declared a variable and you want to know where your program put it, you can use the address of operator to get the address. int age = 21; // declare the variable on the stack cout << "age: " << age << endl; // print the value stored in age cout << "&age: " << &age << endl; // print the location of the value The Stack, Free Store, and Global Namespace Now let's talk about the difference between the stack, the global namespace, and the free store. The stack is part of the area in memory that is reserved for your program. Any local variables that you declare on the stack (ie. normal variable declarations like "int age;") stay there until your program is finished, at which point that area in memory will automatically be cleaned up. If you declare a variable in a function, loop, or conditional statement to be on the stack, it will only exist until the end of that statement. Afterwards, that part of the stack will be cleared and anything declared there will be gone. Global variables exist in the global namespace and the memory they take up is automatically freed at the end of your program. They act just like variables declared on the stack with the exception that they can be accessed from anywhere in your program.

The memory in your computer that is not being taken up by the stack or the global namespace is called the free store (a.k.a. "the heap"). This area of memory is pretty much fair game for any program that wants to use it. Unlike the stack and the global namespace, variables declared on the freestore exist until you explicitly delete them. If you don't delete them by the end of your program, they will just sit there until you restart your computer. This situation is most commonly called a memory leak and is usually very difficult to detect because there's no easy way to see what is stored on the free store. Before we discuss why we would want to store data on the free store, let's look at how you would declare a variable there. This is also a good time to download the source code that comes with this tutorial, read the comments, and see what you get when you run the program. Here's some code that declares a value on the freestore: // a variable has been declared but set to NULL, nothing has been put on the freestore yet int* width = NULL; // now something is on the freestore (30), and width holds the address that points to it width = new int(30); Don't worry, we're going to get to why we would do this in a minute. First, I want you to really understand what goes on when we declare a pointer. The variable width does not represent the value 30, it just stores the location in memory where 30 exists. If we had another pointer to that location in memory and changed the value there, width would then point to that new value. This is because width doesn't represent the value 30, it just points to whatever is sitting at that location on the free store. Once again please check out the downloadable source for this tutorial. Let's now look at some of the operations we can perform on pointers and then get to why we would use them. // Assume that width has already been declared as a pointer // This prints out whatever is stored in the width variable. In this case, it's the location // on the free store that the pointer points to, not the value it points to cout << "\nwidth: " << width << endl; // This prints out the address of the variable width. This is just like what we did with age

// above. It's the address of the variable, not the value at the variable (which is also an // address). The address we get from this is the address of the location on the stack where // we store the address to the value stored on the free store. cout << "&width: " << &width << endl; // The indirection operator (*) is used to print out the value a pointer points to. In this // case it points to the value 30. cout << "*width: " << *width << endl; // Now that we're done using our pointer, we MUST delete it! delete width; Using Pointers Alright, so why use pointers? Remember when we talked about the stack and how local variables only exist where they're declared? For example, if we declare a variable on the stack within a function, it will only exist until the end of that. But what if we want our data to persist? This happens for all kinds of reasons. We might have some data representing the location of an enemy in a game. We'll need that data to be available in all kinds of functions (collision detection, rendering, AI processing, etc). So let's look at ways we can do this. The first way would be to just declare the enemy data in a function and start passing it to other functions. It'll be a bit of a pain because all of our functions will need an extra parameter, but there's an even worse problem with this method. When you declare a variable on the stack in a function, it only lasts as long as that function. If you try passing that data to another function, the new function can't just use the same place on the stack where the data is stored because it could be erased at any time. Thus, the new function has to make its own room on the stack and copy the data. This process is called "passing by value". Passing by Reference vs. Passing by Value This brings me to the difference between passing by reference and passing by value. When you pass by value, an entirely new area in memory is created by the function you've passed the data to and the information is copied over. This is fine for something like an integer, but what about a data structure that consists of tons of data? You'll really slow your program down if it has to constantly copy large amounts of data.

Before I talk about passing by reference, let's look at the global variable solution to our problem. With global variables we can have all of our data stored in one place and it can be accessed from anywhere. So what's the problem with this approach? Well, the problem with global variables is that we can have all of our data stored in one place and it can be accessed from anywhere (yes, I did just repeat myself). This is great for smaller programs. You don't have to create extra parameters in your functions just so you can access data from different parts of your program, and there's no penalty like with passing by value. The problem comes when you try to write large scale applications. Think of the complexity of most games now. They have phyics engines, graphics engines, sound, input, networking...way too much stuff to be able to keep track of with a mess of global variables. Especially when you have a team of 30 people working on a project. Instead of expecting everyone to keep track of every variable in the program, a different method must be used. So now we come to the pointer solution and the definition of passing by reference. When we pass by reference, we only pass the address of the data (the data is located on the free store) to the function that we want to access the data. The address is of fixed length (whatever the size of a pointer is on your machine), so we aren't slowing our program down by copying large amounts of data. We also don't need to worry about the data not being there because we are the ones who control its deletion. Note that we can still cause ourselves problems by deleting something on the free store and then trying to access it from somewhere else. Let's now talk about reference variables and follow with example of passing by reference and passing by value. Reference variables are not pointer variables, but they are very similar. Both reference variables and pointer variables store the address of data on the free store. They differ in how we use them. To declare a reference variable, you simply add a & after the variable like so: int& ref; Reference variables are good for passing the address of a value to a function. If you're wondering why having the function take a pointer isn't just as good, see the following code. Here's a listing of three functions, one that takes the value of a variable, one that takes a pointer to a variable, and one that takes the address of the variable:

// Just takes the values of the parameters passed to it // and copies them into the variables x and y int AddByValue(int x, int y) { return x + y; } // Takes pointers to the parameters passed to it int AddByReference1(int* x, int* y) { // You have to use the indirection operator because they're pointers return (*x) + (*y); } // Takes the values by reference int AddByReference2(int& x, int& y) { return x + y; // ah, that's nice! } // Here's how you would call each function int x = 4; int y = 7; // This one's pretty straight forward, just call it and // it'll copy the values you pass to it AddByValue(x, y); // We have to send the addresses because the function takes pointers. // Remember, a pointer just stores the address of the data, if we // were allowed to just send in x and y (without the & operator) we'd // be telling the variable x to point to the non-existent address 4 // and the variable y to point to the non-existent address 7 AddByReference1(&x, &y); // Aren't reference parameters great? We just send in the variables // and it's taken care of for us AddByReference2(x, y); As you can see, using reference variables as parameters allows us to use cleaner code than would be possible using pointers as parameters. When we used pointers, we had to pass the function parameters using the address of operator. We also had to use the indirection operator when we added the two values (we had to use the indirection operator to get the actual value stored at the location the pointers pointed to, otherwise

we would have added their addresses. Try it out and see what you get!). When we had the function take reference variables, everything was taken care of for us. So the moral of the story is, when you want to pass by reference, use reference variables as function parameters. Conclusion That concludes our discussion of pointers. Please let me know if you think I could make this any better. And be sure to download the source and play with it if you haven't already. One final note: When people explain pointers they often make it sound like pointers are the only thing you should ever use. They do this because they are trying to convince you of their importance. However, don't think that you should only ever use pointers. Click here to download the source code for this tutorial. Copyright Aaron Cox 2004 2005, All Rights Reserved.