<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>
<channel>
	<title>Comments on: SystemVerilog Gotcha: (when copying) a struct is not a class by another name</title>
	<atom:link href="http://www.verilab.com/blog/2008/01/gotcha-when-copying-a-struct-is-not-a-class-by-a-different-name/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.verilab.com/blog/2008/01/gotcha-when-copying-a-struct-is-not-a-class-by-a-different-name/</link>
	<description>Verilab</description>
	<pubDate>Thu, 17 May 2012 19:35:55 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.1</generator>
		<item>
		<title>By: Jason Sprott</title>
		<link>http://www.verilab.com/blog/2008/01/gotcha-when-copying-a-struct-is-not-a-class-by-a-different-name/#comment-155</link>
		<dc:creator>Jason Sprott</dc:creator>
		<pubDate>Wed, 23 Jan 2008 08:28:30 +0000</pubDate>
		<guid isPermaLink="false">http://www.verilab.com/blog/2008/01/gotcha-when-copying-a-struct-is-not-a-class-by-a-different-name/#comment-155</guid>
		<description>Hi Daniel

Thanks for pointing out that error (now corrected). I fixed the variable names and also added the new() of the elements in Y[], inside the foreach loop, for clarity.

Yes, when a struct is copied the values are copied. When a class is copied the reference is copied, or a shallow copy is taken (as described in IEEE1800-2005, Section 7.11). I find people typically remember that when they are dealing with the class itself. When classes are inside something else, it's easy to forget, because we are copying the thing and may not even have visibility of its contents. Arrays of classes fall into that category, but a class composed of other classes is another big one to watch.

There is no method for a deep copy/clone defined in the SystemVerilog language. Both the AVM and VMM define methods for copying objects as part of the methodology. In Vera we had a built-in method called object_copy() as part of the language. This provided a deep copy of any object, without you having to write anything yourself. I found it useful, but the problem was that there was no control, so you could end up copying something *very* large.

Of course when writing custom copy functions we sometimes put bugs in them too.

Jason</description>
		<content:encoded><![CDATA[<p>Hi Daniel</p>
<p>Thanks for pointing out that error (now corrected). I fixed the variable names and also added the new() of the elements in Y[], inside the foreach loop, for clarity.</p>
<p>Yes, when a struct is copied the values are copied. When a class is copied the reference is copied, or a shallow copy is taken (as described in IEEE1800-2005, Section 7.11). I find people typically remember that when they are dealing with the class itself. When classes are inside something else, it&#8217;s easy to forget, because we are copying the thing and may not even have visibility of its contents. Arrays of classes fall into that category, but a class composed of other classes is another big one to watch.</p>
<p>There is no method for a deep copy/clone defined in the SystemVerilog language. Both the AVM and VMM define methods for copying objects as part of the methodology. In Vera we had a built-in method called object_copy() as part of the language. This provided a deep copy of any object, without you having to write anything yourself. I found it useful, but the problem was that there was no control, so you could end up copying something *very* large.</p>
<p>Of course when writing custom copy functions we sometimes put bugs in them too.</p>
<p>Jason</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Daniel Pörsch</title>
		<link>http://www.verilab.com/blog/2008/01/gotcha-when-copying-a-struct-is-not-a-class-by-a-different-name/#comment-154</link>
		<dc:creator>Daniel Pörsch</dc:creator>
		<pubDate>Tue, 22 Jan 2008 13:37:14 +0000</pubDate>
		<guid isPermaLink="false">http://www.verilab.com/blog/2008/01/gotcha-when-copying-a-struct-is-not-a-class-by-a-different-name/#comment-154</guid>
		<description>Hi Verilab,

just a short note, you define A,B as classes and X,Y as structs in the code, but refer to A,B as structs and X,Y as classes in the text. This is confusing, I guess the text is correct?!

What would happen if I didn't have arrays of classes/structs but a single class and a single struct, I guess the same rule would apply, i.e. copy-by-reference for classes and copy-by-value for structs?

There's no Copy-Constructor in SV, is there? Would be handy ...

Rgds,
Daniel</description>
		<content:encoded><![CDATA[<p>Hi Verilab,</p>
<p>just a short note, you define A,B as classes and X,Y as structs in the code, but refer to A,B as structs and X,Y as classes in the text. This is confusing, I guess the text is correct?!</p>
<p>What would happen if I didn&#8217;t have arrays of classes/structs but a single class and a single struct, I guess the same rule would apply, i.e. copy-by-reference for classes and copy-by-value for structs?</p>
<p>There&#8217;s no Copy-Constructor in SV, is there? Would be handy &#8230;</p>
<p>Rgds,<br />
Daniel</p>
]]></content:encoded>
	</item>
</channel>
</rss>

