Functions to allocate and free memory at part of the standard library
that is included thus:
#include <stdlib.h>
Also included is the symbolic identifier NULL
for the pointer to nothing (like null in Java):
#define NULL ((void) 0)
Note that NULL is a
form of 0, meaning
a NULL pointer
is false and all
other pointer values are true.
Space is allocated by calling malloc
with the number of bytes needed (for strings this is always one more
than the maximum length of the string to be stored):
char
*pc = malloc(MAXSTR + 1) ; // can hold a string
of up to MAXSTR characters.
pc = malloc(strlen("Hello!") + 1) ; // can hold a copy of "Hello"
with terminating NUL.
The size given to malloc is always in bytes; the sizeof operator
allows us to get the size in bytes of any type:
double *pd
;
pd = malloc(100 * sizeof(double)) ; // can hold up to 100 double
precision numbers.
double pi = 3.14159 ;
pd = malloc(100 * sizeof(pi)) ; // can hold
up to 100 double precision numbers.
Space no longer needed must be returned to the memory allocator using
free():
pc =
malloc(MAXSTR+1)
; // get
space for a string
//
. . . use the allocated space . . .
free(pc)
;
// free up the space associated with pc
Orphaned
storage:
char
buf[MAXSTR+1] ;
pc = malloc(MAXSTR + 1) ;
pc = buf
;
// we just lost the space allocated forever.
Dangling
references:
pc =
malloc(MAXSTR + 1) ;
char *pc1 = pc ;
// . . . use the allocated space . . .
free(pc) ;
// . . . work without changing pc or pc1 . . .
*pc = 'X' ; // PROBLEM: space was freed - may have been
reallocated
*pc1 = 'Z' ; // PROBLEM: pc1 aliased pc, so its space was also
freed
For this activity you will implement three versions of a basic filter
function to filter (remove) elements from a string; in two of the three
cases you will implement both an array version with indexing and a pointer
version using malloc()
and free().
Create a directory Filter
at the top level of your git repository.Download the file filter.zip
into this directory, extract
the archive contents, and remove the zip file:
unzip
filter.zip
rm filter.zip
Make the executable test_filter (the default target of the Makefile)
and execute the result:
make
./test_filter
At this point, use git to add, commit, and push the skeleton. This will show that you at least were able to initialize the project.
File filter.h declares the interfaces for five variants of a filter function using both character arrays and character pointers, file filter.c contains skeleton implementations of these functions, and test_filter.c contains unit tests for the functions (you may assume any arguments to the functions are not NULL, are properly terminated, and are large enough for the task at hand):
As always, complete the Activity Journal, including your estimated time, your plan, your actual time, and observations.
We will pull your repository after the due date; the assessment is in
terms of this pulled information. We will compile and link your program
using the simple command:
make
If this does not create a program test_filter
without syntax or linking errors you cannot receive a grade
above 35, and your grade will almost certainly be less than this.