CCH asked in 電腦與網際網路程式設計 · 1 decade ago

程式語言的問題,英文內容.

1.what significant justification is there for the -> operator in c and c++?

2.multidimensional array can be stored in row major order, as in c++,or in column major order,as in Fortran.develop the access functions for both of these arrangements for three-dimensional arrays.

3.write a short discussion of what was lost and what was gained in java's designer' decision to not include the pointers of c++.

4.write a c program that does a large number of references to elements of two-dimensioned array,using only subscripting.write a second program that does the same operations but uses pointers and pointer and pointer arithmetic for the storage mapping function to do the array references.compare the time efficiency of the two programs.which of the two programs is likely to be more reliable? why?

Update:

sorry 可以用中文回答嗎?

Update 2:

Q5:

Update 3:

Q5:Rewrite assignment1 with Java. Use Java's built-in class "vector" to implement

a heap-dynamic array in your program.

1 Answer

Rating
  • Inunu
    Lv 5
    1 decade ago
    Best Answer

    1. In C it simply means the value at where the pointer points to. In C++ there is a possibility to override and hence may exhibit a completely different behaviour.

    For instance in Visual C/C++ 2005 objects created with gcnew have '^' pointer which actually points to a meta structure containing pointer to actual object and extra information that can be used in profiling and clean up. It overrides '->' operator so you get the actual object instead of the meta structure.

    2.

    Suppose we have an array and a pointer:

    arr[p][q][r];

    *ptr = &arr[0][0][0];

    Row first: arr[x][y][z] = ptr[(((x * q) + y) * r) + z]

    Column first: arr[x][y][z] = ptr[(((z * q) + y) * r) + x]

    (I.e. changing {x,y,z} to {z,y,x}.)

    3.

    Lost:

    * Requires a common base class acting as void* for arbitrary object

    * A new array needs to be created in order to generate a subset of array (i.e. substring)

    Gain:

    * Type information is preserved and can validate runtime type cast.

    * Arrays have size, which enables runtime index out-of-bound check.

    * Avoids the "ownership" problem with pointers (i.e. who does the free up / clean up?)

    4.

    int TableA[1024][1024];

    void initA()

    {

    memset(&TableA[0], 0, sizeof(int) * 1024 * 1024);

    }

    int TableBData[1024 * 1024];

    int *TableB[1024];

    void initB()

    {

    int i;

    memset(&TableBData[0], 0, sizeof(int) * 1024 * 1024);

    for(i = 0; i < 1024; i++)

    {

    TableB[i] = &TableBData[i * 1024];

    }

    }

    Using TableA is more likely to be more reliably. Using TableB you must ensure this array of pointers are set with correct addresses, which is a potential source of error.

Still have questions? Get your answers by asking now.