<?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/"
	>

<channel>
	<title>Coding Friends &#187; memcpy</title>
	<atom:link href="http://www.codingfriends.com/index.php/tag/memcpy/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codingfriends.com</link>
	<description>Coding Friends, place for developers.</description>
	<lastBuildDate>Sun, 04 Dec 2011 21:11:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>struct &#8211; setup and memory locations</title>
		<link>http://www.codingfriends.com/index.php/2010/07/23/struct-setup-and-memory-locations/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=struct-setup-and-memory-locations</link>
		<comments>http://www.codingfriends.com/index.php/2010/07/23/struct-setup-and-memory-locations/#comments</comments>
		<pubDate>Fri, 23 Jul 2010 09:47:17 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[C / C++]]></category>
		<category><![CDATA[memcpy]]></category>
		<category><![CDATA[struct]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=1109</guid>
		<description><![CDATA[When you setup a struct within c++, it is kinder like having a array of data and if you want to you can access the internal parts by using some memory pointer location fun!. Lets say that you have a struct of struct intvalue &#123; int a; int b; &#125;; Just to say that since [...]]]></description>
			<content:encoded><![CDATA[<p>When you setup a struct within c++, it is kinder like having a array of data and if you want to you can access the internal parts by using some memory pointer location fun!.</p>
<p>Lets say that you have a struct of</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">struct</span> intvalue <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">int</span> a<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">int</span> b<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p>Just to say that since I am using int(eger) values so that is what I am incrementing by in the pointer arithmetic which is why I am casting the pointer to a integer value.</p>
<p>So lets say that we create a variable of intvalue and setup the values as below</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">intvalue testvalue<span style="color: #008080;">;</span>
testvalue.<span style="color: #007788;">a</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">4</span><span style="color: #008080;">;</span>
testvalue.<span style="color: #007788;">b</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">5</span><span style="color: #008080;">;</span></pre></div></div>

<p>We can then pull out the value of a or b, but using memcpy and just outputting to a int(eger) variable as below, the key is the ((int*)&#038;testvalue)+1, this will first convert the testvalue variable to a pointer to memory location and then (int*) casts that pointer to a int pointer, because internally that is what it is, and then just add 1 to it, which points to the second value ( in this case the value of b which is 5)</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">    <span style="color: #0000ff;">int</span> avalue<span style="color: #008080;">;</span>
    <span style="color: #666666;">// convert to a int pointer type and then add one to it (to the next array element as such).</span>
    <span style="color: #0000dd;">memcpy</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>avalue, <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">&amp;</span>testvalue<span style="color: #008000;">&#41;</span><span style="color: #000040;">+</span><span style="color: #0000dd;">1</span>,<span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;a value (or is it the b value :) ) &quot;</span> <span style="color: #000080;">&lt;&lt;</span> avalue <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span></pre></div></div>

<p>The output would be</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">a value <span style="color: #7a0874; font-weight: bold;">&#40;</span>or is it the b value :<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000;">5</span></pre></div></div>

<p>because I am pointing to the second value which is b and thus 5 <img src='http://www.codingfriends.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<p>Of course if you just wanted the value of first int (the value of a in this case) you do not add the 1 to the memory location, for example</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">    <span style="color: #0000dd;">memcpy</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>avalue, <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">&amp;</span>testvalue<span style="color: #008000;">&#41;</span>,<span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>this time I am just converting the testvalue (casting) to a int pointer and thus pointing to the start of the struct and that is where the int a variable is living <img src='http://www.codingfriends.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2010/07/23/struct-setup-and-memory-locations/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>memcpy &#8211; copy the memory contents from one place to another</title>
		<link>http://www.codingfriends.com/index.php/2010/03/05/memcpy-copy-the-memory-contents-from-one-place-to-another/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=memcpy-copy-the-memory-contents-from-one-place-to-another</link>
		<comments>http://www.codingfriends.com/index.php/2010/03/05/memcpy-copy-the-memory-contents-from-one-place-to-another/#comments</comments>
		<pubDate>Fri, 05 Mar 2010 09:52:47 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[C / C++]]></category>
		<category><![CDATA[memcpy]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=778</guid>
		<description><![CDATA[The memcpy function, does not really care about the parameters passed in, if they are the same types or of different types. Just that they are variables already declared (and of course can hold the data being passed from one place to another, because otherwise there may be a slight overlap if the copied value [...]]]></description>
			<content:encoded><![CDATA[<p>The memcpy function, does not really care about the parameters passed in, if they are the same types or of different types.  Just that they are variables already declared (and of course can hold the data being passed from one place to another, because otherwise there may be a slight overlap if the copied value is larger in memory size to the copier).</p>
<p>So the memcpy is just a binary data copy and nothing else, there is no type checking. </p>
<p>Here is a example in c++ code that will copy a newly created struct(ure) from one place to another.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> <span style="color: #0000dd;">memcpy</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span> <span style="color: #000040;">*</span>copied_to, <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">void</span> <span style="color: #000040;">*</span>copier, <span style="color: #0000ff;">size_t</span> num<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>where the return is also the copied_to value, since we are not altering the copier then a const(ant) can be applied and the last parameter is num which is the size of the data you want to copy.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;stdio.h&gt;</span>
<span style="color: #339900;">#include &lt;string.h&gt;</span>
&nbsp;
<span style="color: #666666;">// diferent type of object, e.g. not a int,char</span>
<span style="color: #0000ff;">typedef</span> <span style="color: #0000ff;">struct</span> 
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">int</span> bob<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span> insertType<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #666666;">// create two variables of insertType with default values for bob of 10 and 20</span>
  insertType t1 <span style="color: #000080;">=</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000dd;">10</span> <span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
  insertType t2 <span style="color: #000080;">=</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000dd;">20</span> <span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #666666;">// output the declared values</span>
  <span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;t1 = %d<span style="color: #000099; font-weight: bold;">\n</span>t2 = %d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, t1.<span style="color: #007788;">bob</span>, t2.<span style="color: #007788;">bob</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #666666;">// do a memory copy of t2 into t1 only copy as big as the variable size.</span>
  <span style="color: #0000dd;">memcpy</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>t1, <span style="color: #000040;">&amp;</span>t2, <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>insertType<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #666666;">// output the new values</span>
  <span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;t1 = %d<span style="color: #000099; font-weight: bold;">\n</span>t2 = %d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, t1.<span style="color: #007788;">bob</span>, t2.<span style="color: #007788;">bob</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>here is the output</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">t1 = <span style="color: #000000;">10</span>
t2 = <span style="color: #000000;">20</span>
t1 = <span style="color: #000000;">20</span>
t2 = <span style="color: #000000;">20</span></pre></div></div>

<p>also note that the size_t is a unsigned integral type (normal of int, of the base operating system)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2010/03/05/memcpy-copy-the-memory-contents-from-one-place-to-another/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

