<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-GB">
	<id>https://wiki.kram.nz/index.php?action=history&amp;feed=atom&amp;title=SE250%3Alab-2%3Ajham005</id>
	<title>SE250:lab-2:jham005 - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.kram.nz/index.php?action=history&amp;feed=atom&amp;title=SE250%3Alab-2%3Ajham005"/>
	<link rel="alternate" type="text/html" href="https://wiki.kram.nz/index.php?title=SE250:lab-2:jham005&amp;action=history"/>
	<updated>2026-04-29T02:04:23Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.45.3</generator>
	<entry>
		<id>https://wiki.kram.nz/index.php?title=SE250:lab-2:jham005&amp;diff=4899&amp;oldid=prev</id>
		<title>Mark: 1 revision(s)</title>
		<link rel="alternate" type="text/html" href="https://wiki.kram.nz/index.php?title=SE250:lab-2:jham005&amp;diff=4899&amp;oldid=prev"/>
		<updated>2008-11-03T05:18:58Z</updated>

		<summary type="html">&lt;p&gt;1 revision(s)&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== John Hamer&amp;#039;s lab #2 ==&lt;br /&gt;
&lt;br /&gt;
Pointers are 4 bytes on both the lab PCs and the Linux server.&lt;br /&gt;
This is the same size as int, long and float on both machines.&lt;br /&gt;
I had hoped we might see 64-bit (8 byte) pointers on Linux,&lt;br /&gt;
which should be enabled with the GCC &amp;lt;tt&amp;gt;-m64&amp;lt;/tt&amp;gt; flag.&lt;br /&gt;
However, 64-bit support does not appear to be available.&lt;br /&gt;
&lt;br /&gt;
The expression &amp;lt;tt&amp;gt;(long)(&amp;amp;x - &amp;amp;y)&amp;lt;/tt&amp;gt; does the subtraction first,&lt;br /&gt;
followed by the (redundant) conversion to long.  Pointer subtraction&lt;br /&gt;
in C is defined in terms of the size of the thing pointed at, so the&lt;br /&gt;
result (1) indicates 1 int (a 4-byte quantity).  In contrast,&lt;br /&gt;
&amp;lt;tt&amp;gt;(long)&amp;amp;x - (long)&amp;amp;y&amp;lt;/tt&amp;gt; is a subtraction of two longs, so no&lt;br /&gt;
pointer-size scaling is done.  The result is the number of bytes (4)&lt;br /&gt;
between the two addresses.&lt;br /&gt;
&lt;br /&gt;
Declaring a char array between &amp;lt;tt&amp;gt;x&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;y&amp;lt;/tt&amp;gt; shows that&lt;br /&gt;
the compiler gets to choose where things actually go, and can decide&lt;br /&gt;
to allocate more memory between variables than absolutely necessary.&lt;br /&gt;
There are often (significant) performance advantages in doing this,&lt;br /&gt;
as modern hardware reads and writes memory in 4 or 8 (or larger) byte blocks.&lt;br /&gt;
&lt;br /&gt;
Arrays are worth a moment of careful thought.  Clearly the contents of&lt;br /&gt;
an array are accessed through a pointer, but the pointer is not actually&lt;br /&gt;
stored in memory.  Rather, an array is a &amp;quot;constant&amp;quot; pointer.  You can use &amp;lt;tt&amp;gt;arr&amp;lt;/tt&amp;gt; as&lt;br /&gt;
a pointer but you cannot change its value, rather like you can use but&lt;br /&gt;
cannot change the value of the number 3.  C treats &amp;lt;tt&amp;gt;&amp;amp;arr&amp;lt;/tt&amp;gt; the&lt;br /&gt;
same as &amp;lt;tt&amp;gt;arr&amp;lt;/tt&amp;gt; when you use it as a pointer.  The array indexing expression &amp;lt;tt&amp;gt;&amp;amp;arr[4]&amp;lt;/tt&amp;gt;&lt;br /&gt;
is the same as &amp;lt;tt&amp;gt;arr + 4&amp;lt;/tt&amp;gt; &amp;amp;mdash; an address 4 elements past the start of the array.&lt;br /&gt;
&lt;br /&gt;
Care is needed when taking a pointer to a local (auto) variable.  Returning a pointer&lt;br /&gt;
to an auto variable is almost certainly wrong, and will result in unpredictable behaviour.&lt;br /&gt;
&lt;br /&gt;
Structs are blocks of memory, where the components can be individually accessed.  The compiler may&lt;br /&gt;
alignment the components of a struct, e.g. allocating two or even four bytes for a char.&lt;br /&gt;
&lt;br /&gt;
Unions appear like structs, but all the components overlap in memory.  The size of a union is the&lt;br /&gt;
size of the largest component.&lt;br /&gt;
&lt;br /&gt;
Malloc returns a new, unused block of memory (or 0 if memory is exhaused &amp;amp;mdash; your programs&lt;br /&gt;
need to check for this).  This memory persists until explicitly released by calling &amp;lt;tt&amp;gt;free&amp;lt;/tt&amp;gt;.&lt;br /&gt;
Freed memory can be re-used by a subsequent call to malloc.&lt;br /&gt;
&lt;br /&gt;
A function is a constant pointer.  Functions typically reside near the area used for static variables.&lt;/div&gt;</summary>
		<author><name>Mark</name></author>
	</entry>
</feed>