An array is nothing more than a pointer. To understand an array, you must first understand memory and pointers.
All variables in stored somewhere in memory. A pointer stores that memory location, while a basic object, an int variable, etc., will store the data located at that memory location.
An array is a set of contiguous memory blocks of a specified size.When creating an array, the system must set aside x amount of blocks times the size of the object. Each object is defined to be a certain size depending on what the requirements of said object are. A char, for example, takes up one byte, an int takes up 4, etc. (Note: These are typical values but not always guaranteed). So, if we're creating an array of 10 char, we need 10bytes allocated for us. These need to be in order for the array to work properly.
When an array is actually created, it doesn't store all of the information inside of one variable, instead, it just points to the first element. This is helpful in the fact that all of the elements are separated by the size of the object. Without using any C terminology here (which goes further into depth of how everything works), this is where the [] operator comes in handy. When the [] operator is used on a pointer, the pointer is effectively increased by the number inside of the brackets, and dereferenced. This allows us to modify the value stored at that memory location, or display it to the screen, or do whatever we want with it.
I know I've probably lost you by this point, but try to bare with me. I've typed up a short example demonstrating how an array actually works.
1 2 3 4 5 6 7 8 9 10 11 12
|
#include <iostream>
int main() {
int myArray[10];
for (int i = 0; i < 10; i ++)
myArray[i] = i;
for (int i = 0; i < 10; i ++)
std::cout << myArray[i] << " is located at " << &myArray[i] << "\n";
return 0;
}
|
On my system, this is the output:
0 is located at 0x22ff08
1 is located at 0x22ff0c
2 is located at 0x22ff10
3 is located at 0x22ff14
4 is located at 0x22ff18
5 is located at 0x22ff1c
6 is located at 0x22ff20
7 is located at 0x22ff24
8 is located at 0x22ff28
9 is located at 0x22ff2c |
If you notice, each memory location is exactly 4 bytes apart, the exact size of an int.
Now, to where this all plays into what I previously posted. As I said, an array needs to be contiguous. In order to make an array larger, or smaller if so desired, we need to allocate memory. This is where the new comes into play. Your user will define the size of the array. However, in C++, the standard requires that arrays are generated using a const value, so that it knows exactly how much to allocate for the array. This is why we need to "new" memory for our array. new does some fancy stuff behind the scenes, but it's essentially the same thing as declaring a new array, the only exception is that it returns the memory location of the newly allocated memory for us to use.
I rewrote the example I gave you earlier:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
#include <iostream>
int main() {
int numberOfElements = 0;
int *myArray; // Going to hold our array
std::cout << "Please enter the amount of numbers you want to enter (larger than 0): ";
std::cin >> numberOfElements;
// Should do some error checking to make sure the number is positive
// We need to allocate space for our array
myArray = new int[numberOfElements];
// We need to add the numbers into our array
for (int i = 0; i < numberOfElements; i ++) {
std::cout << "Please enter number " << i + 1 << ": ";
std::cin >> myArray[i];
}
// Display values
for (int i = 0; i < numberOfElements; i ++)
std::cout << "\nNumber " << i + 1 << ": " << myArray[i] << " @ " << &myArray[i];
delete[] myArray;
return 0;
}
|
Notice the output on my system:
Please enter the amount of numbers you want to enter (larger than 0): 5
Please enter number 1: 1
Please enter number 2: 2
Please enter number 3: 3
Please enter number 4: 4
Please enter number 5: 5
Number 1: 1 @ 0x3e2550
Number 2: 2 @ 0x3e2554
Number 3: 3 @ 0x3e2558
Number 4: 4 @ 0x3e255c
Number 5: 5 @ 0x3e2560 |
Again, each int is exactly 4 bytes apart. This is necessary so that the values are all in order, with exactly the same width away from each other.
Don't be afraid if this is over your head, it took me years to not be afraid of pointers and things that involve memory, however, it's worthwhile to understand why things need to be the way they are.
So you might ask, what happens if you don't allocate enough memory? Well, this really depends. Sometimes your program might attempt to access memory that's in use by another program. This will cause your program to crash. Sometimes, your program might attempt to access memory which hasn't been allocated to it yet, this can result in a crash, or not, it really depends on a lot of factors.
I'm sort of just rambling on now, so if you have any more questions, please ask.