<?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"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Data Hiding in C: Programming Using Incomplete and Derived Types</title>
	<atom:link href="http://tinobox.com/wordpress/c-programming/incomplete-types-in-c-programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://tinobox.com/wordpress/c-programming/incomplete-types-in-c-programming/</link>
	<description>Odds and ends of internet entrepreneurship—A Technical Diary</description>
	<lastBuildDate>Wed, 15 Feb 2012 09:23:47 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
	<item>
		<title>By: David M. Doolin, PhD</title>
		<link>http://tinobox.com/wordpress/c-programming/incomplete-types-in-c-programming/comment-page-1/#comment-6081</link>
		<dc:creator>David M. Doolin, PhD</dc:creator>
		<pubDate>Sat, 10 Sep 2011 00:22:03 +0000</pubDate>
		<guid isPermaLink="false">http://tinobox.com/wordpress/?p=3#comment-6081</guid>
		<description>1. Yes and no. If you can do your own name mangling, you can send create a set of functions which handle the cases most useful for you. Warning: if you go down this road, and find yourself writing code generators, you might as well move to c++...

2. I&#039;ll write some test code on setting names, but what you&#039;re proposing is very close to one good way to do it.

3. I think this is overkill. These are public fields, &lt;del datetime=&quot;2011-09-10T05:02:11+00:00&quot;&gt;so p-&gt;artist_name is just fine&lt;/del&gt; &lt;ins datetime=&quot;2011-09-10T05:02:11+00:00&quot;&gt;but it really depends on whether you want your strings on the stack or on the heap. What I&#039;ve shown here expects an allocated char buffer, hence the calls to free.  Also, the code above may or may not complain about unallocated memory, I&#039;m going to revise it slightly to mitigate that.&lt;/ins&gt; 

If you wanted to make them private fields, use char * get_artist_name(Painting * p) to send back a copy. However, I&#039;m going to think about this and write up some test code to see how it behaves, and figure out what a viable use case would be.

Thanks for your observations. I code very little C these days, but it&#039;s still one of my favorite languages.</description>
		<content:encoded><![CDATA[<p>1. Yes and no. If you can do your own name mangling, you can send create a set of functions which handle the cases most useful for you. Warning: if you go down this road, and find yourself writing code generators, you might as well move to c++&#8230;</p>
<p>2. I&#8217;ll write some test code on setting names, but what you&#8217;re proposing is very close to one good way to do it.</p>
<p>3. I think this is overkill. These are public fields, <del datetime="2011-09-10T05:02:11+00:00">so p->artist_name is just fine</del> <ins datetime="2011-09-10T05:02:11+00:00">but it really depends on whether you want your strings on the stack or on the heap. What I&#8217;ve shown here expects an allocated char buffer, hence the calls to free.  Also, the code above may or may not complain about unallocated memory, I&#8217;m going to revise it slightly to mitigate that.</ins> </p>
<p>If you wanted to make them private fields, use char * get_artist_name(Painting * p) to send back a copy. However, I&#8217;m going to think about this and write up some test code to see how it behaves, and figure out what a viable use case would be.</p>
<p>Thanks for your observations. I code very little C these days, but it&#8217;s still one of my favorite languages.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dennis Dillert</title>
		<link>http://tinobox.com/wordpress/c-programming/incomplete-types-in-c-programming/comment-page-1/#comment-6080</link>
		<dc:creator>Dennis Dillert</dc:creator>
		<pubDate>Fri, 09 Sep 2011 23:58:23 +0000</pubDate>
		<guid isPermaLink="false">http://tinobox.com/wordpress/?p=3#comment-6080</guid>
		<description>Thanks for your reply David.

1:
Ok, so you wouldn&#039;t actually include it in the set function because then the user might not see the malloc and thus forget to free it?

2:
You said you guard against possible buffer overflows, would it be unsafe if I do it like this:

void painting_set_artist_name(Painting * p, char * artist_name) {
size_t s = strlen(artist_name)+1; // check inside the function
strncpy(p-&gt;artist_name, artist_name, s);
p-&gt;artist_name[s] = &#039;&#039;;
}

painting_set_artist_name(p, &quot;Picasso&quot;);

3:
Basically, I wanted to know what you think about the technique to include a function pointer as is done in the struct below:

struct painting {
   uint32_t inventory_control;
   uint32_t purchase_price; 
   char * painting_name;
   char * artist_name;
   char * owner_name;
   char * (*get_artist_name)(Painting *);
   void (*set_artist_name)(Painting *, char *);
};

// this could also be done in a separate setup function when creating the object
p-&gt;set_artist_name = &amp;painting_set_artist_name;

// this would be our call in the code
p-&gt;set_artist_name(p, &quot;Picasso&quot;);</description>
		<content:encoded><![CDATA[<p>Thanks for your reply David.</p>
<p>1:<br />
Ok, so you wouldn&#8217;t actually include it in the set function because then the user might not see the malloc and thus forget to free it?</p>
<p>2:<br />
You said you guard against possible buffer overflows, would it be unsafe if I do it like this:</p>
<p>void painting_set_artist_name(Painting * p, char * artist_name) {<br />
size_t s = strlen(artist_name)+1; // check inside the function<br />
strncpy(p-&gt;artist_name, artist_name, s);<br />
p-&gt;artist_name[s] = &#8221;;<br />
}</p>
<p>painting_set_artist_name(p, &#8220;Picasso&#8221;);</p>
<p>3:<br />
Basically, I wanted to know what you think about the technique to include a function pointer as is done in the struct below:</p>
<p>struct painting {<br />
   uint32_t inventory_control;<br />
   uint32_t purchase_price;<br />
   char * painting_name;<br />
   char * artist_name;<br />
   char * owner_name;<br />
   char * (*get_artist_name)(Painting *);<br />
   void (*set_artist_name)(Painting *, char *);<br />
};</p>
<p>// this could also be done in a separate setup function when creating the object<br />
p-&gt;set_artist_name = &amp;painting_set_artist_name;</p>
<p>// this would be our call in the code<br />
p-&gt;set_artist_name(p, &#8220;Picasso&#8221;);</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David M. Doolin, PhD</title>
		<link>http://tinobox.com/wordpress/c-programming/incomplete-types-in-c-programming/comment-page-1/#comment-6079</link>
		<dc:creator>David M. Doolin, PhD</dc:creator>
		<pubDate>Fri, 09 Sep 2011 22:48:42 +0000</pubDate>
		<guid isPermaLink="false">http://tinobox.com/wordpress/?p=3#comment-6079</guid>
		<description>Dennis, I&#039;ll take these one at a time:

&lt;ol&gt;
	&lt;li&gt; 
&lt;blockquote&gt;&quot;how to allocate memory for the pointer fields.&quot;&lt;/blockquote&gt;

 This is a great question. The answer partly depends on how you&#039;re using the object. For the &lt;code&gt;char *&lt;/code&gt; in this struct, I&#039;m assuming that the using code will allocate somewhere along the way, and - &lt;em&gt;this is important&lt;/em&gt; - these pointers aren&#039;t referenced by any other code.&lt;/li&gt;

	&lt;li&gt;
&lt;blockquote&gt;&quot;why you passed the character length in a separate parameter instead of determining it directly in the function itself?&quot;&lt;/blockquote&gt;

 Not sure exactly what you mean. Which line of which snippet?&lt;/li&gt;

	&lt;li&gt;
&lt;blockquote&gt;&quot;What do you think of using function pointers in the struct itself to access the fields?&quot;&lt;/blockquote&gt;

 Not exactly sure what you mean, please post a little snippet. Also, read the articles on &lt;a href=&quot;http://tinobox.com/wordpress/tag/single-inheritance/&quot; rel=&quot;nofollow&quot;&gt;single inheritance in c&lt;/a&gt;, that might asnwer your question.&lt;/li&gt;
&lt;/ol&gt;

</description>
		<content:encoded><![CDATA[<p>Dennis, I&#8217;ll take these one at a time:</p>
<ol>
<li>
<blockquote><p>&#8220;how to allocate memory for the pointer fields.&#8221;</p></blockquote>
<p> This is a great question. The answer partly depends on how you&#8217;re using the object. For the <code>char *</code> in this struct, I&#8217;m assuming that the using code will allocate somewhere along the way, and &#8211; <em>this is important</em> &#8211; these pointers aren&#8217;t referenced by any other code.</li>
<li>
<blockquote><p>&#8220;why you passed the character length in a separate parameter instead of determining it directly in the function itself?&#8221;</p></blockquote>
<p> Not sure exactly what you mean. Which line of which snippet?</li>
<li>
<blockquote><p>&#8220;What do you think of using function pointers in the struct itself to access the fields?&#8221;</p></blockquote>
<p> Not exactly sure what you mean, please post a little snippet. Also, read the articles on <a href="http://tinobox.com/wordpress/tag/single-inheritance/" rel="nofollow">single inheritance in c</a>, that might asnwer your question.</li>
</ol>
<p><span class="cluv">David M. Doolin, PhD&#180;s last [type] ..<a class="5b5d3cf3f6 6079" rel="nofollow" href="http://website-in-a-weekend.net/building-community/why-free-software-costs-so-little-and-so-much/">Why Free Software costs so little… and costs so much</a></span></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dennis Dillert</title>
		<link>http://tinobox.com/wordpress/c-programming/incomplete-types-in-c-programming/comment-page-1/#comment-6077</link>
		<dc:creator>Dennis Dillert</dc:creator>
		<pubDate>Fri, 09 Sep 2011 22:14:24 +0000</pubDate>
		<guid isPermaLink="false">http://tinobox.com/wordpress/?p=3#comment-6077</guid>
		<description>Thanks for another great article, David. I&#039;m looking forward to the update on how to allocate memory for the pointer fields. Also, I wanted to ask why you passed the character length in a separate parameter instead of determining it directly in the function itself? And one last question: What do you think of using function pointers in the struct itself to access the fields?</description>
		<content:encoded><![CDATA[<p>Thanks for another great article, David. I&#8217;m looking forward to the update on how to allocate memory for the pointer fields. Also, I wanted to ask why you passed the character length in a separate parameter instead of determining it directly in the function itself? And one last question: What do you think of using function pointers in the struct itself to access the fields?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David M. Doolin, PhD</title>
		<link>http://tinobox.com/wordpress/c-programming/incomplete-types-in-c-programming/comment-page-1/#comment-2113</link>
		<dc:creator>David M. Doolin, PhD</dc:creator>
		<pubDate>Tue, 29 Sep 2009 15:47:35 +0000</pubDate>
		<guid isPermaLink="false">http://tinobox.com/wordpress/?p=3#comment-2113</guid>
		<description>@nyan - I think scope will take care of that, but please post an example of what you mean and I&#039;ll update the article.
.-= David M. Doolin, PhD&#180;s last blog ..&lt;a href=&quot;http://website-in-a-weekend.net/creating-content/heck-html-title-element/&quot; rel=&quot;nofollow&quot;&gt;What the Heck is HTML TITLE element and why do I need one?&lt;/a&gt; =-.</description>
		<content:encoded><![CDATA[<p>@nyan &#8211; I think scope will take care of that, but please post an example of what you mean and I&#8217;ll update the article.<br />
.-= David M. Doolin, PhD&#180;s last blog ..<a href="http://website-in-a-weekend.net/creating-content/heck-html-title-element/" rel="nofollow">What the Heck is HTML TITLE element and why do I need one?</a> =-.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nyan</title>
		<link>http://tinobox.com/wordpress/c-programming/incomplete-types-in-c-programming/comment-page-1/#comment-2112</link>
		<dc:creator>Nyan</dc:creator>
		<pubDate>Tue, 29 Sep 2009 14:59:40 +0000</pubDate>
		<guid isPermaLink="false">http://tinobox.com/wordpress/?p=3#comment-2112</guid>
		<description>but what if the client define the structure/union with the same name as hidden struct/union name??</description>
		<content:encoded><![CDATA[<p>but what if the client define the structure/union with the same name as hidden struct/union name??</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: unixtechie</title>
		<link>http://tinobox.com/wordpress/c-programming/incomplete-types-in-c-programming/comment-page-1/#comment-1296</link>
		<dc:creator>unixtechie</dc:creator>
		<pubDate>Mon, 08 Jun 2009 06:21:23 +0000</pubDate>
		<guid isPermaLink="false">http://tinobox.com/wordpress/?p=3#comment-1296</guid>
		<description>www.archive.org == &quot;Internet Archive Wayback Machine&quot;, i.e. a site that archives old versions of web pages.

Therefore the essay can be found in a few seconds, if you have the original URL (and you did):
http://web.archive.org/web/20070630120840/http://www.dekorte.com/docs/essays/ooc/</description>
		<content:encoded><![CDATA[<p><a href="http://www.archive.org" rel="nofollow">http://www.archive.org</a> == &#8220;Internet Archive Wayback Machine&#8221;, i.e. a site that archives old versions of web pages.</p>
<p>Therefore the essay can be found in a few seconds, if you have the original URL (and you did):<br />
<a href="http://web.archive.org/web/20070630120840/http://www.dekorte.com/docs/essays/ooc/" rel="nofollow">http://web.archive.org/web/20070630120840/http://www.dekorte.com/docs/essays/ooc/</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: How To Hide A Struct Member in the C Programming Language &#124; There Is NO Box</title>
		<link>http://tinobox.com/wordpress/c-programming/incomplete-types-in-c-programming/comment-page-1/#comment-1176</link>
		<dc:creator>How To Hide A Struct Member in the C Programming Language &#124; There Is NO Box</dc:creator>
		<pubDate>Mon, 01 Jun 2009 22:08:09 +0000</pubDate>
		<guid isPermaLink="false">http://tinobox.com/wordpress/?p=3#comment-1176</guid>
		<description>[...] Here&#8217;s much longer article which goes into more detail on incomplete and derived types. Similar [...]</description>
		<content:encoded><![CDATA[<p>[...] Here&#8217;s much longer article which goes into more detail on incomplete and derived types. Similar [...]</p>
]]></content:encoded>
	</item>
</channel>
</rss>

