COMP1911 23T2 Introduction to Programming

Objectives

In this Lab, you will practise:

Preparation

Before the lab you should re-read the relevant lecture slides and their accompanying examples

Getting Started

Login and run following commands inside a Unix terminal

Create a new directory for this lab called lab08 by typing:

mkdir lab08
Change to this directory by typing:
cd lab08

Introduction

This week we will be reinforcing your understanding of functions, arrays, and strings.

Each activity covers one of these points, however it requires the use of malloc and free.

Remember that the standard pattern for malloc is (if I'm mallocing an "int")


// This is a stack variable, this is what we'd do without using malloc
int a = 7;
printf("a: %d\n", a);

// But we're going to use malloc now instead!

// This is how to create and initialise a heap variable
int *b = malloc(sizeof(int));
if (b == NULL) {
    printf("malloc error\n");
    exit(1);
}
*b = 7;

// Then we'll use our heap variable
printf("b: %d\n", *b);

// Then when we're done we'll free it, and set it's value to NULL
free(b);
b = NULL;


Exercise 1: Mallocing Memory

Modify the C program we've written mallocMem.c which uses malloc to create memory on the heap for variables.

You can copy this file into your current directoy by running

$ cp /web/cs1911/current/tlb/08/mallocMem.c .
$ dcc mallocMem.c -o mallocMem
heapInteger: 3
heapDouble: 4.0

If malloc fails at any point (due to not being able to allocate memory), please print the following error:

malloc error

Once you get the program functioning, make sure you have correctly used "free" function to free the memory, and then set the pointers to be equal to NULL (it's good practice).

 $ 1911 autotest lab08 mallocMem.c

Exercise 2: Malloc and Strcpy

Modify the C program we've written strcpy.c which takes in a single command line argument, creates a copy of it using malloc, and then prints both.

To create a copy of a string, you first need to malloc space for the new character array (don't forget to add an extra "+ 1" for the '\0' terminator!).

Once you have malloc'd this you then need to use strcpy to copy data from the old string into the new string.

You can copy this file into your current directoy by running

$ cp /web/cs1911/current/tlb/08/strcpy.c .

If no arguments are passed in, or more than 1 argument is passed in, then the program should execute the following code:

printf("Please enter 1 argument: Your string\n");
exit 0;
Expected output includes:
$ ./strcpy "Hello there"
String 1: Hello there
String 2: Hello there
$ ./strcpy "Hello there" "Second argument"
Please enter 1 argument: Your string

If malloc fails at any point (due to not being able to allocate memory), please print the following error:

malloc error

Once you get the program functioning, make sure you have correctly used "free" function to free the memory, and then set the pointers to be equal to NULL (it's good practice).

 $ 1911 autotest lab08 strcpy.c

Exercise 3: Building an Array

Modify the program we've written arrayBuild.c which takes a command line argument (a set of numbers 0-9) and populate a 3 x 3 array with those numbers.

You can copy this file into your current directoy by running

$ cp /web/cs1911/current/tlb/08/arrayBuild.c .

E.G. Passing in "123456789" is passing in 1, 2, 3, 4, 5, 6, 7, 8, 9. Then we fill our array the same way we write in English. Across each row, top to bottom. So an input argument of "123456789" would result in a 2D array that could be visualised like

1 2 3
4 5 6
7 8 9

This is a very efficient way to populate an array via command line.

Similar to previous programs, things may go wrong, and the appropriate error must be printed.

If the user doesn't input the right number of arguments, print:

Please enter 1 argument: Your array

If the user enters the right argument, but it's the wrong size (not 3 x 3 worth of elements) print:

Please enter the correct size

If malloc fails at any point (due to not being able to allocate memory), please print the following error:

malloc error

In this exercise the 2D array is represented by a type int** which is the same as int[][].

 $ 1911 autotest lab08 arrayBuild.c

Submission/Assessment

When you are satisfied with your work, ask your tutor to assess it. You are to submit it electronically by typing (run this command in your lab08 directory):
give cs1911 lab08 mallocMem.c strcpy.c arrayBuild.c
Submit advanced exercises only if you attempt the advanced exercise.

Remember the lab assessment guidelines - if you don't finish the exercises you can finish them in your own time, submit them by 19:59:59 Sunday using give and ask ask tutor to assess them at the start of the following lab.

You can also just run the autotests without submitting by typing

1911 autotest lab08