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

Please Answer All Questions Correctly For Upvote Memory leak detection exercise

ID: 3599491 • Letter: P

Question

Please Answer All Questions Correctly For Upvote

Memory leak detection exercise Program B struct actor 4. Where should the actor object linked to c be 1.Which piece of code struct actor has memory lca a Neither A nor B b) Both A and B c) A char name 32]; char *name; struct actor *next; } *head NULL; struct actor *next; 'eed: } *head NULL; a) In get name) in A b) In insertion0 in A char *get_name() f char *q; char *get_name() c) In main) function { char *q; d) In deletion() function 2. Which piece of memory is leaked? q = (char *) malloc(32); printf("Please enter a name: "); scanf("%s", q): return q; malloc(32) in A b) malloc(32) in B c) char name 32] in A d) char name[32] in B q (char *) malloc(32); printf("Please enter a name: "); scanf("%s", q): return q; a) int insertion() { actor "c; char *r; int insertion() { actor *c; char *n; malloc(sizeof(struct actor)); if(c== 0) { c malloc(sizeof(struct actor)); if(c== 0) { Antcomelre printf("out of memoryln"); return -1;) n = get-name(); strcpy(c->name, n); free(n) memory be freed? c->next head; head-c return 1 printf( out of memoryln"); return -1;) c->name = get-name(); c->next = head head = c return 1 3. Where should the 5. When vou write the delction function for A & B how many free0 calls are required? Select all that apply. I One for Program A [ ] Two for Program A [ ] One for Program B I] Two for Program B a) In get-name() in A b) In get name0 in B c) In insertion0 in A !; d) In insertion() in B CSE240 Please take online test after the lecture! /23/2017

Explanation / Answer

Both A and B piece of code have memory leak because code segment tries to access this q before a value has been assigned to it, it might get that garbage value and your program could behave mysteriously. to resolve this memory leak problem you need to use memset() function

q=(char*)malloc(32);

above piece of code has memory leak problem.

After calling the malloc function on both piece of code the memory should be freez.

Actor object linked to object when the insertion function called and set next node value in the list and set to head value C.

When we write the deletion for A & b we need to one free () call for piece of code A and two free() calls for piece od code B.Because due to one malloc call in piece of code block one and