Lab 4: Our own Dynamic Array

Overview

This lab is a bit different from previous labs. This time you need to use the starter code supplied in Replit and you need to implement a program that dynamically grows an array as needed, using malloc. We will deliberately choose not to use realloc(). This exercise will stretch our understanding of implementing reference parameters in C, and in this case some of the reference parameters are themselves pointers!


In your lab-partner slides you will provide:

  1. A link to your Replit program, where you use the Replit starter code and implement the add(...) function. Do not post your solution, as we want each lab partner group to have the experience of working on their own.

  2. A description of how far you got, and what remains to be done.

  3. A description of specific errors you made along the way and what you learned from them. Post code segments (but not your entire program or add function) to illustrate what you learned.

  4. Extra: Time permitting, an implementation where each input is not only the value to be added, but also the index position at which it should go on the list.

Reading

  1. Carefully review class notes that discussed the lab!

  2. See the detailed explanation of malloc, calloc, and realloc at: cs.binghamton.edu/~nael/classes/cs220-f13/lectures/C-tutorial/part4.html

  3. C Pointers Tutorial by Ted Jensen:

    • Ch 8: Pointers to Arrays (pp. 32-33)

    • Ch. 9: Pointers and Dynamic Allocation of Memory, pp. 36-40 (last time it was pp. 34-36)

Lab Instructions / Questions


Begin working with the Replit starter code (also viewable at the bottom of this page). Assume that the array starts with space for 3 integers. As we add integers, when we try to add a new integer to a full array the program should do the following:

  • If the array is full, then:

  1. Allocate space for new array (pNew) that is the size of the current one (pArray), plus 3 more.

  2. Copy over the current array (pArray) into the new one (pNew)

  3. Free up the space from the current array (pArray)

  4. Make the current array pointer (pArray) now point to the new array (pNew)

  5. Update the new array max size

  • Add the new element into the new currrent array (pArray)

  • Increment the variable storing the current array (pArray) size

Very Important


Work in main() first

First implement the above code within main and get it to work there. Only then attempt to move it to the add(...) function, following our rules for reference parameters: 1. Pass with &, 2. Catch with *, 3. Use with *.


Parenthesis for Precedence

Once you do move your code from main() into the add(...) function, you will likely have to add some parenthesis, since [ ] and ++ have precedence over *. If you have something like:

value++;

and then you make value a reference parameter, you might be tempted to have:

*value++; // Wrong!

but that is incorrect, since it first increments the address to point to the next value, and only then tries to do the dereference, which could give you a memory out of bounds error and crash your program. So what you have to have in situations like this is:
(*value)++;

This is similarly true if you have a pointer variable which you use with array notation, such as:

pArray[ i] = 3;

When you make pointer pArray into a reference parameter, you might be tempted to have:

*pArray[ i] = 3; // Wrong!

but similarly that won't work, and you need to have:

(*pArray)[ i] = 3;

Extra


If you have time, enhance your program so that your input number to be stored is followed by the index where that number should be placed in the array. The array should grow as necessary to accommodate any index value. The main part of your program should now look like:
printf("Enter value and index: ");
scanf("%d %d", &newNumber, &index); // Provide both value and index
add( newNumber, index, &pArray, &currentSize, &maxSize); // Also send the index


Starter Code