Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
C++ Game Development Cookbook

You're reading from   C++ Game Development Cookbook

Arrow left icon
Product type Paperback
Published in May 2016
Publisher Packt
ISBN-13 9781785882722
Length 346 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Druhin Mukherjee Druhin Mukherjee
Author Profile Icon Druhin Mukherjee
Druhin Mukherjee
Arrow right icon
View More author details
Toc

Table of Contents (15) Chapters Close

Preface 1. Game Development Basics FREE CHAPTER 2. Object-Oriented Approach and Design in Games 3. Data Structures in Game Development 4. Algorithms for Game Development 5. Event-Driven Programming – Making Your First 2D Game 6. Design Patterns for Game Development 7. Organizing and Backing Up 8. AI in Game Development 9. Physics in Game Development 10. Multithreading in Game Development 11. Networking in Game Development 12. Audio in Game Development 13. Tips and Tricks Index

Using pointers to store memory addresses

In the previous two recipes, we have seen how not having sufficient memory can be a problem to us. However, until now, we have had no control over how much memory is assigned and what is assigned to each memory address. Using pointers, we can address this issue. In my opinion, pointers are the single most important topic in C++. If your concept of C++ has to be clear, and if you are to become a good developer in C++, you must be good with pointers. Pointers can seem very daunting at first, but once you get the hang of it, pointers are pretty easy to use.

Getting ready

For this recipe, you will need a Windows machine with a working copy of Visual Studio.

How to do it…

In this recipe, we will see how easy it is to work with pointers. Once you are comfortable using pointers, we can manipulate memory and store references in memory quite easily:

  1. Open Visual Studio.
  2. Create a new C++ project.
  3. Select Win32 Console Application.
  4. Add a source file called main.cpp or anything that you want to name the source file.
  5. Add the following lines of code:
    #include <iostream>
    #include <conio.h>
    
    using namespace std;
    
    int main()
    {
        float fCurrentHealth = 10.0f;
    
        cout << "Address where the float value is stored: " << &fCurrentHealth << endl;
        cout << "Value at that address: " << *(&fCurrentHealth) << endl;
    
        float* pfLocalCurrentHealth = &fCurrentHealth;
        cout << "Value at Local pointer variable: "<<pfLocalCurrentHealth << endl;
        cout << "Address of the Local pointer variable: "<<&pfLocalCurrentHealth << endl;
        cout << "Value at the address of the Local pointer variable: "<<*pfLocalCurrentHealth << endl;
    
        _getch();
        return 0;
    }

How it works…

One of the most powerful tools of a C++ programmer is to manipulate computer memory directly. A pointer is a variable that holds a memory address. Each variable and object used in a C++ program is stored in a specific place in memory. Each memory location has a unique address. Memory addresses will vary depending on the operating system used. The amount of bytes taken up depends on the variable type: float = 4 bytes, short = 2 bytes:

How it works…

Pointers and memory storage

Each location in the memory is 1 byte. The pointer pfLocalCurrentHealth holds the address of the memory location that has stored fCurrentHealth. Hence, when we display the contents of the pointer, we get the same address as that of the address containing the fCurrentHealth variable. We use the & operator to get the address of the pfLocalCurrentHealth variable. When we reference the pointer using the * operator, we get the value stored at the address. Since the stored address is same as the address storing fCurrentHealth, we get the value 10.

There's more…

Let us consider the following declarations:

  • const float* pfNumber1
  • float* const pfNumber2
  • const float* const pfNumber3

All of these declarations are valid. But what do they mean? The first declaration states that pfNumber1 is a pointer to a constant float. The second declaration states that pfNumber2 is a constant pointer to a float. The third declaration states that pfNumber3 is a constant pointer to a constant integer. The key differences between references and these three types of const pointers are listed here:

  • const pointers can be NULL
  • A reference does not have its own address, whereas a pointer does

    The address of a reference is the actual object's address

  • A pointer has its own address and it holds as its value the address of the value it points to
You have been reading a chapter from
C++ Game Development Cookbook
Published in: May 2016
Publisher: Packt
ISBN-13: 9781785882722
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime