Game Development Reference
In-Depth Information
string playerName;
cin >> playerName;
cout << endl << "Hello " << playerName << endl;
return 0;
}
Now the players can enter a name as long or as short as they like without issue. We will cover why
the string class is safer than a character array in Chapter 13.
Note We could use character arrays for player input until we cover strings. However, I do not like to
encourage the development of bad habits. If a player was to enter too many characters for our array to hold
we would suffer a buffer overrun and introduce bugs into our code that will cause the program to crash. STL
string does not suffer from this problem for reasons that are covered in Chapter 13.
Summary
This chapter has covered C++ arrays that allow us to store groups of data of the same type in
contiguous memory. Contiguous means that the elements of the array follow each other directly in
memory without having to jump to another location.
This can be seen in action when we index arrays or carry out pointer arithmetic. Modern processors
are designed with caches to operate at peak efficiency when dealing with contiguous memory
addresses and arrays, therefore, are vitally important to the performance of modern video games.
We cover more exotic data layouts later in this topic when we introduce the STL, but it is important
to understand arrays to be able to write fast and efficient code under many circumstances.
C style strings are also an important concept to understand. Many current games are still written using
this style of string, although the STL provides a better string class implementation. This is usually
because it can still be faster to operate on arrays directly rather than work with classes. However,
accessing arrays directly comes with some concerns about program stability, especially when dealing
with user input. Overrunning the end of a buffer and overwriting memory used by other variables is a
common cause of hard-to-find bugs and potential security vulnerabilities in many games.
This chapter also introduced the concept of a function. Chapter 5 covers functions in more detail
and you will learn how to write some of your own.
 
Search WWH ::




Custom Search