Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1. Debug the sources to make the program stop crashing. The program crashes from the start. Fix enough of the problems to make it run

1. Debug the sources to make the program stop crashing.

The program crashes from the start. Fix enough of the problems to make it run without crashing. Document fixes in the text file. The text file entries must identify the errors fixed and the function(s) in which those errors were found.

2. Use Valgrind to fix the memory leaks. document the fixes in the text file. The file entries must identify the errors fixed and found. The file entries should describe why those errors occurred.

**********IMPORTANT****************

YOU SHOULD ONLY MODIFY THESES TWO FILES ("Tedtalk.c","Tedtalktest.c")

**********IMPORTANT****************

=========================================

FILES

=========================================

tedtalk.c file below (does not work and crashed)

=========================================

#include #include

#include "EventTime.h"

/// TEDtalk structure is private to the .c file.

struct TEDtalk { char * speaker; char * title; EventTime_t * lastPlayed; };

/// Note: The TEDtalk_t typedef is defined in the .h file

#include "tedtalk.h"

/// Initialize a TEDtalk_t structure with the given values.

TEDtalk_t * newTEDtalk(char * t, char * a) {

TEDtalk_t * m = NULL; m = malloc(sizeof(TEDtalk_t));

m->speaker = malloc(strlen(a) + 1); strcpy(m->speaker, a);

m->title = malloc(strlen(t) + 1); strcpy(m->title, t);

m->lastPlayed = NULL;

return m; }

/// delete the talk

void tedtalkDelete(TEDtalk_t * m) {

//speaker free(m->speaker); //title free(m->title); //time if (NULL != m->lastPlayed) { timeDelete(m->lastPlayed); } }

/// get the talk's speaker

char * tedtalkGetSpeaker( const TEDtalk_t * m) { char * tmp = NULL; tmp = malloc(strlen(m->speaker)+1); strcpy(tmp, m->speaker); return tmp; }

/// get the talk's title

char * tedtalkGetTitle( const TEDtalk_t * m) { char * tmp = NULL; tmp = malloc(strlen(m->title)+1); strcpy(tmp, m->title); return tmp; }

/// get the talk's last play time

EventTime_t * tedtalkGetLastPlayed( const TEDtalk_t * m) { EventTime_t * tmp = NULL; tmp = timeCopy(m->lastPlayed); return tmp; }

/// play or show the talk

void tedtalkPlay(TEDtalk_t * m, EventTime_t * t) { if (NULL != m->lastPlayed) { free(m->lastPlayed); } m->lastPlayed = t; }

/// compare 2 instances for equivalence (equivalent field values)

bool tedtalkEquals( const TEDtalk_t * m, const TEDtalk_t * o) { // Two tedtalks are equal if their titles and speakers are equal if ((NULL != m) && (NULL != o)) { if ((0 == strcmp(m->speaker, o->speaker)) && (0 == strcmp(m->title, o->title))) { return true; } } return false; }

/// make a complete copy of the talk

TEDtalk_t * tedtalkCopy( const TEDtalk_t * m) { TEDtalk_t * d = NULL; EventTime_t * tmp = NULL;

d = malloc(sizeof(TEDtalk_t));

d->speaker = malloc(sizeof(m->speaker) + 1); strcpy(d->speaker, m->speaker);

d->title = malloc(sizeof(m->title) + 1); strcpy(d->title, m->title);

if (NULL != m->lastPlayed) { // copy the last played tmp = timeCopy(m->lastPlayed); d->lastPlayed = tmp; } else { // set lastPlayed to NULL d->lastPlayed = NULL; }

return d; }

/// create a string representation

char * tedtalkToString( const TEDtalk_t * m) { char * sstr = NULL; char * st = NULL; char * sd = NULL; char * tt = NULL; int len = 0;

st = tedtalkGetTitle(m); sd = tedtalkGetSpeaker(m);

// calculate the total string length needed. len = strlen("Title: ") + strlen(st) + strlen(" Speaker: ") + strlen(sd) + 1;

if (NULL != m->lastPlayed) { tt = timeToString(m->lastPlayed); len += strlen(" at ") + strlen(tt); }

// allocate enough space for the tedtalk sstr = malloc( len );

sprintf(sstr, "Title: %s Speaker: %s", st, sd);

if (NULL != m->lastPlayed) { sstr = strcat(sstr, " at "); sstr = strcat(sstr, tt); }

free(sd); free(st); free(tt); return sstr; }

=========================================

tedtalk.h file below

=========================================

#ifndef EVENTTIME_H #define EVENTTIME_H

#include

typedef struct EventTime { ///< structure for the time of an event int hour; ///< start hour int min; ///< start minute char * timeofday; ///< start text of the time of day } EventTime_t ;

/// construct and initialize a time structure /// @param hour an event's hour of delivery/presentation /// @param min an event's hour of delivery/presentation /// @param tod text for the time of day /// @return pointer to a new event time instance

EventTime_t * newEventTime( int hour, int min, char * tod ) ;

/// delete the event time structure /// @param t an event time instance pointer

void timeDelete( EventTime_t * t ) ;

/// copy a time creating space if needed. /// @param src an event time instance pointer /// @return pointer the copy to a new event time instance

EventTime_t * timeCopy( const EventTime_t * src );

/// @param t an event time instance pointer /// @return this time's hour

int timeGetHour( const EventTime_t * t );

/// @param t an event time instance pointer /// @return this time's minutes

int timeGetMinute( const EventTime_t * t );

/// @param t an event time instance pointer /// @return this time's am/pm status

char * timeGetTOD( const EventTime_t * t );

/// @param t an event time instance pointer /// @return a string representation of the time

char * timeToString( const EventTime_t * t ) ;

#endif

=========================================

eventtime.c file below

=========================================

#include #include

#include "EventTime.h"

/// allocates space for a new event time structure, copies /// the tod string passed in

EventTime_t * newEventTime( int hour, int min, char *tod ) { char *mtod ; mtod = malloc(3 * sizeof(char)) ; // tod is always two characters EventTime_t *t = malloc(sizeof(EventTime_t)) ;

// verify hour, minute and tod values hour = hour % 12 ; if (0 == hour) { hour = 12 ; } min = min % 60 ;

// verify that the tod is "am" or "pm" if ((0 != strcmp(tod, "am")) && (0 != strcmp(tod, "pm"))) { strcpy(mtod, "xm") ; } else { strcpy(mtod, tod) ; }

// initialize structure t->hour = hour ; t->min = min ; t->timeofday = malloc((strlen(mtod) + 1) * sizeof(char)) ; strcpy(t->timeofday, mtod) ;

// clean up before leaving free(mtod) ;

return t ; }

/// time destructor

void timeDelete(EventTime_t *t) { free(t->timeofday) ; // has to be done explicitly free(t) ; }

/// copy a time structure

EventTime_t * timeCopy(const EventTime_t *src) { EventTime_t *dest = NULL ;

dest = malloc(sizeof(EventTime_t )) ;

dest->hour = src->hour ; dest->min = src->min ; dest->timeofday = malloc((strlen(src->timeofday) + 1) * sizeof(char)) ; strcpy(dest->timeofday, src->timeofday) ;

return dest ; }

/// getHour

int timeGetHour(const EventTime_t *t) { return t->hour; }

/// getMinute

int timeGetMinute(const EventTime_t *t) { return t->min; }

/// getTOD

char * timeGetTOD(const EventTime_t *t) { return t->timeofday ; }

/// timeToString - convert the contents of an EventTime_t structure /// to a string representation /// allocates space for the string

char *timeToString(const EventTime_t *t) {

char *tstr = NULL ;

// allocate enough space for the time as a string tstr = malloc((2 + 1 + 2 + 2 + 1) * sizeof(char)) ;

// put the string together sprintf(tstr, "%2d:%02d%s", timeGetHour(t), timeGetMinute(t), t->timeofday) ;

return tstr ; }

=========================================

eventtime.h file below

=========================================

#ifndef EVENTTIME_H #define EVENTTIME_H

#include

typedef struct EventTime { ///< structure for the time of an event int hour; ///< start hour int min; ///< start minute char * timeofday; ///< start text of the time of day } EventTime_t ;

/// construct and initialize a time structure /// @param hour an event's hour of delivery/presentation /// @param min an event's hour of delivery/presentation /// @param tod text for the time of day /// @return pointer to a new event time instance

EventTime_t * newEventTime( int hour, int min, char * tod ) ;

/// delete the event time structure /// @param t an event time instance pointer

void timeDelete( EventTime_t * t ) ;

/// copy a time creating space if needed. /// @param src an event time instance pointer /// @return pointer the copy to a new event time instance

EventTime_t * timeCopy( const EventTime_t * src );

/// @param t an event time instance pointer /// @return this time's hour

int timeGetHour( const EventTime_t * t );

/// @param t an event time instance pointer /// @return this time's minutes

int timeGetMinute( const EventTime_t * t );

/// @param t an event time instance pointer /// @return this time's am/pm status

char * timeGetTOD( const EventTime_t * t );

/// @param t an event time instance pointer /// @return a string representation of the time

char * timeToString( const EventTime_t * t ) ;

#endif

=========================================

tedtalktest.c file below

=========================================

#include #include

#include "tedtalk.h" #include "EventTime.h"

/// main function tests the tedtalk module. /// @returns errorCode error Code; EXIT_SUCCESS if no error

int main( void ) {

EventTime_t * atime = NULL ; TEDtalk_t * talk1 = NULL ; TEDtalk_t * talk2 = NULL ; TEDtalk_t * talk3 = NULL ;

printf( "Creating a time... " ) ; atime = newEventTime( 6, 30, "pm" ) ;

printf( "Starting tedtalk tests... " ) ;

talk1 = newTEDtalk( "Do schools kill creativity?", "Ken Robinson" ) ; printf( "First tedtalk initialized... " ) ;

char * speaker = tedtalkGetSpeaker( talk1 ) ; char * title = tedtalkGetTitle( talk1 ) ; printf( "The current tedtalk is: \t%s by %s. ", title, speaker) ; free( speaker ) ; free( title ) ;

char * mstr = tedtalkToString( talk1 ) ; printf( "Otherwise... %s ", mstr ) ;

tedtalkPlay( talk1, atime ) ; mstr = tedtalkToString( talk1 ) ; printf( "After playing the tedtalk... \t%s ", mstr ) ;

EventTime_t * glp = tedtalkGetLastPlayed( talk1 ) ; char * sglp = timeToString(glp) ; speaker = tedtalkGetSpeaker( talk1 ) ; title = tedtalkGetTitle( talk1 ) ; printf( "The same tedtalk referencing members is: \t%s by %s. Last played at: %s ", title, speaker, sglp ) ; free( speaker ) ; free( title ) ; timeDelete( glp ) ; free( sglp ) ;

tedtalkDelete( talk1 ) ;

speaker = malloc( sizeof( "Amy Cuddy" ) + 1 ) ; title = malloc( sizeof( "Your body shapes who you are" ) + 1 ) ; strcpy( speaker, "Amy Cuddy" ) ; strcpy( title, "Your body shapes who you are" ) ; talk2 = newTEDtalk( title, speaker ) ; free( speaker ) ; free( title ) ; mstr = tedtalkToString( talk2 ) ; printf( "The talk2 is: \t%s ", mstr ) ; talk1 = tedtalkCopy( talk2 ) ; mstr = tedtalkToString( talk1 ) ; printf( "The copy of talk2 tedtalk is: \t%s ", mstr ) ; tedtalkDelete( talk2 ) ; mstr = tedtalkToString( talk1 ) ; printf( "After deleting the original talk2, the copy of talk2 " "tedtalk is ... \t%s ", mstr ) ;

char * pstr ; talk3 = tedtalkCopy( talk1 ) ; if ( tedtalkEquals( talk1, talk3 ) ) { mstr = tedtalkToString( talk1 ) ; pstr = tedtalkToString( talk3 ) ; printf( "The talk3 tedtalk \t%s " " is the same as this tedtalk \t%s ", pstr, mstr ) ; free( pstr ) ; } else { printf( "ERROR: tedtalkCopy() failure! " ) ; }

tedtalkDelete( talk3 ) ; tedtalkDelete( talk1 ) ;

talk3 = newTEDtalk( "How great leaders inspire action", "Simon Sinek" ) ; mstr = tedtalkToString( talk3 ) ; printf( "The current talk3 tedtalk is: \t%s ", mstr ) ;

tedtalkPlay( talk3, newEventTime(4, 42, "am") ) ;

mstr = tedtalkToString( talk3 ) ; printf( "The current talk3 tedtalk is: \t%s ", mstr ) ; free( mstr ) ;

tedtalkDelete( talk1 ) ; tedtalkDelete( talk3 ) ;

return EXIT_SUCCESS ; }

=========================================

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Concepts of Database Management

Authors: Philip J. Pratt, Mary Z. Last

8th edition

1285427106, 978-1285427102

More Books

Students also viewed these Databases questions

Question

what you would do to address the situation.

Answered: 1 week ago

Question

What is the most important part of any HCM Project Map and why?

Answered: 1 week ago