Monday, April 16, 2012

Chapter 8

8.3 8.5 8.7 8.15 8.23 8.31 8.47

8.3 Boolean data type in Java and C++
a) In Java the boolean type is actually a child of the "primitive" type and is not a numeric or ordinal type. The "Primitive" types include boolean and Numeric types, as the two sub categories of primitive types.

In C/C++ the boolean type is most likely counted as an enum type, which falls under Basic > Numeric > Integral, so it varies from Java's implementation of the boolean type.

b) I would say that it would be useful for a boolean to be ordered, starting with false for security. The reason is that C/C++ counts any number > 0 to be true, so an ordering of starting from 0 would be useful. Furthermore, converting boolean to integers is useful like in the previous statement.

8.5 Copying Array with =



int x[10];
int y[10];

x = y;

First of all, there is no overloaded copy function declared for Arrays so the above example would not compile in C, although copying of Arrays can be done by overloading the = operator for Arrays. 


There could be another way to copy them by changing the declaration of the above code to the following:
    
       int x[10];
    int y[10];

    *x = *y;


Although, this is not a change to the declaration but to the assignment, so the answer is no to the question: "Can the declarations be fixed so the assignment will work?"


8.7 C/C++ (*x) char to int conversion warning vs error



int (*f)(int);
    int (*g)(int);
    int (*h)(char);

    f = g;
    h = g;

The reason that C gives a warning is that it allows all implicit conversions and trusts the programmer to know what to do. In C++ we get the following warning:  invalid conversion from ‘int (*)(int)’ to ‘int (*)(char)’. C++ threw this error because it only allows implicit conversions that are guaranteed to not corrupt the data.

8.15 Avoiding static_casting in C++


union {
        int i;
        bool b;
    } x;

    int main(){

        x.i = 20000;
        cout << x.i << endl;
        cout << x.b << endl;
        x.b = false;
        cout << x.i << endl;
        cout << x.b << endl;

        return 0;
    }
This code produces the following output for x.b = false: 20000 32 19968 0 x.b = true 20000 32 19969 1 This shows that the data was corrupted after b was set. The reason is that union creates a shared memory space that is used by multiple variables/types. In this case an int and bool data types share the same space. This is dangerous because if there is a difference in the size of the two, then one will manipulate the other. In the second line we get 32 for the bool value, which happens because the memory space is used from 20000 and converted to a byte (8 bits) which has a max of 256. So if x.i was equal to 257, the x.b = 1. There is no stability in using union, and this makes it a bad coding practice.

8.23 == and =

The equality operator (==) can be used on arrays, but it compares the memory addresses, but the assignment operator (=) cannot be applied to arrays because there is no way to assign a new memory location, unless you are using a reference operator, which doesn't work for arrays. 

Arrays are inherently referenced so you are dealing with the memory address and not the values.


8.31 






























template <typename T>



T max(T x[], int size){

  T max = x[0];

  for(int=i;i<size;i++){

    max = (x[i]>max)?x[i]:max;

  return max;

}



template <typename T>



T max(T x[], int size, bool (*gt)(T,T)){

  T max = x[0];

  for(int i=1;i<size;i++){

    max = (*gt)(x[i],max)?x[i]:max;

  }

  return max;



}



No comments:

Post a Comment