Most expensive game to develop: GTA IV - $100,000,000

Sep 29, 2009

Arrays and Strings - Part 1/2

Some people, specially the beginners in programming, find this subject quite complex due the amount of details it has. But understand the principles and rules for using them is the basic if you want to be a programming expert (XD). Things get very difficult in C++ when you have to work with arrays and pointers together. The possibilities for errors increase and knowing how they work may help you to fix possible issues. Here I'll talk about Arrays and Strings and later I'll enter into the Pointers subject.

Array

You can use arrays to store n elements of the the same type under one name:

type name_of_your_choice [number_of_elements]

Quite simple declaration. But let's learn how they work here:

type -> you can choose the type for entire elements in the array. If you pick integer (int), all the elements will be integers, if you choose float, you will have an array of n float elements. But you can use other type than the
fundamental's one, like if you create this struct:

struct sExample{
     int x;
     float y;
     bool choice;
}

and create the array sExample[10] means that each entry of the array will store an int, a float and a bool value. Other words you will have 10 integers, 10 floatings numbers and 10 booleans variables, all of them in a 10-lenght array. Also, the type you choose will determine the amount of memory that will be used to store the values. Integers, in C++/Windows x86 enviroment, uses 4 bytes or 32 bits to store a value. So Array[20] is gonna use 4x20=80 bytes in memory AND this space is reserved once you declare the array - if you don't use all the 20 entrys, you're wasting memory. The math of sExample is 10x4(int) + 10x4(float) + 10x1(bool) = 90 bytes of memory used to store the array.

name_of_your_choice -> the name of your array. You can use any name, but the reserved words of C++. Also, respect the naming rules, like don't start the name with numbers.

number_of_elements (or index) -> always come between two brackets ([ ]). It defines how many elements you want in your array. Only accept integer values like 1, 10, 100 ,500. Don't even bother to use 1.35 or 'c', it won't work :). You guys can test how many integer elements you can use in an array before the compile  give an exception message. My 2GB PC allowed me to use 320M integers - int MyArray[320000000]. Pretty huge number, that I guess I'll never use, again :) A crucial detail: The first element of an array is stored in [0], and not [1]. So the last index of a n-elements array is [n-1]!!!

You also can use multidimensional arrays. The declaration consists in add another pair of brackets after the fisrt one like this:

char myArray[10][50];

You can add as many pair of bracket as you want, but be aware that the size of the array will increase. Take a look at the math:

int hehe[10] = 10 integers  -----> 40 bytes
int hehe2[10][10] = 100 integers -----> 400 bytes
int hehe3[10][10][10] = 1,000 integers -----> 4,000 bytes

"It seems to not a problem at all, what 4,000 bytes can do anyway..."
Well, in big projects and program you have to pay attention what types and the amount of items are being used. To be considered a well-developed program - or game, one of the factor is it has to use less memory as possible, saving precious resources at realtime running. Well let's go to the part of declaring and using an array.

Note: I'm pretty sure I forgot something that I would like to mention XD

To declare an array is simple. See the examples:

int myValues[10];
char myName[25];
float myPI[50];
bool myChoice[5];
int LOL[10][15], LOL2[20];

-> You have to declare the size of the array with integer numbers or using a constant previously declared in #define or with const qualifier :

#define size 50
(...)
const int array_size = 10;
float myArray[array_size];
int noneArray[size];

To initialize the array couldn't be more simple. You have two ways:

1. int myArray[3];      // this is the "hard way"
   myArray[0] = 1;
   myArray[1] = 2;
   myArray[2] = 3;

2. int yourArray[3] = {1,2,3}     // this is the "easy way" - use of braces to declare the values!
   or
   int yourArray[] = {1,2,3}   // here, you don't have to declare the size of the array, once you gonna define how many and what elements there will be

These ways also work for multidimensiaonal arrays. Let's use a 3x3:

int myArray[3][3];
myArray[0][0] = 1;
myArray[2][3] = 23;
myArray[1][0] = 1256;

or

int myArray[3][3] = { 1,2,3,4,5,6,7,8,9 }// the assignment is done linearly. This means the first entry is [0][0], the second is [0][1], the third is [0][2] and goes on, [1][0], [1][1], [1][2], [2][0], etc...

for purpose of beaty, I like to do this:

int myArray[3][3] = { {1,2,3},
                                  {4,5,6},
                                  {7,8,9}
                                };


Sure, you can initialize long arrays with loops but the elements have to be in some sort of linear or geometrical progression or be result of an equation.

Now this is not allowed:

int arrayful[5];
arrayful[5] = {1,2,3,4,5}; // initialize the array after declaration using this method: can't do it!

float A[5]; float B[5];
A=B;     //  perform such operation between arrays: can't do it!


Initialization Hints:
::::: You also can initialize part of the array:

int ourArray[5] = {1,2,3};

Here we initialized the first 3 elements of the array ([0][1][2]). The rest of array is set to 0 value automatically - ourArray[3] = 0
and ourArray[4] = 0;

::::: To set entire array with 0 (zero) value just do this:

int Test[500] = {0};

But putting another value different than 0 (zero), you will initialize just the first element with this value! As we saw before, the other 449 elements will be assigned with the zero value.


But how the arrays are stored in the memory buffer? Here's a didatic vision:




And this is part 1, guys.
Soon I'll post the second part, about strings and the ways to use it in C++. Cya  o/

No comments:

Post a Comment