2

im currently finishing 6.s081 by myself. and there are some codes i truely dont understand

  // Allocate process.
  if((np = allocproc()) == 0){
      return -1;
  }

  // Copy user memory from parent to child.
  if(uvmcopy(p->pagetable, np->pagetable, p->sz) < 0){
     freeproc(np);
     release(&np->lock);
     return -1;
  }
  np->sz = p->sz;

  // copy saved user registers.
  *(np->trapframe) = *(p->trapframe);

these codes are about to create a new process and alocate it memory from its parent.

My question is when i ask chatgpt and google this line is not deep copy, thats a shallow copy is that right to do that? or thats a deep copy, chatgpt is wrong?

*(np->trapframe) = *(p->trapframe);
  • The specific line `*(np->trapframe) = *(p->trapframe);` is a simple assignment/copy. By itself that can't be a deep copy but the whole code block may be a deep copy of `*p`. But we can't tell from the little code posted. – Support Ukraine Aug 21 '23 at 10:08
  • @SupportUkraine sorry i actually just want to ask if thats a deep copy or shallow copy for trapframe.. – KEIFTH YANG Aug 21 '23 at 10:21
  • If `trapframe` points to "something" that **has no pointers** shallow copy and deep copy is the **same**. In such cases the terms shallow and deep makes little to no sense. It's just a (simple) copy. If `trapframe` points to "something" that **has pointers**, it's shallow copy. – Support Ukraine Aug 21 '23 at 11:12

2 Answers2

1

If you want deep copies (implying that there are pointer members in the struct), you'll have to write a function to do the copying.

*(np->trapframe) = *(p->trapframe);

This will do a shallow copy of the pointed out member. If there are pointer members in trapframe, the pointers will be copied, but not what they point at.


The code snippet you've shared could very well be a part of a deep copying function though in which *(np->trapframe) = *(p->trapframe); does a shallow copy of a single member.

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
  • yea i kind of getting it. actually i just want to ask if thats a deep copy or shallow copy for trapframe. And if thats a shallow copy.Isn't that weird to fork a process and save the trampframe by shallow copy since one can change the parents register by changing the child's register? – KEIFTH YANG Aug 21 '23 at 10:24
  • @KEIFTHYANG As I said in the answer _"This will do a shallow copy of the pointed out member. If there are pointer members in `trapframe`, the pointers will be copied, but not what they point at."_. - Are there pointers in `trapframe`? If so, they will be copied but what they are pointing at will not be copied/duplicated. – Ted Lyngmo Aug 21 '23 at 10:35
  • @KEIFTHYANG You're welcome! Glad it helped! – Ted Lyngmo Aug 21 '23 at 10:43
1

You cannot tell if this is a "deep" or "shallow" copy without any context.

Generally "shallow" copy means that a stuct might have pointer members and if you just copy the struct by assignment, then those pointer members won't get duplicated properly. Whereas in case the struct has no pointer members, copying it by assignment will work just fine.

There is no telling what might be appropriate in this case without viewing the struct. As for ChatGPT it is a text generator/hallucinator - it knows nothing about programming, it just generates things it thinks you would like to hear, lying blatantly if necessary.

Lundin
  • 195,001
  • 40
  • 254
  • 396