I
Immortal Nephi
The member function has one or two parameters. First parameter is
reference to array and second parameter may not be needed or assign
array size.
For example to use reference
void Foo::read( const char arrayName[], int arraySize )
{
int i = 0;
do
{
cout << arrayName[ i++ ];
}
while ( i != arraySize );
cout << endl;
}
Sometimes, programmer prefers to use pointer instead because they
want to test null terminator.
For example to use pointer
void Foo::read( const char* arrayName )
{
do
{
cout << *arrayName;
arrayName++;
}
while ( *arrayName != ‘\0’ );
cout << endl;
}
I prefer to test arraySize rather than null terminator because I
don’t trust null terminator. Sometimes, null terminator may be
inserted in the middle of the arrayName. The loop allows to ignore
reading null terminator and checks for arraySize before loop is
terminated.
If you have 16 elements in an arrayName, it is your responsibility to
provide arraySize or null terminator. The operating system will
trigger violation security denied if more elements are read or walked
out outside the arrayName range.
The arrayName is often located in the file scope like global variable
and also it is often located in the function body like local
variable. After you instantiate a class, you will assign local
variable – arrayName and arraySize to the member function read().
Then, member function read() does its own algorithm to manipulate
arrayName in memory directly with the help of arraySize information.
Do you have the solution if you provide wrong array size information?
reference to array and second parameter may not be needed or assign
array size.
For example to use reference
void Foo::read( const char arrayName[], int arraySize )
{
int i = 0;
do
{
cout << arrayName[ i++ ];
}
while ( i != arraySize );
cout << endl;
}
Sometimes, programmer prefers to use pointer instead because they
want to test null terminator.
For example to use pointer
void Foo::read( const char* arrayName )
{
do
{
cout << *arrayName;
arrayName++;
}
while ( *arrayName != ‘\0’ );
cout << endl;
}
I prefer to test arraySize rather than null terminator because I
don’t trust null terminator. Sometimes, null terminator may be
inserted in the middle of the arrayName. The loop allows to ignore
reading null terminator and checks for arraySize before loop is
terminated.
If you have 16 elements in an arrayName, it is your responsibility to
provide arraySize or null terminator. The operating system will
trigger violation security denied if more elements are read or walked
out outside the arrayName range.
The arrayName is often located in the file scope like global variable
and also it is often located in the function body like local
variable. After you instantiate a class, you will assign local
variable – arrayName and arraySize to the member function read().
Then, member function read() does its own algorithm to manipulate
arrayName in memory directly with the help of arraySize information.
Do you have the solution if you provide wrong array size information?