1 / 17

Survey Answers

Survey Answers. Advanced OOP CS 440/540 Spring 2014 Kenneth Chiu. Q5: Implement the two functions given below ( insert_before () and print_list ()) in C or C++:

rumor
Download Presentation

Survey Answers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Survey Answers Advanced OOPCS 440/540Spring 2014Kenneth Chiu

  2. Q5: Implement the two functions given below (insert_before() and print_list()) in C or C++: • structNode {intvalue;structNode *prev, *next;};// Sentinel node. Value is ignored. Initial prev and next point to// itself.struct Node head= {0, &head, &head};// Insert a node with the given value before the given position.void insert_before(Node *position, intval_to_insert) { ...}// Prints the whole list, starting with the first node.void print_list() { ...}

  3. A: Some people seem to have not been exposed to the idea of a sentinel node before. Some people had loops to find the previous node, which is not needed in a doubly-linked list. Some people forgot (or didn’t know) to allocate memory (used Node object on stack). • structNode {int value;struct Node *prev, *next;};struct Node head = {0, &head, &head};void insert_before(struct Node *position, intval_to_insert) {struct Node *n = malloc(sizeof(Node)); n->value = val_to_insert; n->prev = position->prev; n->next = position; n->prev->next = n; position->prev = n;}void print_list() { for (struct Node *n = head->next; n != &head; n = n->next) {printf(“%d\n”, n->value); }}

  4. Q6:Using the above data structure, write a code fragment to construct a list of 1000 such objects. The values should be from 1 to 1000, in order. The objects must be allocated on the heap. However, you must use only one call to malloc()or new to allocate all 1000 objects. A: Basic idea is to allocate, then link. • structNode *a = malloc(1000*sizeof(Node));for (inti = 0; i < 1000; i++) { a[i].value = i;}for (inti = 0; i < 999; i++) { a[i].next = &a[i+ 1];}for (inti = 1; i < 1000; i++) { a[i].prev = &a[i - 1];}head->next = &a[0];a[0].prev = &head;head->prev = &a[999];a[999].next = &head;

  5. Q7: Write code to test at run-time whether the machine is little-endian or big-endian? If you were required to determine the same information at compile-time, how would you do it? A: Leverage the meaning of endianness. • int one = 1;if (*(char *)&one == 1) {printf(“???\n”);} else {printf(“???\n”);} • If you need to know at compile-time, you will need to use some kind of preprocessor symbol/name.

  6. Q8: How do you debug a memory leak? A: Why is it hard? • Use valgrind, Purify, etc. • If you can’t, you might be able to watch it in ‘top’. • To fix, if you can’t use valgrind, then you it becomes very difficult.

  7. Q9: What causes a segfault? How do you debug it? A: A segfault is caused by access to “unmapped” memory. It doesn’t exist, in other words. • Typically, it means you somehow got garbage values into a pointer. • valgrind is the best tool. Otherwise, gdb can be used. • Some people wrote that you’d get a segfault if it was freed, which could happen, but often doesn’t. • Incorrect alignment usually gives bus error.

  8. Q10: You have a program that runs fine when compiled on Linux, but Solaris crashes right after printing some strange garbled output. What is the most likely cause? A: Obviously something is different, but what?

  9. Q11: Is this program correct? What do you think it will print out when you compile and run it? Explain why. • #include <stdio.h>#include <string.h>#include <stdlib.h>int main() { char *s = (char *) malloc(10);strcpy(s, “hello”); free(s);printf(“%s\n”, s);} • [survey/q11.cpp]

  10. Q12:What are the advantages of inline functions over preprocessor macros? • Inline functions don’t have problems with double evaluation. • They are processed by the primary compiler pass, so it can do more with them (such as choose not inline them). • Type safety? Couple people mentioned that. Would this give you a type error? • #define SQR(v) (v)*(v)template <typename T>inline T sqr(const T &v) { return v*v; }SQR(“a”);sqr(“a”);

  11. Q13:Consider the definitions below: • char *p, array[10];char *p2 = array; What is the value of sizeof(p), sizeof(array), and sizeof(p2), respectively? • Size of a pointer (4 or 8, typically), 10, and size of a pointer. Is the value of sizeof a compile-time constant? • Yes, in standard C++, but not always in latest version of C. • Can’t use it in preprocessor, though. • Note that it cannot be both a compile-time constant and a function. Is sizeof a function or an operator? • Operator. If it were a function, what’s the prototype? • size_tsizeof(???);sizeof(int); // Can there be a prototype for this? • Also, if it were a function, you could take it’s address.fp = &sizeof;

  12. Q14. [survey/q14.cpp] A: Can also use conversion. Can’t turn base into a template, however.

  13. 15. [survey/q15.cpp]

  14. Q16: Does this code have a compile-time (syntax) error? How about a run-time error? Explain. • #include <string.h>class String { public: String(const char *s) {buf = new char[strlen(s) + 1];strcpy(buf, s); } ~String() { delete [] buf; } private: char *buf;};int main() { String s1("hello"); String s2(“goodbye”); s2 = s1;}

  15. Q16: Assume that you have a file named foo in the current directory. The first four bytes contain an integer in binary using two’s complement (in native byte order). Let this integer have the value N. The next N bytes then contain a string. The string is not terminated with a 0 (null) byte. Write a program in any language and OS that will read in the string and print it to the console (standard output). Q14:First read the length, then read the rest. • #include <unistd.h>#include <stdlib.h>#include <assert.h>int main() {intrv;intfd = open(“foo”, O_RDONLY); assert(fd >= 0);unsigned int n;rv = read(fd, &n, 4); assert(rv == 4);char *str = malloc(n + 1);str[n] = ‘\0’;rv = read(fd, str, n); assert(rv == n);rv = close(fd); assert(rv == 0);printf(“%s\n”, str);}

  16. Q18: What does a linker do? A: Many people used the word “link” in explaining what it does. It resolves symbol references in object code to addresses and/or numbers, and combines the files into one file (via relocation), usually an executable.

  17. Q19: Why is a shared library called a shared library? A: Most people got the idea that there is sharing going on, but seem a bit fuzzy about where it’s happening. Because it’s memory image can be shared by multiple processes.

More Related