This is my practice exam, I've a exam tomorrow. Can anyone take a look at my answers and see if they're correct or not!! Also please add explanation

I tried to answer them below, can anyone take a look at them and help if they’re right or not? Thanks

  1. (10 points) Assume you are given 3 arrays: An array of integers intarr beginning at memory location 0x7B, an array
    of characters str at 0x5F, and an array of strings strarr at 0xB3. Calculate the memory locations of the following
    elements:

    (a) The 93rd element of intarr

(b) The 22nd element of str

© The 44th element of strarr

Hint: The type of a string is char*
  1. (10 points) Explain what it means to be pass by value, and how this differs from languages like Java or python which
    are pass by reference.

  2. (20 points) Consider the following snippets of code:

    ✞ ☎
    
    
    
    
     typedef struct llist {
         int val;
         llist *next;
         } llist;
         ✝ ✆
         ✞ ☎
         void set_next(llist node) {
         node newnode;
         newnode.val = 10;
         node.next = newnode;
         }
         void set_val(llist *node, int num) {
         node->val = num;
         }
         ✝ ✆
         ✞ ☎
         1 int main() {
         2 llist head;
         3 set_val(&head, 5);
         4 set_next(head);
         5 printf("%d\n", head.num)
         6 printf("%d\n", head.next->num);
         7 }
     ✝ ✆
    

    (a) (5 points) The definition of the struct has an error. Explain what is wrong, and write out code with the same
    intent that will compile.

    (b) (8 points) Assuming that everything is appropriately in scope, what is the output after running main?

    © (7 points) Modify the set next function (and possibly the function call in main) to change the output of line 6
    to be 7.

  3. (20 points) Consider the following snippets of code:

     ✞ ☎
         typedef struct coord {
         int index;
         double val;
         } coord;
         ✝ ✆
    
    
    
    
     ✞ ☎
         1 coord* construct1(int ix, double val) {
         2 coord new_coord;
         3 coord *ptr = &new_coord;
         4 ptr->index = ix;
         5 ptr->val = val;
         6 return ptr;
         7 }
         ✝ ✆
    

    :phone:

     8 coord* construct2(int ix, double val) {
     9 coord *ptr = (coord*) malloc(<TODO>)
     10 ptr->index = ix;
     11 ptr->val = val;
     12 return ptr;
     13 }
    
     ✝ ✆
    

(a) (10 points) Which is constructor is the “appropriate” choice? Explain.

(b) (10 points) Complete the arguments to malloc in line 9.

ANSWERS

  1. a. 0x7B + 93(4) //int is 4 bytes

    b. 0x5f + 22(1) //char is 1 byte

    c. 0xB3 + 44(8) //not sure about this one

  2. Pass by value means copy the value of passed argument and it doesn’t reflect a change in the original argument but when we pass the argument by reference we can change the original argument as we want in other function.

  3. a) The llist *next should be struct llist *next the original statement is missing the keyword struct.

b) 5

10

c)

void set_next(llist node) {   //assumed we passed the argument  head with reference 
    node newnode;
    newnode.val = 10;
    node.next = newnode;
    node.val = newnode.val;

    }
  1. Not sure can anyone answer this one?

For memory address, the answers seem correct to me- but I have no idea about char* thing. You may need to check it up. :3

(10 points) Explain what it means to be pass by value, and how this differs from languages like Java or python which are pass by reference.
2) Pass by value means copy the value of passed argument and it doesn't reflect a change in the original argument but when we pass the argument by reference we can change the original argument as we want in other function.

Answer is incomplete. Highlight a bit more on how pointer/memory address is passed in pass by reference. Then elaborate on whether JAVA and python are (by default) pass by value or pass by reference? When does which language uses what? Eg- Does JAVA pass objects by value or by reference? What if we dont wish an object to get changed?

Just writing the difference can be credited at most 4 marks, that to in best case only. (Its like this here :3 ). Well…if I were the professor I would certainly be a harsh one lol.

The llist *next should be struct llist *next the original statement is missing the keyword struct.

Yes.

 (7 points) Modify the set next function (and possibly the function call in main) to change the output of line 6 to be 7.

Didnt get it- why not simply do newnode.val = 10; ?? I think I got the question wrong lol.

4.a. It seems that construct 2 should be an ideal one due to memory allocation
4.b. malloc(sizeof(struct coord));

1 Like

Well I am late but anyways I will leave the answer here:

1 © => you are right about this one. Usually in C we really make an array of pointers to strings instead of array of datatype string(which does not exist). That’s the reason the hint mentions ‘char*’

Remember that these sizes are compiler/implementation dependent. Only size of char is guaranteed.

2 (b) => the answer would be 5 and garbage value/SIGSEGV.
2 © => there are 3 problems with set_next functions. first we don’t pass by reference, 2nd we are not assigning the address of node and 3rd that this newnode is allocated on stack and will be deleted after its scope ends.
oh there is also a 4th node is not a datatype but an identifier.

so:

void set_next(llist &node) {
    llist* newnode = malloc(sizeof llist);
    newnode->val = 10;
    node.next = newnode;
}

rest are already answered

1 Like

@vijju123 @taran_1407 can you guys help? please thanks

Thank you so much @vijju123 can you please elaborate 4) a a liitle bit more thank you!!

memory management - ANSI C do you have to use malloc() when creating a struct? - Stack Overflow This answer will help you better.

Also, I dont know, but can variable scope be a reason? I mean, once we exit constructor 1, will the object get destroyed leading to pointer leading to faulty stuff? Idk but its mostly gonna be a no to that i guess.

Thanks @vijju for the help you’re the best :slight_smile:

@vijju123 you’re right because the first constructor allocates memory on stack, the stack wipe itself after it exits the method.

Thanks for that kunnu :slight_smile:

1 Like

Thank you!!