shallow-copy

When should I pass or return a struct by value?

断了今生、忘了曾经 提交于 2019-11-27 00:51:34
问题 A struct can be either passed/returned by value or passed/returned by reference (via a pointer) in C. The general consensus seems to be that the former can be applied to small structs without penalty in most cases. See Is there any case for which returning a structure directly is good practice? and Are there any downsides to passing structs by value in C, rather than passing a pointer? And that avoiding a dereference can be beneficial from both a speed and clarity perspective. But what counts

Flexible array member not getting copied when I make a shallow copy of a struct

帅比萌擦擦* 提交于 2019-11-26 18:38:19
问题 I have made a shallow copy a struct I have in the following manner: struct Student{ char *name; int age; Courses *list; //First course (node) Student *friends[]; //Flexible array member stores other student pointers }Student; void shallowCopy(const Student *one){ Student *oneCopy = malloc(sizeof(one) + 20*sizeof(Student*)); *oneCopy = *one; <--------------- ERROR POINTS TO THIS LINE } When I check the first element of the flexible array member it oneCopy , it is null. But if I check the first

How do I create a copy of an object in PHP?

一个人想着一个人 提交于 2019-11-26 14:30:49
It appears that in PHP objects are passed by reference. Even assignment operators do not appear to be creating a copy of the Object. Here's a simple, contrived proof: <?php class A { public $b; } function set_b($obj) { $obj->b = "after"; } $a = new A(); $a->b = "before"; $c = $a; //i would especially expect this to create a copy. set_b($a); print $a->b; //i would expect this to show 'before' print $c->b; //i would ESPECIALLY expect this to show 'before' ?> In both print cases I am getting 'after' So, how do I pass $a to set_b() by value, not by reference? In PHP 5+ objects are passed by

In Java, what is a shallow copy?

若如初见. 提交于 2019-11-26 11:16:37
java.util.Calendar.clone() returns "...a new Calendar with the same properties" and returns "a shallow copy of this Calendar". This does not appear to be a shallow copy as answered here on SO. That question is tagged language-agnostic, Java does not seem to follow the language agnostic definition. As I step through the code I notice that the structure and the elements are copied to this new object, more than the language agnostic structure only. In Java, what is a shallow copy? How does it differ from a Java deep copy (if that exists)? KitsuneYMG A shallow copy just copies the values of the

Shallow copy or Deep copy?

﹥>﹥吖頭↗ 提交于 2019-11-26 08:14:41
问题 I am a bit new to these two methods of copying one object into the other. I am confused and unable to spot out the major difference between deep copy and shallow copy.. I had gone through a lots of theory regarding this, but I need explanation with proper examples.. I have program in which I copy one object into another. --> class A { public int a = 0; public void display() { Console.WriteLine(\"The value of a is \" + a); } } class Program { static void Main(string[] args) { A ob1 = new A();

How do I create a copy of an object in PHP?

筅森魡賤 提交于 2019-11-26 03:38:35
问题 It appears that in PHP objects are passed by reference. Even assignment operators do not appear to be creating a copy of the Object. Here\'s a simple, contrived proof: <?php class A { public $b; } function set_b($obj) { $obj->b = \"after\"; } $a = new A(); $a->b = \"before\"; $c = $a; //i would especially expect this to create a copy. set_b($a); print $a->b; //i would expect this to show \'before\' print $c->b; //i would ESPECIALLY expect this to show \'before\' ?> In both print cases I am

In Java, what is a shallow copy?

非 Y 不嫁゛ 提交于 2019-11-26 02:19:29
问题 java.util.Calendar.clone() returns \"...a new Calendar with the same properties\" and returns \"a shallow copy of this Calendar\". This does not appear to be a shallow copy as answered here on SO. That question is tagged language-agnostic, Java does not seem to follow the language agnostic definition. As I step through the code I notice that the structure and the elements are copied to this new object, more than the language agnostic structure only. In Java, what is a shallow copy? How does

What is the difference between a deep copy and a shallow copy?

这一生的挚爱 提交于 2019-11-25 21:36:49
问题 This post is a Community Wiki . Edit existing answers to improve this post. It is not currently accepting new answers. What is the difference between a deep copy and a shallow copy? 回答1: Shallow copies duplicate as little as possible. A shallow copy of a collection is a copy of the collection structure, not the elements. With a shallow copy, two collections now share the individual elements. Deep copies duplicate everything. A deep copy of a collection is two collections with all of the