<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Cesar Gimenes</title>
	<atom:link href="http://gimenes.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://gimenes.wordpress.com</link>
	<description>Dicas, comentários, códigos, etc.</description>
	<lastBuildDate>Mon, 30 Mar 2009 17:55:56 +0000</lastBuildDate>
	<language>pt-br</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='gimenes.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Cesar Gimenes</title>
		<link>http://gimenes.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://gimenes.wordpress.com/osd.xml" title="Cesar Gimenes" />
	<atom:link rel='hub' href='http://gimenes.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Economize memória combinando union e struct</title>
		<link>http://gimenes.wordpress.com/2006/12/19/economize-memoria-combinando-union-e-struct/</link>
		<comments>http://gimenes.wordpress.com/2006/12/19/economize-memoria-combinando-union-e-struct/#comments</comments>
		<pubDate>Tue, 19 Dec 2006 23:56:39 +0000</pubDate>
		<dc:creator>Cesar</dc:creator>
				<category><![CDATA[C/C++]]></category>

		<guid isPermaLink="false">http://gimenes.wordpress.com/2006/12/19/economize-memoria-combinando-union-e-struct/</guid>
		<description><![CDATA[Muitas vezes a informação que queremos armazenar é muito menor que um int de 32 bits, por exemplo usar uma variavel int para armazenar um dado booleano. Isso é um enorme desperdício de espaço. Entretanto podemos combinar um int com uma struct e obter um código muito elegante, que vai economizar memória e de quebra [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gimenes.wordpress.com&amp;blog=197727&amp;post=7&amp;subd=gimenes&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Muitas vezes a informação que queremos armazenar é muito menor que um int de 32 bits, por exemplo usar uma variavel int para armazenar um dado booleano. Isso é um enorme desperdício de espaço.</p>
<p>Entretanto podemos combinar um int com uma struct e obter um código muito elegante, que vai economizar memória e de quebra trás algumas vantagens, veja o exemplo:</p>
<pre><span style="color:#2e8b57;"><strong>union</strong></span> control
{
    <span style="color:#2e8b57;"><strong>unsigned</strong></span> <span style="color:#2e8b57;"><strong>int</strong></span> flag;
    <span style="color:#2e8b57;"><strong>struct</strong></span> f
    {
        <span style="color:#2e8b57;"><strong>unsigned</strong></span> <span style="color:#2e8b57;"><strong>int</strong></span> id:<span style="color:#ff00ff;">16</span>;
        <span style="color:#2e8b57;"><strong>unsigned</strong></span> <span style="color:#2e8b57;"><strong>int</strong></span> isRootWindow:<span style="color:#ff00ff;">1</span>;
        <span style="color:#2e8b57;"><strong>unsigned</strong></span> <span style="color:#2e8b57;"><strong>int</strong></span> widgetType:<span style="color:#ff00ff;">8</span>;
        <span style="color:#2e8b57;"><strong>unsigned</strong></span> <span style="color:#2e8b57;"><strong>int</strong></span> mouseIn:<span style="color:#ff00ff;">1</span>;
        <span style="color:#2e8b57;"><strong>unsigned</strong></span> <span style="color:#2e8b57;"><strong>int</strong></span> pixelSize:<span style="color:#ff00ff;">3</span>;
        <span style="color:#2e8b57;"><strong>unsigned</strong></span> <span style="color:#2e8b57;"><strong>int</strong></span> reserved:<span style="color:#ff00ff;">2</span>;
    } f;
} control;</pre>
<p>Note que declaramos uma variável unsigned int chamada flag no mesmo nível que a struct f. O union vai &#8220;unir&#8221; flag e f fazendo com que as duas variáveis tenham o mesmo endereço na memória.<br />
Mas ai vem o truque, note na declaração dos membros da struct f nos definimos o comprimento em bits de cada um colocando no final da declaração &#8220;:n&#8221; onde n é o numero em bits.</p>
<p>Por exemplo: &#8220;:16&#8243; em id faz com que ela tenha 16 bits de comprimento e colocar :1 em mouseIn faz com que ela tenha apenas um bit de comprimento.</p>
<p>Como a struct f e a int flag tem exatamente 32 bits de tamanho as duas combinam perfeitamente.<br />
<strong>Como usar:</strong></p>
<p>Podemos acessar cada membros individualmente como nos exemplos:<br />
&#8230; ptr-&gt;control.f.id++; &#8230;<br />
ou<br />
&#8230; if (ptr-&gt;control.f.mouseIn) &#8230;</p>
<p>Ou podemos acessar todos de uma só vez via flag:<br />
&#8230; ptr-&gt;control.flag = 0;</p>
<p><strong>Vantagens:</strong><br />
Otimiza o uso da memória.<br />
É um meio mais fácil de acessar dados booleanos que se usarmos o modo antigo usando AND, XOR,etc.<br />
Como as informações ficam mais próximas geralmente o uso de memória cache é melhora.</p>
<p><strong>Desvamtagens:</strong><br />
Depende da plataforma, você pode ter que ajustar seu código para ele rodar corretamente em plataformas diferentes da original.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/gimenes.wordpress.com/7/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/gimenes.wordpress.com/7/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gimenes.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gimenes.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/gimenes.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/gimenes.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/gimenes.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/gimenes.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/gimenes.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/gimenes.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/gimenes.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/gimenes.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/gimenes.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/gimenes.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/gimenes.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/gimenes.wordpress.com/7/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gimenes.wordpress.com&amp;blog=197727&amp;post=7&amp;subd=gimenes&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://gimenes.wordpress.com/2006/12/19/economize-memoria-combinando-union-e-struct/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/143324deb3feeeb0e94f81db0a0e36c6?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Cesar</media:title>
		</media:content>
	</item>
	</channel>
</rss>
