Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#include #include #include typedef struct tree { struct tree* left; union { struct tree* right; int data; } rd; } Inttree; Inttree* lefttree(Inttree* t) {

image text in transcribedimage text in transcribed

#include  #include  #include  typedef struct tree { struct tree* left; union { struct tree* right; int data; } rd; } Inttree; Inttree* lefttree(Inttree* t) { return t->left; } Inttree* righttree(Inttree* t) { return t->left ? t->rd.right : NULL; } bool isleaf(Inttree* t) { return !(t->left); } void printtree(Inttree* t) { if(t == NULL) return; if(isleaf(t)) printf("%d ", t->rd.data); else { printtree(lefttree(t)); printtree(righttree(t)); } } void freetree(Inttree* t) { if(t == NULL) return; if(!isleaf(t)) { freetree(lefttree(t)); freetree(righttree(t)); } free(t); } Inttree* maketree(Inttree* left, Inttree* right) { Inttree* t = malloc(sizeof(Inttree)); t->left = left; t->rd.right = right; return t; } Inttree* makeleaf(int val) { Inttree* t = malloc(sizeof(Inttree)); t->left = NULL; t->rd.data = val; return t; } void q1() { char str[] = {65, 83, 67, 73, 73, 32, 105, 115, 32, 102, 117, 110, 0}; Inttree root; root.left = maketree(makeleaf(3), maketree(makeleaf(1), makeleaf(4))); root.rd.right = maketree(maketree(makeleaf(1), makeleaf(5)), maketree(makeleaf(9), makeleaf(3))); printf("%s ", &(str[0])); printtree(&root); printf(" "); freetree(root.left); freetree(root.rd.right); } static Inttree* initializetree() { return NULL;//CODE OMITTED; } void q2() { int a1 = 0xDEADBEEF; char str[20]; int a2 = 0xABACABAC; Inttree* tree = initializetree(); //Code omitted //Breakpoint 1 printf("%s ", &(str[0])); printtree(tree); printf(" "); } int main(int argc, char** argv) { q1(); q2(); }
Consider the following code: memory, c. You may find this reference useful on the difference between structs and unions. Tip: Recall that % prints an integer (in decimal) and % prints out a string (characters at increasing memory addresses up until the NULL terminator). Q1.1: During the function call q1 ( ), what two lines get printed? You may find an ASCII table (like this one) useful. We run GDB on memory. c on a 64-bit little-endian system, up to the line marked Breakpoint 1 . We then run several GDB commands; the commands and their outputs are here: memoryGDBoutput, txt Quick reference guide: print expr evaluates expr and prints out the result. x/20xw expr prints out 20 words of memory, starting at the given memory address, as if memory was an array of word-size values, in hexadecimal. $sp is a pointer to the bottom of the stack. Q2.1: Given the information from the GDB memory dump, what two lines get printed by q2 ( ) ? Hint: Try and find where the two constants are in the stack. What information in the GDB dump could you have used to find those constants, if you didn't know their actual values? Hint: Remember that when printing a string, it prints characters at increasing memory addresses until we reach the NULL terminator (byte 0x00). Hint: You would need to recreate the tree by following the pointers (addresses) read from the memory dump. You would need to follow how the structure data is defined to properly recreate the tree (identifying where the left and right pointers are)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions