In the dynamic memory page in the tutorial on this site, some example code is given to show the use of the operators new, new[], delete, delete[], and the nothrow method of handling memory allocation failure.
The example code creates an array of five elements, and lets the user fill those elements with integers, then it prints the five elements that were entered by the user. I added in the line "cin.get();" before the "return 0;" so I could mess with the program and see my results.
// rememb-o-matic
#include <iostream>
#include <new>
usingnamespace std;
int main ()
{
int i,n;
int * p;
cout << "How many numbers would you like to type? ";
cin >> i;
p= new (nothrow) int[i];
if (p == 0)
cout << "Error: memory could not be allocated";
else
{
for (n=0; n<i; n++)
{
cout << "Enter number: ";
cin >> p[n];
}
cout << "You have entered: ";
for (n=0; n<i; n++)
cout << p[n] << ", ";
cin.get();
delete[] p;
}
return 0;
}
I'm having trouble understanding what the two parameters for the for loops (in bold) are specifying.. Are those three parameters specifying when n=0, or when n<i, or when n++ to fire the code within the for loop, when one of those three parameters is filled? What exactly does the parameter n++ mean? Also, how is p[n] being shifted +1 element every time the user inputs a new integer, so that the stored data doesn't just replace the same element over and over?
Loops and Arrays! These are fundamental concepts that you probably should know before looking into more intermediate concepts like dynamic memory.
i was filled by the user by the above cin line. It's poorly named... it probably should be named "count" or "size" or something similar... but programmers are lazy. Essentially, i = the number of ints the user wants to enter.
This 'for' business is the loop. for loops have 3 sections in the parenthesis, each seperated with a semicolon:
1) The initialization (n=0). This is performed once when the loop first starts
2) The condition (n<i). This is performed every time before the loop body is executed. If the condition is true, the loop body executes, otherwise the loop is skipped.
3) The iteration (n++). This is performed every time the loop completes, before the condition is checked again.
So basically what happens is the loop starts by setting n to zero. Then each time it loops, n increases by 1, so 'n' basically counts the number of times the loop has executed.
It keeps looping until n >= i. And since n is the counter... that means if the user input '6' for i, then that loop would run 6 times.
As for p[n], this is using 'n' as an index for array 'p'. 'p' is multiple integers, not just one. The number of integers is determined by i (user input). So if the user input 3, then p has 3 integers:
p[0]
p[1]
p[2]
Note that even though these are all p, they are 3 seperate integers and can store their own value.
You can use an integer variable (in this case 'n') to index p. So when you do p[n], if n==2 that'd be the same as doing p[2].
Since this is inside a loop, n changes every time the loop runs. And therefore a different integer is written to every time the loop occurs.
1) The initialization (n=0). This is performed once when the loop first starts
2) The condition (n<i). This is performed every time before the loop body is executed. If the condition is true, the loop body executes, otherwise the loop is skipped.
3) The iteration (n++). This is performed every time the loop completes, before the condition is checked again.