From 08677204ca0f38b109a26811fe4a88538cb379c7 Mon Sep 17 00:00:00 2001 From: cdez Date: Sun, 7 Jan 2024 22:04:12 -0700 Subject: [PATCH] Fixed most of the problems with the StringUtils +- Phase6_Prep.c +- StringUtils.c Note: Lev did things wrong using malloc in the videos. Corrections have been made. Wrong: malloc( sizeof( sourceStrLen + 1 ) ) Correct: malloc( sizeof(char) * (sourceStrLen + 1) ) This will correctly fix most of the dynamic memory allocation problems that will arise from programs described in the video. --- Preparation/Phase6/Phase6_Prep.c | 2 ++ Preparation/Phase6/StringUtils.c | 10 +++++----- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/Preparation/Phase6/Phase6_Prep.c b/Preparation/Phase6/Phase6_Prep.c index 9da87f9..7e535b7 100644 --- a/Preparation/Phase6/Phase6_Prep.c +++ b/Preparation/Phase6/Phase6_Prep.c @@ -66,10 +66,12 @@ int main() printf("\nLowercase String: '%s'\n", testStringFour); // string get to delimiter // stringDelimiter + printf("\nAbout to get string to delimiter -> '.' from standard in:\n"); getStringToDelimiter(stdin, '.', testStringFive); printf("\nCaptured to delimiter: '%s'\n", testStringFive); // string get to end of line // stringEOL + printf("\nAbout to get string to line end from standard in:\n"); getStringToLineEnd(stdin, testStringFive); printf("\nCaptured to end of line: '%s'\n", testStringFive); diff --git a/Preparation/Phase6/StringUtils.c b/Preparation/Phase6/StringUtils.c index e1662d5..b4a55bf 100644 --- a/Preparation/Phase6/StringUtils.c +++ b/Preparation/Phase6/StringUtils.c @@ -220,14 +220,14 @@ bool getStringConstrained(FILE *inStream, bool clearLeadingNonPrintable, char delimiter, char *capString) { // initialize variables - int intChar = -1, index = 0; + int intChar = EOF, index = 0; // initialize output string capString[index] = NULL_CHAR; // capture first value in stream using fgetc intChar = fgetc(inStream); // loop to clear non-printable or space if indicated - while((((intChar != -1) && + while((((intChar != EOF) && (clearLeadingNonPrintable && (intChar < (int)SPACE))) || (clearLeadingSpace && (intChar == (int)SPACE)))) { @@ -236,13 +236,13 @@ bool getStringConstrained(FILE *inStream, bool clearLeadingNonPrintable, } // end loop to clear non-printable or space if indicated // check for end of file found - if(intChar == -1) + if(intChar == EOF) { // return false (failed) return false; } // loop to get input from file stream - while(((intChar != -1 && index < MAX_STR_LEN - 1) && + while(((intChar != EOF && index < MAX_STR_LEN - 1) && (stopAtNonPrintable && intChar >= (int)SPACE)) || ((!stopAtNonPrintable) && (intChar != (char)delimiter))) { @@ -400,7 +400,7 @@ void strToLowerCase(char *outString, const char *inString) int index = 0; // copy source string in case of aliasing using malloc and copyString - tempStr = (char *)malloc((sizeof(sourceStrLen + 1))); + tempStr = (char *)malloc((sizeof(char) * (sourceStrLen + 1))); copyString(tempStr, inString); // loop across source string while(tempStr[index] != NULL_CHAR && index < MAX_STR_LEN)