Question

SELECT ALL OPTIONS BELOW THAT ARE TRUE FOR THE GIVEN CODE #include <string.h> #include <stdlib.h> #include...

SELECT ALL OPTIONS BELOW THAT ARE TRUE FOR THE GIVEN CODE

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#define MAX_STR_LEN 100

struct Node {
  char *data;
  struct Node *left;
  struct Node *right;
};

struct Node *new_node(char *d) {
  struct Node *r = malloc(sizeof(struct Node));
  r->data = malloc(sizeof(char) * MAX_STR_LEN);
  strncpy(r->data, d, MAX_STR_LEN);
  free(d);
  r->left = NULL;
  r->right = NULL;
  return r;
}

void freeSubTree(struct Node *r) {
  free(r->data);
  if (r->left != NULL) freeSubTree(r->left);
  if (r->right != NULL) freeSubTree(r->right);
  free(r);
}

void printSubTree(struct Node *r) {
  printf("%s ", r->data); 
  if (r->left != NULL) printSubTree(r->left);
  if (r->right != NULL) printSubTree(r->right);
}


int main() {
  struct Node *root = new_node("ABC"); 
  struct Node *left = new_node("DEF");
  struct Node *right = new_node("GHI");
  root->left = left;
  root->right = right;
  printSubTree(root);
  freeSubTree(root);
}

OPTIONS:

a.Memory leak: a Node hasn't been freed by the end of the program

b.Memory leak: a Node's item array. Root cause of leak is in new_node()  

c.Memory leak: a Node's item array. Root cause of leak is in freeSubTree()

d.Invalid free: when free called on a struct Node* in freeSubTree()

e.Invalid free: when free called on a char* in freeSubTree()  

f.Invalid free: when free called on a char* in new_node()

g.No memory errors or leaks

h.memory error in printSubTree(): when dereferencing r

i.Memory error in freeSubTree(): when dereferencing r

j.Memory error in printf caused by invalid argument: r->data holds invalid address

Homework Answers

Answer #1

malloc is used to allocate memory in heap dynamically or at the run time. In this process, operating system or compiler allocate requested memory the running process if available else malloc return NULL.

free is used to de-allocate the dynamically allocated memory back to operating system.

If this dynamically allocated memory is not de-allocated, operating system may not have enough memory to continue its function and it can result into program crash or even restart of the system. This problem is called memory leak.

In the above C program, only option 'f' is correct because it's trying to deallocate memory which is a string literal not a dynamically allocated memory and hence free doesn't have permission to de-allocate(write) it. All other options are false.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
For the following code in C, I want a function that can find "america" from the...
For the following code in C, I want a function that can find "america" from the char array, and print "america is on the list" else "america is not on the list" (Is case sensitive). I also want a function to free the memory at the end of the program. #include <stdio.h> #include <stdlib.h> struct Node { void *data; struct Node *next; }; struct List { struct Node *head; }; static inline void initialize(struct List *list) { list->head = 0;...
It is about C++linked list code. my assignment is making 1 function, in below circumstance,(some functions...
It is about C++linked list code. my assignment is making 1 function, in below circumstance,(some functions are suggested for easier procedure of making function.) void search_node(struct linked_list* list, int find_node_ value) (The function to make) This function finds the node from the list that value is same with find_node_value and count the order of the node. This function should print message “The order of (node_value) is (order).” and error message “Function search_node : There is no such node to search.”....
"C language" Take this code and make the minor modification necessary to create a circular linked...
"C language" Take this code and make the minor modification necessary to create a circular linked list (Hint: Store a pointer to the first node in the next pointer of the last node.) Demonstrate that this is working by traversing the list until the first pointer is encountered 3 times. Next redefine the node structure to include a back pointer. This will enable your program to move from front to back and then from back to front. It is not...
Helllp plz Allow the InsertAt method to add to the front position 0 (zero) also if...
Helllp plz Allow the InsertAt method to add to the front position 0 (zero) also if you try to add to a position greater than the number of nodes you get an error warning. /INSERT.C /*THIS PROGRAM READS A BINARY FILE (MUST ALREADY EXIST AND BE FILLED) */ /*AND PUTS IT INTO A LINKED LIST AND PRINTS THE LIST TO THE SCREEN) */ #include #include #include #include typedef struct ENTRY { char name[81]; }ENTRY; typedef struct LISTREC /* LISTREC is...
In this lab, you will write a program that creates a binary search tree based on...
In this lab, you will write a program that creates a binary search tree based on user input. Then, the user will indicate what order to print the values in. **Please write in C code** Start with the bst.h and bst.c base code provided to you. You will need to modify the source and header file to complete this lab. bst.h: #ifndef BST_H #define BST_H typedef struct BSTNode { int value; struct BSTNode* left; struct BSTNode* right; } BSTNode; BSTNode*...
Using the C programming language implement Heapsort in the manner described in class. Here is some...
Using the C programming language implement Heapsort in the manner described in class. Here is some example code to use as a guideline. Remember, you need only implement the sort algorithm, both the comparison and main functions have been provided. /* * * after splitting this file into the five source files: * * srt.h, main.c, srtbubb.c, srtinsr.c, srtmerg.c * * compile using the command: * * gcc -std=c99 -DRAND -DPRNT -DTYPE=(float | double) -D(BUBB | HEAP | INSR |...
i want to complete this code to insert a new node in the middle of list...
i want to complete this code to insert a new node in the middle of list (take a node data from user, search the node and insert new node after this node). this is the code #include <iostream> #include <stdlib.h> using namespace std ; struct Node{                int data;                Node *link ;}; struct Node *head=NULL, *tail=NULL; /* pointers to Node*/ void InsertFront(); void InsertRear(); void DeleteFront(); void DeleteRear(); int main(){                int choice;                do{                               cout << "1:...
Question: I get a Segmentation fault error sometimes when I addElementBack or print. Am I using...
Question: I get a Segmentation fault error sometimes when I addElementBack or print. Am I using pointers correctly and deleting memory properly? #ifndef DYNAMICARRAY_H #define DYNAMICARRAY_H #include <cstdlib> #include <iostream> using namespace std; // Node class class Node { int data; Node* next; Node* prev; public: Node(); Node(int); void SetData(int newData) { data = newData; }; void SetNext(Node* newNext) { next = newNext; }; void SetPrev(Node* newPrev) { prev = newPrev; }; int getData() { return data; }; Node* getNext()...
Data Structures using C++ Consider the following class #ifndef LINKEDQUEUETYPE_H #define LINKEDQUEUETYPE_H #include <iostream> #include <new>...
Data Structures using C++ Consider the following class #ifndef LINKEDQUEUETYPE_H #define LINKEDQUEUETYPE_H #include <iostream> #include <new>    #include <cstdlib> #include "QueueADT.h" using namespace std; // Definition of the node template <class ItemType> struct NodeType {        ItemType info;        NodeType<ItemType> *next; }; template <class ItemType> class LinkedQueueType: public QueueADT<ItemType> { public:        // Constructor        LinkedQueueType();           // Default constructor.           // Post: An empty queue has been created. queueFront = NULL;           //       queueBack = NULL;...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g,...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g, char wordlist[][MAX_WORD_LENGTH], int numwords)] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) What int setup_game needs to do setup_game() does exactly what the name suggests. It sets up a new game of hangman. This means that it picks a random word from the supplied wordlist array and...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT
Active Questions
  • In order to conduct a hypothesis test for the population proportion, you sample 450 observations that...
    asked 6 minutes ago
  • Doctor’s Order: Vancomycin 500mg tab i po q12h X 7 days Available: Vancomycin 500mg tablets What...
    asked 19 minutes ago
  • Calculate the ΔG∘rxn for the reaction using the following information. 4HNO3(g)+5N2H4(l)→7N2(g)+12H2O(l) ΔG∘f(HNO3(g)) = -73.5 kJ/mol; ΔG∘f(N2H4(l))...
    asked 20 minutes ago
  • Question 03: Saturn Shoes (Pvt.) Ltd manufacture multi-style fashion boots for the residents of Missouri. Leather...
    asked 22 minutes ago
  • A highway with a design speed of 100 km/hr is designed with a sag curve connecting...
    asked 34 minutes ago
  • Shift Registers can be used for serial/parallel interface applications. True or false?
    asked 1 hour ago
  • Scenario 1: To describe the instructors’ experience, the researcher records the year in which each instructor...
    asked 1 hour ago
  • develop a flowchart or pseudocode to check the prime numbers 1- below 100 what to do...
    asked 1 hour ago
  • Which of the following statements are true? I. The sampling distribution of ¯xx¯ has standard deviation...
    asked 1 hour ago
  • Which of the following methods of reporting cash flows provided by operating activities does the Financial...
    asked 1 hour ago
  • SITUATION 2: EFFECTIVE STRESS An engineer investigates a granular soil deposit, 4 meters thick, overlaying a...
    asked 1 hour ago
  • Suppose that R is a commutative ring and I is an ideal in R. Please prove...
    asked 1 hour ago