Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(C++) 3.How can you use pointer to print the first element of an array? Give an

ID: 3821277 • Letter: #

Question

(C++)

3.How can you use pointer to print the first element of an array? Give an example.

4.Explain the “Program Output” listed on page 507, Program9-7 of the text book. How does the pointer notation work with array name?

PAGE 507 PROGRAM 9-7:

Program Output

Note on Question 3 and 4 above: It is very critical that you thoroughly understand the use of pointers to access elements of an array. This is a very common use case.

5.When you increment/decrement a pointer, are you changing the address or the element at the address? If it is the address, how does the program now how much to increment or decrement? Hint: Remember the use of “sizeof” function in earlier chapters.

6.Explain the difference between the following; assuming ptr1 and ptr2 are pointers to integers.

If (ptr1 < ptr2)

If (*ptr1 < *ptr2)

7.

a.Why do we need dynamic memory allocation?

b.What commands are used to allocate and release memory?

c. What is memory leak, and how do you prevent it?

Explanation / Answer

3) In the following way we can use a pointer to print the first element of the array:

int array[5] = {1,2,3,4,5};

int *ptr;

ptr = array

cout<< *(ptr+0);

i.e. by making the pointer point to the array, which points tho the location of the first element of the array. We can just dereference the pointer to find the value of the first element of the array.

4) When we assign doublePtr = coins, we point the pointer doublePtr to the location where the first element of the array coints is stored.

When you declare an array as:

double coins[NUM_COINS];

you are in fact declaring a pointer coins to the first element in the array. That is, coins is exactly the same as &coins[0]. The only difference between coins and a pointer variable is that the array name is a constant pointer - you cannot change the location it points at. When you write an expression such as coins[i] this is converted into a pointer expression that gives the value of the appropriate element. To be more precise, coins[i] is exactly equivalent to *(coins+i) i.e. the value pointed at by coins + i . In the same way *(coins+ 1) is the same as coins[1] and same applies to doublePtr i.e we can use it as doublePtr[i] but it will be converted to pointer notation while accessing that memory location.

5) When we increment/decrement a pointer we essentially change the address location to which the pointer points to.

Incrementing the pointer results in incrementing the memory address by the number of bytes occupied by the base type. For example, variables x and p are pointers of double type.

      double *x;

      double *p;

      p=x++;

Pointer x is incremented by 1. Now if the memory address of x was 2000 and after incrementing p will contain memory address 2008 as double takes 8 bytes. I.e. in the memory, the pointer is incremented by the count of increment in the program multiplied by the sizeof(type_of_pointer).

6) ptr1 < ptr2 compares the memory addresses pointed at by the two pointers whereas *ptr1 < *ptr2 compares the values stored at the memory locations pointed at by the two pointers.

7)

a) We need dynamic memory allocations for the following reasons:

i) When a lot of memory is required. Stack can only hold so much memory, so if we allocate a lot of memory on stack, we are risking crash.

ii) When the memory assigned must live after the function returns. Stack memory is destroyed after a function is returned but dynamic memory is allocated on heap and is freed when we free it.

iii) When we are building a structure (like array, or graph) of size that is unknown (i.e. may get big), dynamically changes or is too hard to precalculate. Dynamic allocation allows your code to naturally request memory piece by piece at any moment and only when you need it. It is not possible to repeatedly request more and more stack space in a for loop.

b) new,malloc,calloc,realloc are used to alloctae dynamic memory in c++. new is addition in c++ and the other three are continued from c in c++. delete command is used to release memory allocated using new and free is used to release memory allocated by the other three.

c) Memory leaks occur when we allocate a dynamic memory i.e. in heap and forget to release it. To avoid memory leaks, memory allocated on heap should always be freed when no longer needed.