0: #include <iostream> 
 1: #include <string> 
 2: using namespace std; 
 3: 
 4: int main() { 
 5:    int myIntArray[4] = {1, 2, 3, 4}; 
 6:    double myDoubleArray[4] = {100, 101, 102, 103}; 
 7: 
 8:    int i; 
 9:    int *ip, *ibp; 
10:    double *dp, *dbp; 
11: 
12:    ip = &myIntArray[0]; 
13:    ibp = myIntArray; 
14:    dp = &myDoubleArray[0]; 
15:    dbp = myDoubleArray; 
16: 
17:    for (i=0; i < 16; i++) { 
18:       dp++; ip++; 
19:    } 
20: 
21:    cout << " dbp: " << dbp << endl; 
22:    cout << " dp: " << dp << endl << endl; 
23:    cout << " ibp: " << ibp << endl; 
24:    cout << " ip: " << ip << endl << endl; 
25: 
26:    cout << " (int *) difference: " << (ip - ibp) << endl; 
27:    cout << "(double *) difference: " << (dp - dbp) << endl; 
28: }

(b) [5 pts] Assume for parts (b) and (c) that myIntArray gets placed at memory address 1000, and myDoubleArray gets placed at memory address 2000. What is the value of each variable in the code (i, myDoubleArray, myIntArray, dp, dbp, ip, ibp) when the program reaches Line 16?

how you figure it out?


Also.. this one

25: float *getFloatArray() { 
26:    float *floatArray; 
27:    float floatArray2[ARRAY_SIZE]; 
28:    int i; 
29: 
30:    floatArray = new float[ARRAY_SIZE]; 
31: 
32:    floatArray = floatArray2; 
33:    for (i=0; i < ARRAY_SIZE; i++) 
34:       floatArray[i] = i * 2.0; 
35:    return(floatArray); 
36: }

what is wrong with this part of the code? also which part made the program memory leak?
and why??


thanks,

A pointer stores a memory address. The variable that represents an array is a pointer to element [0] of the array.

In this case, the reference operator (operator&) returns the memory address of the variable it is used on.

The index operator used with arrays (operator[]) is a type of de-referencing operator, like (operator*) or (operator->)...

Apply those 3 general rules to your code and you should be able to determine the answers.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.