reinterpret_cast

From: Rich (Someone_at_somewhere.com)
Date: 12/29/04


Date: Wed, 29 Dec 2004 17:44:20 +0000 (UTC)

Hello,

Sorry to be a pain, I had another cast question and have completed the
question correctly I think, have answered correctly, but not entiely sure.

The question is

Create a function that takes a pointer to an array of double and a value
indicating the size of the array.
The function should print each element in the array.
Now create an array of double and initialise each element to 0 then use
your function to print the elements of the array.
Next use reinterpret_cast to cast the starting address of the array to
an unsigned char* and set each byte of the array to 1( hint: you will
need to use sizeof to calculate the number of bytes in a double ). now
use the array printing function to print the results why do you think
each elemement was not set to 1.0?

Ok first off here is the function which I am certain I have correct :)

void PrintArray( double* dArray, int nElements )
{
        for( int i = 0; i < nElements; i++ )
        {
                cout << dArray[ i ] << endl;
        }
} ///:~

Now the main part of the program what I am not certain I have done
correctly :(

int main()
{
        double dArray[ 10 ] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        char pause = ' ';
        double* p_dArray = &dArray[ 0 ];
        PrintArray( p_dArray, 10 );
        unsigned char* UC = reinterpret_cast<unsigned char*> ( &dArray[ 0 ] );
// cast to unsigned char* pointing to start address of array
        for( int i = 0; i < ( 10 * sizeof(double) ) / ( sizeof( unsigned char )
) ; i++ )
        {
                UC[ i ] = '1';
        }
        PrintArray( p_dArray, 10 );
        cin >> pause;
        return 0;

} ///:~
in the for loop I used a calculation to divide number of bytes in a
double by number of bytes in an unsigned char, since the question says
set each byte of the array element to value 1 then there will be however
many bytes in a double more to set than if using a double, on my
computer an unsigned char is 1 byte a double is 8 bytes, so if there are
10 double elements in the array it will need to set 80 bytes to the
value of 1.

As for why it did not print out 1.0 for each element, this is becuase
there is a mantissa and exponent part to a double and since these are
being set to '1' then this has the effect on the entire value of the
double will be affected.

Sorry for the questions, but we haven't covered this yet and was an
excercise to find out about all the different casts including const_cast
and answer the questions.

Hopefully I have this question right, but if someone could confirm this
it would be useful

TIA



Relevant Pages

  • Re: Copying an array slice (Was: Re: Difficulties with passing multi-dimensional arrays)
    ... > the pointer to array of unknown size. ... but it still compiles without a cast. ... unknown at compile time so nothing can be checked. ...
    (comp.lang.c)
  • Re: To cast or not to cast?
    ... I would cast it to byte array just in case. ... And make sure array size is OK before it's added to the SQL. ... > I'm certain that the biometricdata is not correctly inserted ... I know that the marshal copying from pointer to bytewent ok ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: What does it mean?
    ... There certainly are reasons to cast versus ... > an exception or create the appropriate object. ... The code example given is most definitley a runtime type check. ... control array is a particular control. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: reinterpret_cast
    ... > Sorry to be a pain, I had another cast question and have completed the ... > value indicating the size of the array. ... The division by sizeof (unsigned char) is not needed. ... because the character ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Casting struct[] to object[]
    ... conversion from struct to object isn't a cast. ... System.Array or System.Object, you want to cast every item in the array. ... > private int TestA; ...
    (microsoft.public.dotnet.languages.csharp)