Gw Temp

Menu

Tutorial - 'Advanced C++ Concepts Part 2 - Static and Dynamic' by AzureFenrir

An item about Programming Languages posted on

Blurb

The recovered second part of AzureFenrir's series, this time covering old-school static and dynamic pointer arrays in C++.

Body

Advanced C++ Concepts Part 2 - Static and Dynamic Arrays in C++


AzureFenrir's back! I demand that you will read this tutorial, and you will like it, whether you like it or not. If not enough people read this by day X, then I shall hold the GW server hostage and reduce MDN's post count by 0.01 until my demands are met. MWAHAHAHAHA!

Just kidding.

Anyway, this tutorial is about static and dynamic arrays, which are basically variables that contain lists of information. For example, if we define the variable "newvariable" as an array of 4 integers, then the array can store four different integers at once.

You will need a working knowledge of C++ to understand this tutorial. In addition, to understand dynamic arrays, you need a good knowledge of pointers and the C++ (*) operator.

So...why use an array when you could just declare four variables? Well, first of all, arrays are easier to use, especially if you have, say, a table of four thousand random numbers. Are you really going to declare 400 variables, then initialize them? I highly doubt it.

Array elements can also be referred to by a number or a variable, which makes them effective to use in loops and stuff. For example:

int arraynew[100];    //This code creates a new array of 100 integes. Don't worry if you do not understand this, I'll explain it in this tutorial.

for(int i = 0; i < 100; ++i)
{
    arraynew[i] = ((i * 5) + 7) / 10
}


Array Element - a single variable in an array. Remember that an array is a list of many variables.

Whoo, you've populated 100 variables with a few lines of code. If you were to do this with 100 variables...well...good luck with the typing.

Finally, arrays can also be dynamic, which means that the number of elements that they store can be changed by code. This is very useful, as you can make a program that can read and store varying amount of data (provided that the host system has enough memory).

But let's look at static arrays first:


Static Arrays:

Static Arrays are arrays who alwas store the same number of variables. They are declared like this:

Type variable_name[# of "stuff"]

For example:

int Array1[254];    //Creates an array of 254 integers.
float Array2[100];    //Creates an array of 100 floating-point numbers.
char Array3[103];    //Creates string of 103 characters (remember tutorial #2?).
some_type Array4[254];    //Creates an array of 254 some_type variables.

When you want to use a certain element of the array:

Array1[element #] = value

For Example:

Array1[35] = 1337];    //Stores the value 1337 into the 35th element of Array1.

Remember that array elements are numbered starting with zero. Thus, the first element is array[0], then array[1], ..., all the way through element #(number of "stuff" - 1). So if you have this array:

int array1[100];

The elements will be numbered from 0 to 99.

These arrays do not allow for much flexibility, but...


Dynamic Arrays:

Dynamic Arrays are so much better than static arrays in that they can store any number of variables. You could freely change the number of variables contained in it at any point in your program.

A dynamic array is declared like this:

type *variable

That's right - it IS a pointer. The only way for us to make dynamic arrays is to use a pointer...so we could use the new keyword to allocate however much memory we need. So...basically, we're creating new arrays and making the variable point to this new array.

Let's examine a sample program line-by-line to examine this concept:

#include <iostream.h>

int main()
{
    int *array1;

    array1 = new int[100];
    array1[45] = 13;

    array1 = new int[50];
    array1[35] = 1337;
    cout << array1[45] << endl;

    return 0;
}


This program will create a new dynamic array named array1:
    int *array1;

It will then assign it to an array of 100 elements:
    array1 = new int[100];

And store the number 13 into the 45th element:
    array1[45] = 13;

Then, it'll assign the array to an aray of 50 elements (effectively resizing it):
    array1 = new int[50];

And assigning 1337 to element #35:
    array1[35] = 1337;

Finally, it will display the number stored in element #45:
    cout << array1[45] << endl;

And end the program.

If you run the program, it will NOT display 13. Instead, you'll get either a weird number or zero! Why is that?

Well...when you use the new keyword, you assigned the pointer to another array. The element #45 of the OLD ARRAY contains 13, but the new one does not. Therefore, C++ will display whatever is stored in the UNINITIALIZED element #45 (which is usually -2^31 + 1).

So how do we get the elements of the old array into the new array? Array copying, of course!

#include <iostream.h>

int main()
{
    int *array1, *arraytemp;

    array1 = new int[100];
    array1[45] = 13;

    arraytemp = new int[50];

    // BEGIN Array Copying Code -->
    for(int i = 0; i < 50; ++i)
    {
        arraytemp[i] = array1[i];
    }
    delete array1;
    array1 = arraytemp;
    // <-- END Array Copying Code

    array1[35] = 1337;
    cout << array1[45] << endl;

    return 0;
}


As you can see, we simply used a loop to copy all the elements from arraytemp to array1. After the loop, we delete array1 to save memory, and set array1 to our new resized array, "arraytemp." The program will now properly display "13."

Phew. That ends the second set of my C++ concepts tutorial. I think OOP is a good next topic :).

*AzureFenrir falls asleep on his chair and is struck by thunder*