Question: / / Compile with: gcc - no - pie - g - Wl , - z , norelro - fno - stack - protector -

// Compile with: gcc -no-pie -g -Wl,-z,norelro -fno-stack-protector -U_FORTIFY_SOURCE -o heap2 heap2.c #include #include #include #define NAME_LENGTH 20 #define INPUT_LENGTH 20 int main(int argc, char *argv[]){ char input[INPUT_LENGTH],*pres,*vp,*jan; int choice=0; while (choice !=99){ printf("** MENU **
"); printf("[1] Allocate memory for president's name.
"); printf("[2] Input president's name.
"); printf("[3] Display president's name.
"); printf("[4] Free memory for president's name.
"); printf("[5] Allocate memory for vice president's name.
"); printf("[6] Input vice president's name.
"); printf("[7] Display vice president's name.
"); printf("[8] Free memory for vice president's name.
"); printf("[9] Allocate memory for janitor's name.
"); printf("[10] Input janitor's name.
"); printf("[11] Display janitor's name.
"); printf("[12] Free memory for janitor's name.
"); printf("[99] QUIT
"); fgets(input, INPUT_LENGTH-1, stdin); choice=atoi(input); switch (choice){ case 1: pres=malloc(NAME_LENGTH); break; case 2: printf("Enter president's name: "); fflush(stdin); fgets(pres, NAME_LENGTH-1, stdin); break; case 3: printf("President's name: %s
", pres); break; case 4: free(pres); break; case 5: vp=malloc(NAME_LENGTH); break; case 6: printf("Enter vice president's name: "); fflush(stdin); fgets(vp, NAME_LENGTH-1, stdin); break; case 7: printf("Vice president's name: %s
", vp); break; case 8: free(vp); break; case 9: jan=malloc(NAME_LENGTH); break; case 10: printf("Enter janitor's name: "); fflush(stdin); fgets(jan, NAME_LENGTH-1, stdin); break; case 11: printf("Janitor's name: %s
", jan); break; case 12: free(jan); break; }} printf("BYE.
"); return 0; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!