The most extreme shallow copy is just copying a pointer to a large
data structure like a graph or tree.  Deep copy is copying the entire
data structure and returning a new pointer.

Here is a more common example:

typedef struct list {
  struct list *next;
  void *data;
} LIST_NODE;

In practice you'd want to code these as loops instead of recursion.
But this gives the idea.

LIST_NODE *shallow_copy(LIST_NODE *lst)
{
  return lst ? new_list_node(shallow_copy(lst->next), data) : NULL;
}

LIST_NODE *deep_copy(LIST_NODE *lst)
{
  return lst ? new_list_node(deep_copy(lst->next), data_copy(data)) :
NULL;
}

Where data_copy is assumed to copy its argument into fresh memory and
return a poionter and new_list_node returns a freshly allocated node
with given fields.


On Nov 18, 6:33 am, rahul sharma <rahul23111...@gmail.com> wrote:
> plz give me xample of these two .....as per from book m not able to get
> thesw clearly,...i have reda from wiki and got it but cant relate with
> these...plz differ b/w these two with xample..thnx in advance
>
> a shallow copy of an object copies all the member field values.this works
> well if the fields are values,but may not be what we want for fields that
> point to dynamically allocsted value.The pointer will be copied.But memory
> it point will not be copied.
>
> a deep cpy copies all fields,and makes copies of dynamically allocated
> memory pointed to by the fields..

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to