A new operator can help us here. The sizeof operator produces an integer value of type size_t
that gives the number of bytes occupied by its operand, where size_t is a type defi ned by the
standard library. Many standard library functions return a value of type size_t , and the size_t
type is defi ned within the standard library using a typedef statement to be equivalent to one of the
fundamental types, usually unsigned int . The reason for using size_t rather than a fundamental
type directly is that it allows fl exibility in what the actual type is in different C++ implementations.
The C++ standard permits the range of values accommodated by a fundamental type to vary, to
make the best of a given hardware architecture, and size_t can be defi ned to be the equivalent of
the most suitable fundamental type in the current machine environment.
Look at this statement that refers to the variable dice from the previous example:
cout < < sizeof dice;
The value of the expression sizeof dice is 4 because dice was declared as type int and therefore
occupies 4 bytes. Thus this statement outputs the value 4 .
The sizeof operator can be applied to an element in an array or to the whole array. When the
operator is applied to an array name by itself, it produces the number of bytes occupied by the whole
array, whereas when it is applied to a single element with the appropriate index value or values, it
results in the number of bytes occupied by that element. Thus, in the last example, you could output
the number of elements in the pstr array with the expression:
cout < < (sizeof pstr)/(sizeof pstr[0]);
The expression (sizeof pstr)/(sizeof pstr[0]) divides the number of bytes occupied by the
whole pointer array, by the number of bytes occupied by the fi rst element of the array. Because each
element in the array occupies the same amount of memory, the result is the number of elements in
the array.
That is quoted from Ivor Horton's Beginning Visual C++ 2010 published by Wrox. That is where I got the idea.
I agree with you on the use of short and int and int is clearly the better choice but I can see that there, in some really strange (and quite possibly very inefficient coding in other ways) be a use for short, but I think most of the time it is trivial.
Now back to the problem at hand. It still will not let me write to the various blocks of memory that I have dynamically assigned. To read the values in I shouldn't need to pass the size because the size will increase after every 5th number is input to accomadate for five more numbers. I can't even get it to accept the first five numbers. In the output results function if I change the loop (the function is below) to iterate manually 4 times (for the 4 values and disregard the fifth negative number) I get -17891602 4 times in a row. Obviously this is just garbage left over in memory that is being displayed so it isn't writing the values I've supplied to it to the console (in this example I supplied 1, 2, 3, 4, -5).
The outputResults function:
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 27 28
|
void outputResults(const int* pDataArray)
{
int arraySize = sizeof(pDataArray)/sizeof(int); //To know how many iterations there
//are going to be we must determine
//the size of the array.
short iterationCounter = 1; //This is initially set to one so that
//it can easily be passed to the
//function calculateAverage() as the
//second parameter of how many nums.
//there are.
while (iterationCounter <= (/*arraySize - 1*/4)) //Since the array will store the last
{ //value (or neg. value) we will want
//to exclude this last value.
int mod5 = iterationCounter % 5;
if (mod5 == 0) cout << endl;
int value = *(pDataArray + (iterationCounter - 1));//The minus 1 is used here
//because indexes start at zero, not
//at one which is the value that
//iteration counter is initially set
//to.
cout << value << ' ';
iterationCounter++; //Since we have added a value, we will
} //increment iteration counter in order
//to access the next value on the next
//iteration of the loop.
system ("PAUSE");
}
|
I don't understand why it isn't writing the values to memory like it should and why doesn't it seem to be allocating the memory for the array?