/* * Copyright 2011 Steven Gribble * * This file is the solution to an exercise problem posed during * one of the UW CSE 333 lectures (333exercises). * * 333exercises is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * 333exercises is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with 333exercises. If not, see . */ // Lecture 4 exercise 2 // Implement AllocSet( ), FreeSet( ) // - AllocSet( ) needs to use malloc twice: // - once to allocate a new ComplexSet // - once to allocate the “points” field inside it // // - FreeSet( ) needs to use free twice #include // needed for printf() #include // needed for assert() #include // needed for malloc() typedef struct complex_st { double real; // real component double imag; // imaginary component } Complex; typedef struct complex_set_st { int num_points_in_set; Complex *points; // an array of Complex } ComplexSet; // here are the prototypes we need to implement ComplexSet *AllocSet(Complex c_arr[], int size); void FreeSet(ComplexSet *set); int main(int argc, char **argv) { Complex arr[5]; ComplexSet *x; x = AllocSet(arr, 5); assert(x->num_points_in_set == 5); FreeSet(x); return 0; } ComplexSet *AllocSet(Complex c_arr[], int size) { ComplexSet *newset; Complex *pointarray; int i; assert(size > 0); // malloc a new ComplexSet newset = (ComplexSet *) malloc(sizeof(ComplexSet)); if (newset == NULL) return NULL; // malloc the new pointarray pointarray = (Complex *) malloc(size*sizeof(Complex)); if (pointarray == NULL) { // sneaky case: free the malloc'ed ComplexSet if we fail! free(newset); return NULL; } // initialize the fields newset->points = pointarray; for (i=0; inum_points_in_set = size; return newset; } void FreeSet(ComplexSet *set) { // do some sanity checks assert(set != NULL); assert(set->points != NULL); // free the point array within the structure free(set->points); // good hygiene to set it to NULL after freeing set->points = NULL; // free the ComplexSet itself free(set); }