Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Does the below C code violate (MEM30-C. Do not access freed memory) if so can yo

ID: 3710059 • Letter: D

Question

Does the below C code violate (MEM30-C. Do not access freed memory) if so can you explain why of why not? And if it does please provide a code example in a screen shot of the way you fixed the code.

#include

#include

// Function prototypes
void fillPassword(size_t , char[]);
void showResults(char);
void printf_s_l();

// should have void listed
void showMenu();

// Define a variable to hold a password
// and the copy
char password[15];
char cpassword[15];
int main(void)

{
   // Welcome the User
   printf_s_l("Welcome to the C Array Program! ");

   // Variables
   char cont = 'y'; // To continue with loop
   int cVar = 0; // process variable

   // Display menu and Get Selection
   while (1)

   {
       // Display the Menu
       showMenu();

       scanf("%c", &cont);

      // Display the menu response
      showResults(cont);

       // if user wants to exit
       if( cont == 'e' || cont == 'E' )

        break;

        // to get the next line character
        scanf("%c", &cont);

   }

   // Call the Copy routine
   fillPassword(sizeof(password),password);

   // Display variable values
   printf("password is %s ", password);
   printf("cVar is %d ", cVar);

   // Copy password
   memcpy(cpassword, password,sizeof(password));

   // Pause before exiting
   char confirm;

printf("Confirm your exit!");

   confirm = getchar();   

   return 0;

}

// Make a String of '1's
void fillPassword(size_t n, char dest[]) {
   // Should be n-1
   for (size_t j = 0; j < n; j++) {  
       dest[j] = '1';
   }
   // Add null terminator for string
   dest[n] = '';
}

/* Display the Results*/
void showResults(char value) {

   switch (value){

   case 'F':
      printf("Welcome to the Football season! ");
       break;

   case 'f':
       printf("Welcome to the Football season! ");
       break;

   case 'S':   
       printf("Welcome to the Soccer season! ");
       break;

   case 's':
       printf("Welcome to the Soccer season! ");
       break;

   case 'B':   
       printf("Welcome to the Baseball season! ");

       break;       

   case 'b':
       printf("Welcome to the Baseball season! ");
       break;       

   case 'E':   
       printf("Exiting the Menu system! ");
       break;

   case 'e':
     printf("Exiting the Menu system! ");
       break;

   default:
       printf("Please enter a valid selection ");

   }

}

/* Display the Menu*/
void showMenu(void) {

   printf("Enter a selection from the following menu. ");

   printf("B. Baseball season. ");

   printf("F. Football season. ");

   printf("S. Soccer season. ");

   printf("E. Exit the system. ");

}

Explanation / Answer

No, you are not violating MEM30-C, because it is all about declaring the pointer and free up the head memory thereafter by de-referencing the pointer or de-allocating the pointer to null so the Heap Memory can be re-utilized by other object(s). In your program you have not used any pointer therefore you need not to de-reference it and hence you are not violating anything.

One thing, I want to bring your attention into the 'quality of coding' from what can i observed from this code Eg. Poor Coding of Switch Statements, etc. Its not at all a recommended. Hope, you will work on those skills as well. Thank you for Trusting Chegg. We are pleasured to help and assist you.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote